Check whether an Array can be made 0 by splitting and merging repeatedly
Check whether an Array can be made 0 by splitting and merging repeatedly
Given an array arr[] with N elements, the task is to find whether all the elements of the given array can be made 0 by given operations. Only 2 types of operations can be performed on this array:
Split an element B into 2 elements C and D such that B = C + D.
Merge 2 elements P and Q as one element R such that R = P^Q i.e. (XOR of P and Q).
You have to determine whether it is possible to convert array A to size 1, containing a single element equal to 0 after several splits and/or merges?
Input: arr = [9, 17]
Output: Yes
Explanation: Following is one possible sequence of operations –
1) Merge i.e 9 XOR 17 = 24
2) Split 24 into two parts each of size 12
3) Merge i.e 12 XOR 12 = 0
As there is only 1 element i.e 0. So, it is possible.
Input: arr = [1]
Output: No
Comments
Post a Comment