|
| 1 | +class Solution(object): |
| 2 | + def doesValidArrayExist(self, derived): |
| 3 | + """ |
| 4 | + :type derived: List[int] |
| 5 | + :rtype: bool |
| 6 | + """ |
| 7 | + n = len(derived) |
| 8 | + xor_sum = 0 |
| 9 | + |
| 10 | + # Hints-02: The xor-sum of the derived array should be 0 since there is always a duplicate occurrence of each element. |
| 11 | + |
| 12 | + # Example: Length of derived=4 |
| 13 | + # derived[0]=org[0]^org[1] |
| 14 | + # derived[1]=org[1]^org[2] |
| 15 | + # derived[2]=org[2]^org[3] |
| 16 | + # derived[3]=org[3]^org[0] |
| 17 | + |
| 18 | + # Xor-Sum = derived[0] ^ derived[1] ^ derived[2] ^ derived[3] |
| 19 | + # Xor-Sum = (org[0]^org[1]) ^ (org[1]^org[2]) ^ (org[2]^org[3]) ^ (org[3]^org[0]) |
| 20 | + # #re-arrange it..... |
| 21 | + # Xor-Sum = (org[0]^org[0]) ^ (org[1]^org[1]) ^ (org[2]^org[2]) ^ (org[3]^org[3]) |
| 22 | + # Xor-Sum = 0 |
| 23 | + |
| 24 | + for num in derived: |
| 25 | + xor_sum ^= num |
| 26 | + |
| 27 | + return xor_sum == 0 |
| 28 | + |
0 commit comments