Longest Subarray having strictly positive XOR
Last Updated :
07 Dec, 2022
Given an array arr[] of N non-negative integers. The task is to find the length of the longest sub-array such that XOR of all the elements of this sub-array is strictly positive. If no such sub-array exists then print -1
Examples:
Input: arr[] = {1, 1, 1, 1}
Output: 3
Take sub-array[0:2] = {1, 1, 1}
Xor of this sub-array is equal to 1.
Input: arr[] = {0, 1, 5, 19}
Output: 4
Approach:
- If the XOR of the complete array is positive, then answer is equal to N.
- If all the elements are zeroes then the answer is -1 as it is impossible to get strictly positive XOR.
- Otherwise, let’s say that index of the first positive number is l and the last positive number is r.
- Now XOR of all the elements of the index range [l, r] must be zero as elements before l and after r are 0s which will not contribute to the XOR value and the XOR of the original array was 0.
- Consider the sub-arrays A1, A1, …, Ar-1 and Al+1, Al+2, …, AN.
- The first subarray would have XOR value equal to A[r] and second, would have an XOR value A[l] which is positive.
- Return the length of the larger sub-array among these two sub-arrays.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
int StrictlyPositiveXor( int A[], int N)
{
int allxor = 0;
bool checkallzero = true ;
for ( int i = 0; i < N; i += 1) {
allxor ^= A[i];
if (A[i] > 0)
checkallzero = false ;
}
if (allxor != 0)
return N;
if (checkallzero)
return -1;
int l = N, r = -1;
for ( int i = 0; i < N; i += 1) {
if (A[i] > 0) {
l = i + 1;
break ;
}
}
for ( int i = N - 1; i >= 0; i -= 1) {
if (A[i] > 0) {
r = i + 1;
break ;
}
}
return max(N - l, r - 1);
}
int main()
{
int A[] = { 1, 0, 0, 1 };
int N = sizeof (A) / sizeof (A[0]);
cout << StrictlyPositiveXor(A, N);
return 0;
}
|
Java
import java.io.*;
class GFG
{
static int StrictlyPositiveXor( int []A, int N)
{
int allxor = 0 ;
boolean checkallzero = true ;
for ( int i = 0 ; i < N; i += 1 )
{
allxor ^= A[i];
if (A[i] > 0 )
checkallzero = false ;
}
if (allxor != 0 )
return N;
if (checkallzero)
return - 1 ;
int l = N, r = - 1 ;
for ( int i = 0 ; i < N; i += 1 )
{
if (A[i] > 0 )
{
l = i + 1 ;
break ;
}
}
for ( int i = N - 1 ; i >= 0 ; i -= 1 )
{
if (A[i] > 0 )
{
r = i + 1 ;
break ;
}
}
return Math.max(N - l, r - 1 );
}
public static void main (String[] args)
{
int A[] = { 1 , 0 , 0 , 1 };
int N = A.length;
System.out.print(StrictlyPositiveXor(A, N));
}
}
|
Python3
def StrictlyPositiveXor(A, N) :
allxor = 0 ;
checkallzero = True ;
for i in range (N) :
allxor ^ = A[i];
if (A[i] > 0 ) :
checkallzero = False ;
if (allxor ! = 0 ) :
return N;
if (checkallzero) :
return - 1 ;
l = N; r = - 1 ;
for i in range (N) :
if (A[i] > 0 ) :
l = i + 1 ;
break ;
for i in range (N - 1 , - 1 , - 1 ) :
if (A[i] > 0 ) :
r = i + 1 ;
break ;
return max (N - l, r - 1 );
if __name__ = = "__main__" :
A = [ 1 , 0 , 0 , 1 ];
N = len (A);
print (StrictlyPositiveXor(A, N));
|
C#
using System;
class GFG
{
static int StrictlyPositiveXor( int []A, int N)
{
int allxor = 0;
bool checkallzero = true ;
for ( int i = 0; i < N; i += 1)
{
allxor ^= A[i];
if (A[i] > 0)
checkallzero = false ;
}
if (allxor != 0)
return N;
if (checkallzero)
return -1;
int l = N, r = -1;
for ( int i = 0; i < N; i += 1)
{
if (A[i] > 0)
{
l = i + 1;
break ;
}
}
for ( int i = N - 1; i >= 0; i -= 1)
{
if (A[i] > 0)
{
r = i + 1;
break ;
}
}
return Math.Max(N - l, r - 1);
}
public static void Main ()
{
int []A = { 1, 0, 0, 1 };
int N = A.Length;
Console.WriteLine(StrictlyPositiveXor(A, N));
}
}
|
Javascript
<script>
function StrictlyPositiveXor(A, N)
{
let allxor = 0;
let checkallzero = true ;
for (let i = 0; i < N; i += 1) {
allxor ^= A[i];
if (A[i] > 0)
checkallzero = false ;
}
if (allxor != 0)
return N;
if (checkallzero)
return -1;
let l = N, r = -1;
for (let i = 0; i < N; i += 1) {
if (A[i] > 0) {
l = i + 1;
break ;
}
}
for (let i = N - 1; i >= 0; i -= 1) {
if (A[i] > 0) {
r = i + 1;
break ;
}
}
return Math.max(N - l, r - 1);
}
let A = [ 1, 0, 0, 1 ];
let N = A.length;
document.write(StrictlyPositiveXor(A, N));
</script>
|
Time Complexity: O(N)
Auxiliary Space: O(1)
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...