Trends

XOR adjacent equality check

Trending 1 year ago
beritaja.com

Given an array arr[] of magnitude N, nan task is to cheque if we tin make each elements of nan array adjacent by taking 2 adjacent elements, removing them, and placing their bitwise XOR successful their spot until only 2 elements remain. If it’s possible, return “YES”, different “NO”.

Examples:

Input: N = 3, arr[] = {0, 2, 2}
Output:  YES
Explanation: we tin region nan first 2 elements, 0 and 2, and switch them by 0⊕2=2(here ⊕ is nan xor operator). The array will beryllium [2, 2], truthful each nan elements are equal.

Input: N = 4, arr[] = {2, 3, 1, 10}
Output: NO
Explanation: There’s nary measurement to make each elements adjacent by performing described operation

Input: N = 5, arr[]={3, 3, 3, 2, 2}
Output: YES
Explanation: As we region 3 and 2 and switch them pinch 3⊕2=1 and past region 1 and 2 and switch them by 1⊕2=3, So nan array will beryllium [3, 3, 3], Hence each nan elements of array becomes equal

Approach: Follow nan beneath attack for knowing nan solution.

  •  The Solution to this problem is to cheque if it’s imaginable to disagreement an array of numbers into 2 subsets specified that nan sum of elements successful each subset is equal, utilizing nan XOR operation.
  • The XOR cognition has nan spot that nan XOR of 2 adjacent numbers is 0. This intends that if nan sum of elements successful 1 subset is adjacent to nan sum of elements successful different subset, nan XOR of each elements successful nan first subset will beryllium adjacent to nan XOR of each elements successful nan 2nd subset.
  • The XOR cognition is performed connected each elements of nan array to get nan consequence stored successful a adaptable say “xorTot“. The worth of “xorTot” represents nan XOR of each elements successful nan array. Next, For each constituent successful nan array, it performs an XOR cognition pinch nan erstwhile consequence stored successful a adaptable say “preXor“, The updated consequence aft nan XOR cognition is stored backmost successful “preXor”.
  • If astatine immoderate constituent “preXor” becomes adjacent to “xorTot“, it intends that a subset of elements pinch adjacent sum has been found, and their XOR is adjacent to nan XOR of each elements successful nan array. We initialize a count adaptable opportunity “count” and whenever “preXor” becomes adjacent to “xorTot”, we increment our count. 
  • Therefore, if nan worth of “count” is greater than 1, it intends that location are astatine slightest 2 subsets pinch an adjacent sum, and nan array tin beryllium divided into 2 subsets pinch adjacent sum utilizing nan XOR operation.

Here is simply a step-by-step mentation of nan implementation:

  • Initialize variables: xorTot which will shop nan XOR of each elements successful nan array, preXor which will shop nan XOR of elements from nan commencement of nan array until definite scale i, and count which will shop nan number of times nan XOR of elements from nan commencement of nan array until scale one is adjacent to xorTot.
  • Loop done nan array, XORing each constituent pinch xorTot to find nan XOR of each elements successful nan array.
  • Loop done nan array again, XORing each constituent pinch preXor. If preXor becomes adjacent to xorTot, it intends that nan XOR of elements from nan commencement of nan array until nan existent scale one is adjacent to xorTot. In this case, preXor is reset to 0 and nan adjacent scale is simply a caller start, and nan count is incremented. Now cheque for nan remaining indices
  • Check if nan count is greater than 1 aliases if xorTot is adjacent to 0. If either of these conditions is true, past it’s imaginable to divided nan array into 2 non-empty parts specified that nan XOR of elements successful each portion is equal. So, nan output is “YES”. If neither of these conditions is true, past nan output is “NO”.

Below is nan Implementation successful C++:

C++

// C++ codification to instrumentality nan approach. #include <bits/stdc++.h> using namespace std; void solve(int arr[], int n) { int xorTot = 0, preXor = 0, count = 0; // XOR of each elements of array for (int one = 0; one < n; i++) { xorTot = xorTot ^ arr[i]; } for (int one = 0; one < n; i++) { preXor = preXor ^ arr[i]; // Moment definite elements of the // array go adjacent to xorTot. if (preXor == xorTot) preXor = 0, count++; } if (count > 1 || xorTot == 0) cout << "YES" << endl; else cout << "NO" << endl; } // Driver code int main() { // TestCase 1 int arr[] = { 3, 3, 3, 2, 2 }; int n = sizeof(arr) / sizeof(arr[0]); // Function Call solve(arr, n); return 0; }

Time Complexity: O(N), wherever N is nan number of elements successful nan array arr.
Auxiliary Space: O(1)

Editor: Naga



Read other contents from Beritaja.com at
More Source
close