There is an array ‘A’ of length ‘N’. You can perform the following operation on the array, select ‘i’, ‘1< i <= N - 1’ and replace ‘A[i]’ with ‘A[i] | A[i - 2]’, where ‘|’ represents bitwise OR operation.
Your task is to find the maximum element ‘X’ you can obtain by operating zero or more times.
Example:
‘A’ = [1, 3, 2, 5]
A[3] = A[3] or A[1] = 3 | 5 = 7
‘A’ = [1, 3, 2, 7]
Hence the maximum element we can obtain is 7.
Therefore, the answer is 7.
First-line contains 'T,' denoting the number of Test cases.
For each Test case:
The first line contains an integer ‘N’ denoting the number of students.
The second line contains ‘N’ elements of array ‘A’.
Output format :
Return the maximum element ‘X’ you can obtain by operating zero or more times.
Note:-
You don't need to print anything. Just implement the given function.
1 <= 'T' <= 10
1 <= 'N' <= 10^5
1 <= 'A[i]' <= 2^30
Time Limit: 1 sec
2
6
1 3 4 2 4 5
2
1 2
7
2
For test case 1:
A = [1, 3, 4, 2, 4, 5]
A[3] = A[3] or A[1] = 2 | 3 = 3
A = [1, 3, 4, 3, 4, 5]
A[5] = A[5] or A[3] = 5 | 3 = 7
A = [1, 3, 4, 3, 4, 7]
Hence the maximum element we can obtain is 7.
Therefore, the answer is 7.
For test case 2:
A = [1, 2]
No operations can be performed on the array.
Hence the maximum element we can obtain is 2.
Therefore, the answer is 2.
2
6
1 3 7 11 4 5
3
1 2 3
15
3
Handle the odd and the even indices separately.
Approach:-
Algorithm:-
O(N), where 'N' is the length of the array ‘A’.
We are iterating the array ‘A’ of length ‘N’ once.
Hence, overall time complexity is O(N).
O(1).
We use two variables to store OR of odd and even indices, which take constant space. So the space complexity is O(1).