2433. Find The Original Array of Prefix Xor LeetCode Solution
In this guide, you will get 2433. Find The Original Array of Prefix Xor LeetCode Solution with the best time and space complexity. The solution to Find The Original Array of Prefix Xor problem is provided in various programming languages like C++, Java, and Python. This will be helpful for you if you are preparing for placements, hackathons, interviews, or practice purposes. The solutions provided here are very easy to follow and include detailed explanations.
Table of Contents
- Problem Statement
- Complexity Analysis
- Find The Original Array of Prefix Xor solution in C++
- Find The Original Array of Prefix Xor solution in Java
- Find The Original Array of Prefix Xor solution in Python
- Additional Resources
Problem Statement of Find The Original Array of Prefix Xor
You are given an integer array pref of size n. Find and return the array arr of size n that satisfies:
pref[i] = arr[0] ^ arr[1] ^ … ^ arr[i].
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:
– pref[0] = 5.
– pref[1] = 5 ^ 7 = 2.
– pref[2] = 5 ^ 7 ^ 2 = 0.
– pref[3] = 5 ^ 7 ^ 2 ^ 3 = 3.
– pref[4] = 5 ^ 7 ^ 2 ^ 3 ^ 2 = 1.
Example 2:
Input: pref = [13]
Output: [13]
Explanation: We have pref[0] = arr[0] = 13.
Constraints:
1 <= pref.length <= 105
0 <= pref[i] <= 106
Complexity Analysis
- Time Complexity: O(n)
- Space Complexity: O(n)
2433. Find The Original Array of Prefix Xor LeetCode Solution in C++
class Solution {
public:
vector<int> findArray(vector<int>& pref) {
vector<int> ans(pref.size());
ans[0] = pref[0];
for (int i = 1; i < ans.size(); ++i)
ans[i] = pref[i] ^ pref[i - 1];
return ans;
}
};
/* code provided by PROGIEZ */
2433. Find The Original Array of Prefix Xor LeetCode Solution in Java
class Solution {
public int[] findArray(int[] pref) {
int[] ans = new int[pref.length];
ans[0] = pref[0];
for (int i = 1; i < ans.length; ++i)
ans[i] = pref[i] ^ pref[i - 1];
return ans;
}
}
// code provided by PROGIEZ
2433. Find The Original Array of Prefix Xor LeetCode Solution in Python
class Solution:
def findArray(self, pref: list[int]) -> list[int]:
ans = [0] * len(pref)
ans[0] = pref[0]
for i in range(1, len(ans)):
ans[i] = pref[i] ^ pref[i - 1]
return ans
# code by PROGIEZ
Additional Resources
- Explore all LeetCode problem solutions at Progiez here
- Explore all problems on LeetCode website here
Happy Coding! Keep following PROGIEZ for more updates and solutions.