Tuesday, November 29, 2022
HomeSoftware DevelopmentExamine if Binary Array will be break up into X Subarray with...

Examine if Binary Array will be break up into X Subarray with similar bitwise XOR


  

import java.io.*;

import java.util.*;

  

public class GFG {

  

    

    

    

    public static String test(int arr[], int n, int x)

    {

        int xor = 0;

        int count0 = 0, count1 = 0;

  

        for (int i = 0; i < n; i++) {

            xor ^= arr[i];

            if (xor == 0)

                count0++;

        }

        if (count0 >= x && xor != 1) {

            return "Sure";

        }

        xor = 0;

        for (int i = 0; i < n; i++) {

            xor ^= arr[i];

            if (xor == 1) {

                count1++;

                xor = 0;

            }

        }

        if (count1 >= x && (count1 - x) % 2 == 0) {

            return "Sure";

        }

        return "No";

    }

  

    

    public static void fundamental(String[] args)

    {

        int[] A = { 0, 1, 1, 0, 0 };

        int N = A.size;

        int X = 3;

  

        

        System.out.println(test(A, N, X));

    }

}

RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Most Popular

Recent Comments