Skip to content

Intuition

The solution leverages patterns in the XOR operation of specific sequences within an array. By recognizing these patterns, we can simplify the XOR computation, reducing the problem size and allowing for an efficient calculation.

 

Approach

Explanation:

  1. Pattern Analysis:
  2. Certain patterns in XOR operations simplify the problem. For instance, the XOR of numbers of the form 4x and 4x + 2 always results in 2, while the XOR of numbers 4x, 4x + 2, 4x + 4, and 4x + 6 results in 0.
  3. Similarly, the XOR of 4x + 1 and 4x + 3 is 2, and extending this to 4x + 1, 4x + 3, 4x + 5, and 4x + 7 yields 0.

  4. Base Case Handling:

  5. If n == 1, the result is just the start since there's only one element.

  6. Initial Setup:

  7. res is initialized to 0 and will store the XOR of selected elements.
  8. end is computed as the last element in the nums array (start + 2 * (n - 1)).

  9. Initial Check and Adjustments:

  10. If start % 4 > 1, meaning start is in the form of 4x + 2 or 4x + 3, the XOR operation includes start itself, and n is reduced by 1.
  11. Similarly, if end % 4 <= 1, indicating end is in the form of 4x or 4x + 1, the XOR operation includes end, and n is reduced by 1.

  12. Final XOR Computation:

  13. After the adjustments in the previous step, n will always be even, and according to the observed patterns, the XOR of the remaining elements will always result in either 0 or 2.
  14. The final XOR result is then computed by XOR-ing res with n % 4, following the pattern.

Complexity

  • Time complexity: $O(1)$, since the computation involves a fixed number of steps.
  • Space complexity: $O(1)$, as only a few variables are used.

Code

```cpp [] class Solution { public: int xorOperation(int n, int start) { if (n == 1) return start;

    int res = 0;
    int end = start + 2 * (n - 1);

    if (start % 4 > 1) {
        res ^= start;
        --n;
    }

    if (end % 4 <= 1) {
        res ^= end;
        --n;
    }

    res ^= n % 4;

    return res;
}

}; ```