-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathmax_xor_in_range.cpp
More file actions
33 lines (31 loc) · 1.05 KB
/
max_xor_in_range.cpp
File metadata and controls
33 lines (31 loc) · 1.05 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
// https://www.hackerrank.com/contests/codeagon/challenges/xor-queries/problem
#include<iostream>
using namespace std;
int main()
{
int t;
cin>>t;
while(t--)
{
int x,l,r;
cin>>x>>l>>r;
int xorval=0; //Number in range l to r with max XOR value
int xormax=x; //Max XOR value
int rem=l; //Make xorval be atleast l
for(int j=1<<30;j;j=j>>1)
{
int temp=xormax^j;
if(temp>xormax || rem-j>=0)
{
if((xorval^j)<=r)
{
rem-=j;
xorval=xorval^j;
xormax=xormax^j;
}
}
}
cout<<xormax<<endl;
}
}
// You are given a task to handle queries. In each query, you will be given three positive integers . You have to choose a number that lies in the interval [] such that is maximum, where denotes the bitwise xor operation. Can you find the maximum value of ?