Check if array can be divided into two parts with same XOR | Letuscode
Given an array of numbers, how do we check if it can be divided into two parts such that XOR values of both are same.
For example consider an array A = [1, 2, 3], it can be divided into [1, 2], and [3] because 0001 ^ 0010 = 0011
Similarly, [8, 3, 12, 7], can be divided into [8,7] and [3, 12] as 1000 ^ 0111 = 0011 ^ 1100 = 1111
This is a tricky question, where you need not actually try to divide the numbers into two sets. If any two parts of the array have the same XOR value, the XOR of all the elements will be Zero.
So we just need to check if the XOR value of all the elements is zero or not.
Read full article from Check if array can be divided into two parts with same XOR | Letuscode
No comments:
Post a Comment