Firstly your code in the first link is not N^2 solution (not exactly). The time complexity for that is a lil tricky.

The first thing to notice is that, the numbers can only be under a 32 bit signed integer. So when you’d loop through each number, and then each number is being tested for it’s 32 bits. When you find a set bit (say kth bit is set), you’re looping through numbers from i to n -1, where possibility of finding a number with kth set bit need not travel till end of the array (unless for one tricky test case, [1, 2, 4, 8, 16, ...........2^{30}] and that too has a length of only 30). So finding the next element with kth set bit can be found in less than N iterations. So your code is not actually O(N^2) but simply O(32*N). Hope you understood.