title: Fastest and most efficient way to find the maximum no. that can be obtained by performing bitwise and on 2 DISTINCT elements of array
body: <p>Given an array of non-negative integers, what is the fastest and most efficient way to find the maximum no. that can be obtained by performing bitwise and (i.e, &amp; operator) on 2 DISTINCT elements of the array?</p>
  
  <p>This  is my code until now : </p>
  
  <pre><code>max = 0
  for(i=0; i&lt;n; i++)
  {
      for(j=i+1; j&lt;n; j++)
      {
          temp = a[i] &amp; a[j];
          if(temp &gt; max)
              max = temp
      }
   }
  </code></pre>
  
  <p>This, of course, is the naive method. I am looking for a more efficient solution.</p>
  
  <p>Maybe something like using a trie(actually a binary tree) to find max XOR of elements of array. The description for the max XOR solution can be found at <a href="http://threads-iiith.quora.com/Tutorial-on-Trie-and-example-problems?share=1" rel="nofollow">http://threads-iiith.quora.com/Tutorial-on-Trie-and-example-problems?share=1</a></p>