[Solved] Longest AND Subarray codechef solution

October Challenge 2021 Division 3 (Rated) - CodeChef

Longest AND Subarray codechef solution

You are given an integer NN. Consider the sequence containing the integers 1,2,,N1,2,…,N in increasing order (each exactly once). Find the length of the longest subarray in this sequence such that the bitwise AND of all elements in the subarray is positive.

Longest AND Subarray codechef solution

Input Format

  • The first line contains TT denoting the number of test cases. Then the test cases follow.
  • Each test case contains a single integer NN on a single line.

Longest AND Subarray codechef solution

Output Format

For each test case, output on a single line the length of the longest subarray that satisfy the given property.

Longest AND Subarray codechef solution

Constraints

  • 1T1051≤T≤105
  • 1N1091≤N≤109

Longest AND Subarray codechef solution

Subtasks

  • Subtask 1 (100 points): Original constraints

Sample Input 1 

5
1
2
3
4
7

Sample Output 1 

1
1
2
2
4

Explanation

Test case 11: The only possible subarray we can choose is [1][1].

Test case 22: We can’t take the entire sequence [1,2][1,2] as a subarray because the bitwise AND of 11 and 22 is zero. We can take either [1][1] or [2][2] as a subarray.

Test case 44: It is optimal to take the subarray [2,3][2,3] and the bitwise AND of 22 and 33 is 22.

Test case 55: It is optimal to take the subarray [4,5,6,7][4,5,6,7] and the bitwise AND of all integers in this subarray is 44.


Updating Soon…


Longest AND Subarray codechef solution

Counting Tuples codechef solution

 

Leave a Comment