Video Approach:
Read problem statements.
The MEX (minimum excluded) of an array is the smallest non-negative integer that does not belong to the array. For instance:
- The MEX of is , because does not belong to the array.
- The MEX of is , because and belong to the array, but does not.
- The MEX of is 4 because and belong to the array, but does not.
Find the maximum possible MEX of an array of non-negative integers such that the bitwise OR of the elements in the array does not exceed .
Input Format
- The first line contains denoting the number of test cases. Then the test cases follow.
- Each test case contains a single integer on a single line.
Output Format
For each test case, output on a single line the maximum possible MEX of the array satisfying the given property.
Constraints
Subtasks
Subtask 1 (100 points): Original constraints
Sample Input 1
4
0
1
2
5
Sample Output 1
1
2
2
4
Explanation
Test case : The array could be .
Test case : The array could be . Here the bitwise OR of and is and the MEX of the array is as both and belong to the array.
Test case : The array could be . Here the bitwise OR of all integers in the array is and the MEX of the array is .
MEX-OR Solution:
#include <iostream>
using namespace std;
int main() {
// your code goes here
int t;
cin>>t;
while(t--){
int x, ans = 1;
cin>>x;
if(x == 0){
cout<<1<<endl;
}
else if(x == 1 || x == 2){
cout<<2<<endl;
}
else{
while(ans*2 <= x){
ans*=2;
}
if(ans == x){
cout<<x<<endl;
}
else if(x ==(2*ans-1)){
cout<<x+1<<endl;
}
else{
cout<<ans<<endl;
}
}
}
return 0;
}
0 Comments