Problem link : contest practice Difficulty : Simple Prerequisites : Basic Math, Recursion, Bit Operations Problem : Given an array find maximum AND(&) possible between any two elements. ExplanationAt first, let's consider some partial solutions. How to get 50 pointsHere you can go through each possible pair and check what AND value is. And we can find what is the maximum AND possible. Complexity of this will be O(N*N).
How to get 100 pointsWhen we are ANDing two numbers, we would like to have a 1 at the most significant bit(MSB). So, first we'll try to get a 1 at MSB. Now, suppose we denote A[i]=b_i_{n},b_i_{n1}...b_i_{0}, where b_i's are the bits that could be 1 or 0. Let's say S be the set of A[i] whose b_i_{n} is 1 and S' be the set of A[i] whose b_i_{n} is 0. If size of S ≥ 2 we are sure that our answer will be maximum if we chose a pair of numbers from S, because n'th bit of their AND will be 1 for sure. So, we know our answer lies in S. However, if size of S is less than 2, we can never have n'th bit 1 in our answer. So, we'll have to continue with n'th bit as 0. Note that our answer will now be in S'. Now, we know our answer is in S or S' and we also know n'th bit of our answer. So, our new subproblem is to find (n1)'th bit of our answer using numbers in S or S'. We can write a recursive code for this. What will be the complexity? For each of the n bits, we'll traverse whole array to sort according to their bits. So O(n*N). We will be keeping n=30, because A[i] ≤ 10^{9}.
This question is marked "community wiki".
asked 27 Jul '14, 14:47

There must be some error in test cases as I did a simple sorting of numbers and took & of consecutive numbers and got an AC.But it's is wrong solution as ans for test case : 3 10 3 19 should be 3 but it will give 2. 19 = 10011 10 = 01010 3 = 00011 ANS = 3 but by sorting ANS = 2 And it's an AC code. So I think that the solutions should be rejudged. Link : http://www.codechef.com/viewsolution/4392454 answered 27 Jul '14, 15:12
@thecodekaiser even I used the same approach...but my program gives the answer of your test case as 3 only. Here is the code.
}
(30 Aug '17, 13:41)

I was bored to write a code on the formal algorithm I had developed i.e to check ranges of 2^n  where in you have to find the max range with at least two elements in that range and then compute AND operation on the closest two numbers. So instead, I checked the highest 5 elements and their AND operations with all elements and stored the max giving AC(100) with O(10*n). Not very efficient and accurate but can be helpful in terms of saving time in contests. answered 28 Jul '14, 08:31

I submitted this code in java.It gave NZEC.Can anybody tell why?
} answered 27 Jul '14, 15:02

How can we solve this problem using Trie ? I read the blog and understood the idea of finding 2 elements whose XOR is maximum using trie. But if we have to use that concept here then if a particular bit is zero in the number then either of the branch of the trie can yield optimal answer. So how to choose a specific branch ? answered 27 Jul '14, 16:05
1
I can't understand your code. :( , I can't still understand the idea. I mean let's say current bit is 0 , and this node has both left and right child. So why should we go to left ? I mean the right child also may produce the maximum ans can't it. Take for the example of inserting 0001,0111. Now if we try find and with 0011 now we go to left,left then what? I have got a 0 , and I could go both left and right. If we go left according to matching we get the max and is 1, but right has the answer 11.
(27 Jul '14, 18:00)
If the current bit of the number is 1 and current node also has an edge to 1 then moving to that edge is optimal. But if the current bit of the number is 0 then how to decide which edge we must take ? How is this optimal : "if the current node has the left/right son according to the current bit of the number you go in that son". if the current bit is 0 and current node has both left and right son : then how is choosing the son denoting 0 is optimal ?
(27 Jul '14, 18:04)
could anybody clear the doubt asked above ?
(27 Jul '14, 21:27)
@Editorialist , if you would clear the matter or tell us that it isn't possible it would be helpful. We are waiting here .
(27 Jul '14, 22:12)
yep..wht is reasoning behind this.pls do explain .
(28 Jul '14, 00:22)
If a particular bit is 0 then explore both the sub tree under it and choose the one which gives maximum value .
(29 Jul '14, 17:35)
showing 5 of 6
show all

my ans uses a different approach . I just sorted the array and took and of the numbers and maintained that a&b <=max(a,b); Is my approach correct? my solution is link http://www.codechef.com/viewsolution/4386414 answered 27 Jul '14, 20:28

my solution is easy one :)
answered 28 Jul '14, 22:20

push back every result into vector and sort.print the last one. it accepted. answered 28 Jul '14, 22:47

JUST STICK TO AND DEF.(it gives the minterm i.e. & of 2 number is smaller then small of 2 numbers, so........ ) #include<stdio.h> main(void) { int n,i,j,max=0; scanf("%d",&n); int a[n]; for(i=0;i<n;i++) scanf("%d",&a[i]);
answered 18 Dec '14, 00:15

what is the maths behind simply sorting the array and printing max of sum(by bitwise and) of consecutive elements. Why is there no need to test sum of nonconsecutive elements? answered 18 Dec '14, 11:43

Not so much to do! simply sort the numbers and run a loop to find the maximum if AND of consecutive numbers And that gives you the answer.Here is the code for it: include<bits stdc++.h="">int main() {ios_base::sync_with_stdio(false); int n; cin>>n; int a[n]; for(int i=0;i<n;i++) {="" cin="">>a[i]; } sort(a,a+n); int max=1; for(int i=0;i<n1;i++) {="" int="" t="a[i]&a[i+1];" if(t="">max) max=t; } cout<<max;
} It is 100 points submission..now the logic: Rather than running an O(n^2) algo you have many things to keep in mind. 1.Bigger numbers when ANDed will provide big results(generally). 2.A big and a small number will provide small results(most of the times) 3.So better not to find all nC2 pairs but compute from the consecutive pairs and get the solution. HOPE IT HELPS! answered 03 Jun '15, 11:37

So i used the logic, for every number calculate the position of set bit and for that bit keep the record of two max numbers, for eg: in 2, 8, 10, a record will be kept for 8 and 10 for bit position 2. Keep a record of the max bit position where more than two elements have set bit for that particular bit position. Now print (arr[maxn][0] & arrmaxn) where maxn is highest bit power and 0 and two are the highest elements for that particular bit. My solution passed for subtask 1 when i used set, but it doesnt pass anymore when i use array with the same logic. Please review my code answered 22 Aug '15, 18:43

answered 22 Dec '15, 17:47

the set of test cases for this question is not exhaustive, if u see my algorithm, u can get 100 points for the question but my algorithm fails in this test case  3 85 42 21 my algorithm  store all elements in array . sort the array. then traverse in reverse direction and compute the maximum of array[i],array[i1] the max will be the ans answered 16 Dec '17, 21:39

I am getting RE(SIGSEGV) in the code  I haven't allocated a very large amount of memory and I cannot detect any illegal memory reference made by me. I have used an iterative solution of the bitrepresentation logic. I would be thankful for any help. answered 26 Jan '18, 00:17

nice solution learned a lot about how to play with bits thnx man