forked from BHAVYAghub/assign
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathx.cpp
64 lines (51 loc) · 1.59 KB
/
x.cpp
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
// C++ Program to count all subarrays having
// XOR of elements as given value m with
// O(n) time complexity.
#include <bits/stdc++.h>
using namespace std;
// Returns count of subarrays of arr with XOR
// value equals to m
long long subarrayXor(int arr[], int n, int m)
{
long long ans = 0; // Initialize answer to be returned
// Create a prefix xor-sum array such that
// xorArr[i] has value equal to XOR
// of all elements in arr[0 ..... i]
int* xorArr = new int[n];
// Create map that stores number of prefix array
// elements corresponding to a XOR value
unordered_map<int, int> mp;
// Initialize first element of prefix array
xorArr[0] = arr[0];
// Computing the prefix array.
for (int i = 1; i < n; i++)
xorArr[i] = xorArr[i - 1] ^ arr[i];
// Calculate the answer
for (int i = 0; i < n; i++) {
// Find XOR of current prefix with m.
int tmp = m ^ xorArr[i];
// If above XOR exists in map, then there
// is another previous prefix with same
// XOR, i.e., there is a subarray ending
// at i with XOR equal to m.
ans = ans + ((long long)mp[tmp]);
// If this subarray has XOR equal to m itself.
if (xorArr[i] == m)
ans++;
// Add the XOR of this subarray to the map
mp[xorArr[i]]++;
}
// Return total count of subarrays having XOR of
// elements as given value m
return ans;
}
// Driver program to test above function
int main()
{
int arr[] = { 4, 2, 2, 6, 4 };
int n = sizeof(arr) / sizeof(arr[0]);
int m = 6;
cout << "Number of subarrays having given XOR is "
<< subarrayXor(arr, n, m);
return 0;
}