LeetCode-in-Java

2433. Find The Original Array of Prefix Xor

Medium

You are given an integer array pref of size n. Find and return the array arr of size n that satisfies:

Note that ^ denotes the bitwise-xor operation.

It can be proven that the answer is unique.

Example 1:

Input: pref = [5,2,0,3,1]

Output: [5,7,2,3,2]

Explanation: From the array [5,7,2,3,2] we have the following:

Example 2:

Input: pref = [13]

Output: [13]

Explanation: We have pref[0] = arr[0] = 13.

Constraints:

Solution

public class Solution {
    public int[] findArray(int[] pref) {
        int[] result = new int[pref.length];
        result[0] = pref[0];
        for (int i = 1; i < pref.length; i++) {
            result[i] = pref[i] ^ pref[i - 1];
        }
        return result;
    }
}