-
Notifications
You must be signed in to change notification settings - Fork 1
/
subarraywithxorK.java
76 lines (57 loc) · 1.77 KB
/
subarraywithxorK.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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
package aryanhere.Striver;
import java.util.HashMap;
import java.util.Map;
public class subarraywithxorK {
public static int subarraywithXorrk(int []arr, int d){
int n = arr.length;
int count = 0;
for(int i = 0 ; i < n ; i++){
for(int j = i + 1; j < n; j++){
int xorr = 0;
for(int k = i ; k < j; k++){
xorr = xorr ^ arr[k];
}
if(xorr == d) count++;
}
}
return count;
}
public static int subarraywithxorK1(int[] arr , int d){
int n = arr.length;
int count = 0;
for(int i = 0; i < n; i++){
int xorr = 0;
for(int j = i; j < n ; j++){
xorr = xorr ^ arr[j];
}
if(xorr == d) count++;
}
return count;
}
public static int subarraywithxorK2(int arr[] , int d){
int n = arr.length;
int xr = 0;
HashMap<Integer , Integer > map = new HashMap<>(n);
map.put(xr,1);
int count = 0;
for(int i = 0 ; i < n; i++){
xr = xr ^ arr[i];
int x = xr ^ d;
if(map.containsKey(x)){
count += map.get(x);
}
if(map.containsKey(xr)){
map.put(xr,map.get(xr) + 1);
}else{
map.put(xr,1);
}
}
return count;
}
public static void main(String[]args){
int arr[] = {4 ,5 ,6 , 2 , 2 , 4};
int d = 2;
int ans = subarraywithXorrk(arr, d);
System.out.println("THe Subarray with xorr k = " + ans);
}
}