# Maximize the number of subarrays with XOR as zero

Given an array of N numbers. The task is to maximize the number of subarrays with XOR value zero by swapping the bits of array element of any given subarray any number of times.

**Note:** 1<=A[i]<=10^{18}

**Examples:**

Input:a[] = {6, 7, 14}

Output :2

2 subarrays are {7, 14} and {6, 7 and 14} by swapping their bits individually in subarrays.

Subarray {7, 14} is valid as 7 is changed to 11(111 to 1011) and 14 is changed to 11, hence the subarray is {11, 11} now. Subarray {6, 7, 14} is valid as 6 is changed to 3 and 7 to 13 and 14 is unchanged, so 3^13^14 = 0.

Input:a[] = {1, 1}

Output :

**Approach: **The first observation is that only even number of set bits at any given index can lead to XOR value 0. Since the maximum size of the array element can be of the order 10^{18}, we can assume 60 bits for swapping. The following steps can be followed to solve the above problem:

- Since only the number of set bits are required, count the number of set bits in every i-th element.
- There are two conditions which need to be satisfied simultaneously in order to make the XOR of any subarray as zero by swapping bits. The conditions are as follows:
- If there are even number of set bits in any range L-R, then we can try making the XOR of subarray 0.
- If the sum of the bits is less than or equal to twice of the largest number of set bits in any given range, then it is possible to make its XOR zero.

The mathematical work that needs to be done in L-R range for every subarray has been explained above.

A **naive solution** will be to iterate for every subarray and check both the conditions explicitly and count the number of such subarrays. But the time complexity in doing so will be **O(N^2).**

An **efficient solution** will be to follow the below mentioned steps:

- Use
**prefix[]**sum array to compute the number of subarrays which obey the first condition only. - The
**step-1**gives us the number of subarrays which as sum of bits as even in**O(N)**complexity. -
Using inclusion-exclusion principle, we can explicitly subtract the number of subarrys whose
**2*largest number**of set bits in subarray exceeds sum of set bits in subarray. - Using maths, we can reduce the number of subarray checking in the Step-3, since the number of set bits can be a minimum of 1, we can just check for every subarray of
**length 60**, since beyond that length, the second condition can never be falsified. - Once done we can subtract the number from the number of subarrays obtained in
**Step-1**to get our answer.

Below is the implementation of the above approach:

## C++

`#include <bits/stdc++.h> ` `using` `namespace` `std; ` ` ` `// Function to count subarrays not satisfying condition 2 ` `int` `exclude(` `int` `a[], ` `int` `n) ` `{ ` ` ` `int` `count = 0; ` ` ` `// iterate in the array ` ` ` `for` `(` `int` `i = 0; i < n; i++) { ` ` ` ` ` `// store the sum of set bits ` ` ` `// in the subarray ` ` ` `int` `s = 0; ` ` ` `int` `maximum = 0; ` ` ` ` ` `// iterate for range of 60 subarrays ` ` ` `for` `(` `int` `j = i; j < min(n, i + 60); j++) { ` ` ` `s += a[j]; ` ` ` `maximum = max(a[j], maximum); ` ` ` ` ` `// check if falsifies the condition-2 ` ` ` `if` `(s % 2 == 0 && 2 * maximum > s) ` ` ` `count++; ` ` ` `} ` ` ` `} ` ` ` ` ` `return` `count; ` `} ` ` ` `// Function to count subarrays ` `int` `countSubarrays(` `int` `a[], ` `int` `n) ` `{ ` ` ` ` ` `// replace the array element by number ` ` ` `// of set bits in them ` ` ` `for` `(` `int` `i = 0; i < n; i++) ` ` ` `a[i] = __builtin_popcountll(a[i]); ` ` ` ` ` `// calculate prefix array ` ` ` `int` `pre[n]; ` ` ` `for` `(` `int` `i = 0; i < n; i++) { ` ` ` `pre[i] = a[i]; ` ` ` `if` `(i != 0) ` ` ` ` ` `pre[i] += pre[i - 1]; ` ` ` `} ` ` ` ` ` `// Count the number of subarays ` ` ` `// satisfying step-1 ` ` ` `int` `odd = 0, even = 0; ` ` ` ` ` `// count number of odd and even ` ` ` `for` `(` `int` `i = 0; i < n; i++) { ` ` ` `if` `(pre[i] & 1) ` ` ` `odd++; ` ` ` `} ` ` ` `even = n - odd; ` ` ` ` ` `// Increase even by 1 for 1, so that the ` ` ` `// subarrys starting from the index-0 ` ` ` `// are also taken to count ` ` ` `even++; ` ` ` ` ` `// total subarrays satsfying condition-1 only ` ` ` `int` `answer = (odd * (odd - 1) / 2) + (even * (even - 1) / 2); ` ` ` ` ` `cout << answer << endl; ` ` ` ` ` `// exclude total subarrays not satsfying condition2 ` ` ` `answer = answer - exclude(a, n); ` ` ` ` ` `return` `answer; ` `} ` ` ` `// Driver Code ` `int` `main() ` `{ ` ` ` ` ` `int` `a[] = { 6, 7, 14 }; ` ` ` `int` `n = ` `sizeof` `(a) / ` `sizeof` `(a[0]); ` ` ` ` ` `cout << countSubarrays(a, n); ` ` ` ` ` `return` `0; ` `} ` |

*chevron_right*

*filter_none*

## Python3

# Python3 code for the given approach.

# Function to count subarrays not

# satisfying condition 2

def exclude(a, n):

count = 0

# iterate in the array

for i in range(0, n):

# store the sum of set bits

# in the subarray

s = 0

maximum = 0

# iterate for range of 60 subarrays

for j in range(i, min(n, i + 60)):

s += a[j]

maximum = max(a[j], maximum)

# check if falsifies the condition-2

if s % 2 == 0 and 2 * maximum > s:

count += 1

return count

# Function to count subarrays

def countSubarrays(a, n):

# replace the array element by

# number of set bits in them

for i in range(0, n):

a[i] = bin(a[i]).count(‘1’)

# calculate prefix array

pre = [None] * n

for i in range(0, n):

pre[i] = a[i]

if i != 0:

pre[i] += pre[i – 1]

# Count the number of subarays

# satisfying step-1

odd, even = 0, 0

# count number of odd and even

for i in range(0, n):

if pre[i] & 1:

odd += 1

even = n – odd

# Increase even by 1 for 1, so that the

# subarrys starting from the index-0

# are also taken to count

even += 1

# total subarrays satsfying condition-1 only

answer = ((odd * (odd – 1) // 2) +

(even * (even – 1) // 2))

print(answer)

# exclude total subarrays not

# satsfying condition2

answer = answer – exclude(a, n)

return answer

# Driver Code

if __name__ == “__main__”:

a = [6, 7, 14]

n = len(a)

print(countSubarrays(a, n))

# This code is contributed by Rituraj Jain

**Output:**

3 2

**Time Complexity:** O(N*60)

## Recommended Posts:

- Differences between number of increasing subarrays and decreasing subarrays in k sized windows
- Maximize the number of sum pairs which are divisible by K
- Maximize the number of segments of length p, q and r
- Maximize the product of four factors of a Number
- Maximize number of 0s by flipping a subarray
- Number of subarrays with odd sum
- Number of subarrays having sum less than K
- Maximize array elements upto given number
- Number of subarrays with m odd numbers
- Number of subarrays having sum in a given range
- Number of subarrays having sum exactly equal to k
- Number of subarrays having sum of the form k^m, m >= 0
- Number of subarrays having product less than K
- Find number of subarrays with even sum
- Number of subarrays with given product

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.