-
Notifications
You must be signed in to change notification settings - Fork 1
/
numberofsubaarray.java
68 lines (58 loc) · 1.73 KB
/
numberofsubaarray.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
package aryanhere.Striver;
import java.util.HashMap;
public class numberofsubaarray {
public static int subarraywithsumK(int arr[] , int d){
int n = arr.length;
int count = 0;
for(int i = 0 ; i < n ; i++){
for(int j = i ; 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 subarraywithsumK2(int arr[] ,int d){
int n = arr.length;
int count = 0;
//traversing into the array
for(int i = 0 ; i < n; i++){
int xorr = 0;
// to avoid the extra complexity using only two loops..!
for(int j = i ; j < n ; j++){
xorr = xorr ^ arr[j];
}
if(xorr == d) count++;
}
return count;
}
public static int subarraywithsumK3(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 xor = xr ^ d;
if(map.containsKey(xor)){
count += map.get(xor);
}
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 , 3 , 2 , 1};
int d = 4;
int ans = subarraywithsumK3(arr, d);
System.out.println(" the number of subarrays are " + ans);
}
}