-
Notifications
You must be signed in to change notification settings - Fork 110
/
solution.java
51 lines (41 loc) · 1.74 KB
/
solution.java
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
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
import java.io.*;
import java.math.*;
import java.security.*;
import java.text.*;
import java.util.*;
import java.util.concurrent.*;
import java.util.regex.*;
public class Solution {
/* To maximize A xor B, we want A and B to differ as much as possible at every bit index.
We first find the most significant bit that we can force to differ by looking at L and R.
For all of the lesser significant bits in A and B, we can always ensure that they differ
and still have L <= A <= B <= R. Our final answer will be the number represented by all
1s starting from the most significant bit that differs between A and B
Example:
L = 10111
R = 11100
_X___ <-- that's most significant differing bit
01111 <-- here's our final answer
Notice that we never directly calculate the values of A and B
**/
static int maximizingXor(int L, int R) {
int xored = L ^ R;
int significantBit = 31 - Integer.numberOfLeadingZeros(xored);
int result = (1 << (significantBit + 1)) - 1;
return result;
}
/* Driver Code */
private static final Scanner scanner = new Scanner(System.in);
public static void main(String[] args) throws IOException {
BufferedWriter bufferedWriter = new BufferedWriter(new FileWriter(System.getenv("OUTPUT_PATH")));
int l = scanner.nextInt();
scanner.skip("(\r\n|[\n\r\u2028\u2029\u0085])?");
int r = scanner.nextInt();
scanner.skip("(\r\n|[\n\r\u2028\u2029\u0085])?");
int result = maximizingXor(l, r);
bufferedWriter.write(String.valueOf(result));
bufferedWriter.newLine();
bufferedWriter.close();
scanner.close();
}
}