You are provided an array A of n integers. You are required to partition the array into the minimum number of subarrays such that the XOR of any non-empty subset of the subarrays is not equal to zero. If no such partition exists, then print 1. Otherwise, print 1.
Note:
  • A subarray is a non-empty contiguous subsequence of an array.
  • The XOR of a set of subarrays is defined as the sum of determined XORs of the numbers in all the subarrays when accumulated.



Solution

#include <iostream>
using namespace std;
int main() {
    long long num,c=0;
    cin >> num;   
    long long a[num];
    for(long long i=0;i<num;i++)
    {
       cin>>a[i];    }
    for(long long i=0;i<num;i++)
    {
        c=c^a[i];
        if(c==0)
        {
            cout<<-1;
            exit(0);
        }
    }
    cout<<1;
}




if you have any problem in this code, take a screenshot and contact with Rishabh Chaubey.

Post a Comment

Previous Post Next Post