[Solved] Consecutive Xor STARTERS 73 CodeChef

Chef has an array AA of length NN. He can perform the following operation on AA:

1) Select an index ii (1 \le i \le N – 1)(1≤iN−1) and select an integer XX (1 \le X \lt 2^{20})(1≤X<220).\\2) Set A_i := (A_i \oplus X)Ai​:=(Ai​⊕X) and A_{i + 1} := (A_{i + 1} \oplus X)Ai+1​:=(Ai+1​⊕X). (Here, \oplus⊕ denotes the bitwise XOR operation)

Determine if Chef can make all the elements of AA equal by applying the above operation any number of times (possibly zero).

Input Format
  • The first line contains a single integer TT — the number of test cases. Then the test cases follow.
  • The first line of each test case contains an integer NN — the size of the array AA.
  • The second line of each test case contains NN space-separated integers A_1, A_2, \dots, A_NA1​,A2​,…,AN​ denoting the array AA.
Output Format

For each test case, output YES if it is possible to make all the elements of AA equal by applying the given operation any number of times. Otherwise, output NO.

You may print each character of YES and NO in uppercase or lowercase (for example, yesyEsYes will be considered identical).

Constraints
  • 1≤T≤10^5
  • 1≤N≤10^5
  • 0≤Ai​<2^20
  • The sum of NN over all test cases won’t exceed 2⋅10^5.

Sample 1:

Input:

3
4
3 2 2 3
6
0 1 2 3 4 5
3
1 2 4

Output:

YES
NO
YES
[Solved] Consecutive Xor STARTERS 73 CodeChef

Solution

/* package codechef; // don't place package name! */

import java.util.*;
import java.lang.*;
import java.io.*;

/* Name of the class has to be "Main" only if the class is public. */
class Codechef
{
	public static void main (String[] args) throws java.lang.Exception
	{
		// your code goes here
		Scanner sc = new Scanner(System.in);
        int t = sc.nextInt();
        while(t-->0){
            int n = sc.nextInt();
            int[] arr = new int[n];
            for(int i=0;i<n;i++){
                arr[i] = sc.nextInt();
            }
            int xor = 0;
            for(int i=0;i<n-1;i++){
                xor = xor ^ arr[i];
            }
            if(xor == arr[n-1]){
                System.out.println("YES");
            }
            else if(n%2 == 0){
                System.out.println("NO");
            }
            else{
                System.out.println("YES");
            }
        }
	}
}

Happy Learning – If you require any further information, feel free to contact me.

Share your love
Saurav Hathi

Saurav Hathi

I'm currently studying Bachelor of Computer Science at Lovely Professional University in Punjab.

📌 Nodejs and Android 😎
📌 Java

Articles: 444

Leave a Reply

Your email address will not be published. Required fields are marked *