Bitwise swaps solution codechef

Bitwise swaps solution codechef

 

SOLUTION:- CLICK HERE

 

Given an array AA consisting of NN integers A1,A2,,ANA1,A2,…,AN, determine if you can sort this array by applying the following operation several times (possibly, zero):

  • Pick a pair of indices (i,j)(i,j) with iji≠j and Ai&Aj0Ai&Aj≠0, and swap the values of AiAi and AjAj. Here, && denotes the bitwise AND operation.

For example, if A=[6,4,2]A=[6,4,2], the two possible operations are (1,2)(1,2) and (1,3)(1,3)(2,3)(2,3) cannot be performed because A2&A3=4&2=0A2&A3=4&2=0.

Input Format

  • The first line of input contains a single integer TT, denoting the number of test cases. The description of TT test cases follows.
  • The first line of each test case contains a single integer NN.
  • The second line contains NN space-separated integers A1,A2,,ANA1,A2,…,AN

Output Format

For each test case, output the answer on a new line — YES if the given array can be sorted by repeatedly applying the given operation, and NO otherwise.

You may print each character of the answer string in either uppercase or lowercase (for example, the strings "yEs""yes""Yes" and "YES" will all be treated as identical).

Constraints

  • 1T1041≤T≤104
  • 1N31051≤N≤3⋅105
  • 0Ai<2310≤Ai<231 for each 1iN1≤i≤N
  • The sum of NN over all test cases does not exceed 31053⋅105

Subtasks

Subtask #1 (100 points): Original constraints

Sample Input 1 

4
3
6 4 2
6
9 34 4 24 1 6
6
9 34 24 4 1 6
2
1 0

Sample Output 1 

Yes
Yes
No
No

Explanation

Test case 11: AA can be sorted by applying the single operation (1,3)(1,3).

Test case 22: AA can be sorted by applying the following operations in order: (1,5),(2,6),(2,3),(4,5)(1,5),(2,6),(2,3),(4,5).

Test cases 33 and 44: It can be shown that no sequence of operations will sort AA.

SOLUTION:- CLICK HERE

Leave a Comment

Your email address will not be published.