Maximize the number of subarrays with XOR as zero
Last Updated :
06 Sep, 2022
Given an array of N numbers. The task is to maximize the number of subarrays with XOR value zero by swapping the bits of an array element of any given subarray any number of times.
Note: 1<=A[i]<=1018
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 an even number of set bits at any given index can lead to XOR value 0. Since the maximum size of the array elements can be of the order 1018, 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 is required, count the number of set bits in every i-th element.
- There are two conditions that need to be satisfied simultaneously in order to make the XOR of any subarray zero by swapping bits. The conditions are as follows:
- If there are even a 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 that obey the first condition only.
- Step-1 gives us the number of subarrays which is a sum of bits as even in O(N) complexity.
- Using inclusion-exclusion principle, we can explicitly subtract the number of subarrays whose 2*largest number of set bits in subarray exceeds the sum of set bits in the subarray.
- Using maths, we can reduce the number of subarray checking in 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;
int exclude( int a[], int n)
{
int count = 0;
for ( int i = 0; i < n; i++) {
int s = 0;
int maximum = 0;
for ( int j = i; j < min(n, i + 60); j++) {
s += a[j];
maximum = max(a[j], maximum);
if (s % 2 == 0 && 2 * maximum > s)
count++;
}
}
return count;
}
int countSubarrays( int a[], int n)
{
for ( int i = 0; i < n; i++)
a[i] = __builtin_popcountll(a[i]);
int pre[n];
for ( int i = 0; i < n; i++) {
pre[i] = a[i];
if (i != 0)
pre[i] += pre[i - 1];
}
int odd = 0, even = 0;
for ( int i = 0; i < n; i++) {
if (pre[i] & 1)
odd++;
}
even = n - odd;
even++;
int answer = (odd * (odd - 1) / 2) + (even * (even - 1) / 2);
cout << answer << endl;
answer = answer - exclude(a, n);
return answer;
}
int main()
{
int a[] = { 6, 7, 14 };
int n = sizeof (a) / sizeof (a[0]);
cout << countSubarrays(a, n);
return 0;
}
|
Java
import java.util.*;
class GFG
{
static int exclude( int a[], int n)
{
int count = 0 ;
for ( int i = 0 ; i < n; i++)
{
int s = 0 ;
int maximum = 0 ;
for ( int j = i; j < Math.min(n, i + 60 ); j++)
{
s += a[j];
maximum = Math.max(a[j], maximum);
if (s % 2 == 0 && 2 * maximum > s)
count++;
}
}
return count;
}
static int countSubarrays( int a[], int n)
{
for ( int i = 0 ; i < n; i++)
a[i] = Integer.bitCount(a[i]);
int []pre = new int [n];
for ( int i = 0 ; i < n; i++)
{
pre[i] = a[i];
if (i != 0 )
pre[i] += pre[i - 1 ];
}
int odd = 0 , even = 0 ;
for ( int i = 0 ; i < n; i++)
{
if (pre[i]% 2 == 1 )
odd++;
}
even = n - odd;
even++;
int answer = (odd * (odd - 1 ) / 2 ) + (even * (even - 1 ) / 2 );
System.out.println(answer);
answer = answer - exclude(a, n);
return answer;
}
public static void main(String[] args)
{
int a[] = { 6 , 7 , 14 };
int n = a.length;
System.out.println(countSubarrays(a, n));
}
}
|
Python3
def exclude(a, n):
count = 0
for i in range ( 0 , n):
s = 0
maximum = 0
for j in range (i, min (n, i + 60 )):
s + = a[j]
maximum = max (a[j], maximum)
if s % 2 = = 0 and 2 * maximum > s:
count + = 1
return count
def countSubarrays(a, n):
for i in range ( 0 , n):
a[i] = bin (a[i]).count( '1' )
pre = [ None ] * n
for i in range ( 0 , n):
pre[i] = a[i]
if i ! = 0 :
pre[i] + = pre[i - 1 ]
odd, even = 0 , 0
for i in range ( 0 , n):
if pre[i] & 1 :
odd + = 1
even = n - odd
even + = 1
answer = ((odd * (odd - 1 ) / / 2 ) +
(even * (even - 1 ) / / 2 ))
print (answer)
answer = answer - exclude(a, n)
return answer
if __name__ = = "__main__" :
a = [ 6 , 7 , 14 ]
n = len (a)
print (countSubarrays(a, n))
|
C#
using System;
class GFG
{
static int exclude( int []a, int n)
{
int count = 0;
for ( int i = 0; i < n; i++)
{
int s = 0;
int maximum = 0;
for ( int j = i; j < Math.Min(n, i + 60); j++)
{
s += a[j];
maximum = Math.Max(a[j], maximum);
if (s % 2 == 0 && 2 * maximum > s)
count++;
}
}
return count;
}
static int countSubarrays( int []a, int n)
{
for ( int i = 0; i < n; i++)
a[i] = bitCount(a[i]);
int []pre = new int [n];
for ( int i = 0; i < n; i++)
{
pre[i] = a[i];
if (i != 0)
pre[i] += pre[i - 1];
}
int odd = 0, even = 0;
for ( int i = 0; i < n; i++)
{
if (pre[i]%2== 1)
odd++;
}
even = n - odd;
even++;
int answer = (odd * (odd - 1) / 2) + (even * (even - 1) / 2);
Console.WriteLine(answer);
answer = answer - exclude(a, n);
return answer;
}
static int bitCount( long x)
{
int setBits = 0;
while (x != 0) {
x = x & (x - 1);
setBits++;
}
return setBits;
}
public static void Main(String[] args)
{
int []a = { 6, 7, 14 };
int n = a.Length;
Console.WriteLine(countSubarrays(a, n));
}
}
|
Javascript
<script>
function exclude(a, n)
{
let count = 0;
for (let i = 0; i < n; i++)
{
let s = 0;
let maximum = 0;
for (let j = i; j < Math.min(n, i + 60); j++)
{
s += a[j];
maximum = Math.max(a[j], maximum);
if (s % 2 == 0 && 2 * maximum > s)
count++;
}
}
return count;
}
function countSubarrays(a, n)
{
for (let i = 0; i < n; i++)
a[i] = bitCount(a[i]);
let pre = new Array(n);
for (let i = 0; i < n; i++) {
pre[i] = a[i];
if (i != 0)
pre[i] += pre[i - 1];
}
let odd = 0, even = 0;
for (let i = 0; i < n; i++) {
if (pre[i] & 1)
odd++;
}
even = n - odd;
even++;
let answer = parseInt((odd * (odd - 1) / 2) +
(even * (even - 1) / 2));
document.write(answer + "<br>" );
answer = answer - exclude(a, n);
return answer;
}
function bitCount(x)
{
let setBits = 0;
while (x != 0) {
x = x & (x - 1);
setBits++;
}
return setBits;
}
let a = [ 6, 7, 14 ];
let n = a.length;
document.write(countSubarrays(a, n));
</script>
|
Complexity Analysis:
- Time Complexity: O(N*60), as we are using a loop to traverse N*60 times.
- Auxiliary Space: O(N), as we are using extra space for pre array.
Share your thoughts in the comments
Please Login to comment...