Sunday, 5 October 2014

Maximizing XOR

#include <iostream>
#include <algorithm>
using namespace std;
int maxXor(int l, int r) 
{
    int i,j,m,ans=0;
    for(i=l;i<r;i++)
        {
        for(j=i+1;j<=r;j++)
            {
            m=i^j;
            if(ans<=m)
                ans=m;
        }
    }
return ans;
}

int main() 
{
    int res;
    int _l;
    cin >> _l;
    int _r;
    cin >> _r;
    res = maxXor(_l, _r);
    cout << res;
    return 0;
}

1 comment:

  1. The solution for each pair of (l,r) takes O(n^2) time. Can you please post a solution with a lower time complexity ? Thanks in advance

    ReplyDelete

Chocolate Feast Solution Hackerrank

#include<iostream> using namespace std; int main() {     int t;     cin>>t;     while(t--)     {         int n,c,m,w,tot...