18. Game of XOR

The problem can be found at the following link: Question Link

My Approach

In this problem, I used XOR property and some observations:

  • XOR of any even occurrences of a number is zero.

  • By observing arrays of different sizes, I found the number of occurrences for each index:

From the above observation, we can conclude:

  • If the array length is even, return 0 since all even occurrences result in an XOR value of 0.

  • If the array length is odd, iterate through the array and XOR all odd occurrence indices, as the XOR of odd occurrences gives the number itself.

Time and Auxiliary Space Complexity

  • Time Complexity: O(N), where N is the length of the array.

  • Auxiliary Space Complexity: O(1)

Code (C++)

Contribution and Support

For discussions, questions, or doubts related to this solution, please visit our discussion section. We welcome your input and aim to foster a collaborative learning environment.

If you find this solution helpful, consider supporting us by giving a ⭐ star to the getlost01/gfg-potd repository.

Last updated

Was this helpful?