MAX AND

3.7

19 votes
Basic Programming, Bit Manipulation, Basics of Bit Manipulation
Problem

You are given array AB each containing N inetegers. You have to create array C of N inetegers where Ci=Ai&Bi (Here, & denotes the Bitwise AND operation). You can shuffle elements of  AB  however you want. Find maximum value of C1&C2&&CN.

 Input format

  • The first line contains T denoting the number of test cases. The description of T test cases is as follows:
  • For each test case:
    • The first line contains an integer N denotes size of array AB.
    • The next line contains N space-separated integers A1,A2,,AN - denoting the elements of A.
    • The next line contains N space-separated integers B1,B2,,BN - denoting the elements of B.

Output format

For each test case, print maximum value of C1&C2&&CN in a separate line.

Constraints

1T1051N1050Ai,Bi109

The sum of  N over all test cases does not exceed 105.

 

Time Limit: 1
Memory Limit: 256
Source Limit:
Explanation

The best configuration here will be have A1=3, and B1=3, and A2=2 and B2=2. So the array C will be [3,2]. So the answer will be 2 (3&2=2).

Editor Image

?