Given two integers: L and R, the lower & upper limits respectively, you have to find the maximum XOR value for the pair of numbers A & B where, L ≤ A ≤ B ≤ R
For Example:
If L=10 & R=15, then the possible pairs will be
10⊕10=0
10⊕11=1
10⊕12=6
10⊕13=7
10⊕14=4
10⊕15=5
11⊕11=0
11⊕12=7
11⊕13=6
11⊕14=5
11⊕15=4
12⊕12=0
12⊕13=1
12⊕14=2
12⊕15=3
13⊕13=0
13⊕14=3
13⊕15=2
14⊕14=0
14⊕15=1
15⊕15=0
Here two pairs (10,13) and (11,12) have maximum XOR value 7. So, output is '7'
Implementation:
First line - L, lower limit
Second line - R, upper limit
Output
The maximum XOR value in the range.
Sample Input
1 10
Sample Output
15
For Example:
If L=10 & R=15, then the possible pairs will be
10⊕10=0
10⊕11=1
10⊕12=6
10⊕13=7
10⊕14=4
10⊕15=5
11⊕11=0
11⊕12=7
11⊕13=6
11⊕14=5
11⊕15=4
12⊕12=0
12⊕13=1
12⊕14=2
12⊕15=3
13⊕13=0
13⊕14=3
13⊕15=2
14⊕14=0
14⊕15=1
15⊕15=0
Here two pairs (10,13) and (11,12) have maximum XOR value 7. So, output is '7'
Implementation:
#include<stdio.h> int main() { int L,R,max=0,i,j,xr; scanf("%d",&L); scanf("%d",&R); for(i=L;i<=R;i++) { for(j=i+1;j<=R;j++) { xr=i^j; if(xr>max) max=xr; } } printf("%d\n",max); return 0; }
Input
First line - L, lower limit
Second line - R, upper limit
Output
The maximum XOR value in the range.
Sample Input
1 10
Sample Output
15