-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathDay69.java
More file actions
25 lines (23 loc) · 788 Bytes
/
Day69.java
File metadata and controls
25 lines (23 loc) · 788 Bytes
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
import java.util.HashMap;
import java.util.Map;
public class Day69 {
public int countSubarrays(int[] A, int B) {
int count = 0;
int xor = 0;
Map<Integer, Integer> prefixXOR = new HashMap<>();
prefixXOR.put(0, 1);
for (int num : A) {
xor ^= num;
int target = xor ^ B;
count += prefixXOR.getOrDefault(target, 0);
prefixXOR.put(xor, prefixXOR.getOrDefault(xor, 0) + 1);
}
return count;
}
public static void main(String[] args) {
SubarraysWithXOR obj = new SubarraysWithXOR();
int[] A = {1, 2, 3, 2};
int B = 2;
System.out.println("Number of subarrays with XOR equal to " + B + ": " + obj.countSubarrays(A, B)); // Output: 3
}
}