-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathFindXORInRange.java
62 lines (47 loc) · 1.29 KB
/
FindXORInRange.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
52
53
54
55
56
57
58
59
60
61
62
package bit_manipulation.easy;
/***
* Problem in GeeksForGeeks: https://practice.geeksforgeeks.org/problems/find-xor-of-numbers-from-l-to-r/1
*
* You are given two integers L and R, your task is to find the XOR of elements of the range [L, R].
*
* Example 1:
* Input: L = 4, R = 8
* Output: 8
*
* Example 2:
* Input: L = 3, R = 7
* Output: 3
*/
public class FindXORInRange {
public static void main(String[] args) {
int l = 4, r = 8;
System.out.println("Brute Force: " + findXORInRangeBruteForce(l, r));
System.out.println("Mod 4 method: " + findXORInRangeMod4(l, r));
}
private static int findXORInRangeBruteForce(int l, int r) {
int xor = 0;
for (int i = l; i <= r; i++) {
xor ^= i;
}
return xor;
}
private static int findXORInRangeMod4(int l, int r) {
int xorOfTotal = findXORInRangeOfN(r);
int xorTillL = findXORInRangeOfN(l - 1);
int actualXOR = xorTillL ^ xorOfTotal;
return actualXOR;
}
private static int findXORInRangeOfN(int n) {
int mod = n % 4;
if (mod == 0) {
return n;
}
if (mod == 1) {
return 1;
}
if (mod == 2) {
return n + 1;
}
return 0;
}
}