Help me in solving XORRY1 problem

My issue

how to gerate a and b , to turn off leftmost set bit , and turn 0f rmaining bit

My code

#include<bits/stdc++.h>
using namespace std;
int main () {
    int t;
    cin>>t;
    while (t--){
    		   int x;
    		   cin>>x;
    		    int diff= INT_MAX;
    		    vector<int> ans(2,0);
    		   for (int i=0,j=x;i <= j;i++,j--){
    		   		int k = i^j;
    		   		if (k==x) {
    		   			int k= j-i;
    		   	      if (diff>k) {
    		   	      	ans[0]= i;
    		   	      	ans[1]= j;
    		   	      	diff= k;
    		   	      }
    		   		}
    		   }

    		   cout<<ans[0]<<" "<<ans[1]<<endl;

    }
	return 0;  
}

Problem Link: Xorry 1 Practice Coding Problem - CodeChef

@abhishekfth
here , refer my c++ code

#include <bits/stdc++.h>
using namespace std;

int main() {
	// your code goes here
        int t;
        cin>>t;
        while(t--)
        {
            long long int n,hi;
            cin>>n;
            long long int p=1;
            while(p<=n)
            {
                long long int val=p&n;
                if(val==p)
                {
                    hi=p;
                }
                p=p*2;
            }
            long long int ans=n^hi;
            cout<<ans<<" "<<hi<<endl;
        }
}
1 Like