I am unable to understand why the my solution is wrong (failing on test case 2) for the this question: 2072C - Creating Keys for StORages Has Become My Main Skill Can someone please explain what am i missing here?
include <bits/stdc++.h>
using namespace std;
define dbg(v) \
cerr << "Line(" << __LINE__ << ") -> " << #v << " = " << (v) << endl;
define int long long
void scanarr(int arr[], int n) { for(int i = 0; i < n; i++) cin >> arr[i]; }
int order(int x){
int ans = 0; while(x!=0){ if(x/2 != 0) ans++; x /= 2; } return ans; }
int32_t main(){ ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
int t; cin>>t; while(t--){ int n,x; cin>>n>>x; int p=0, x1=x; while(x1!=0){ if(x1%2 == 0) break; p++; x1 /= 2; }//cout<<p<<'\n'; int a[31]; a[0] = 1; for(int i=1; i<31; i++){ a[i] = a[i-1]*2; } int mex = a[p]; if(mex >= n){ for(int i=0; i<n-1; i++){ cout<<i<<" "; } if(order(n-1) == order(x)) cout<<n-1<<'\n'; else cout<<x<<'\n'; }else{ for(int i=0; i<n-1; i++){ if(i>= mex){ cout<<0<<" "; }else { cout<<i<<" "; } }cout<<x<<'\n'; } } return 0;
}
Simply you have to count consecutive 1's bits of 'x' from left, once you get 0 break it. (assume we store it in cnt) Then run a for loop with condition
(i < (1 << cnt)) && (i < n)
and storeans[i] = i;
then check if all OR operation on ans elements is equal to 'x'. if not then place x to last element of ans.