File tree Expand file tree Collapse file tree 1 file changed +42
-0
lines changed Expand file tree Collapse file tree 1 file changed +42
-0
lines changed Original file line number Diff line number Diff line change
1
+ /*
2
+ Problem Link: https://leetcode.com/problems/find-the-original-array-of-prefix-xor/description/
3
+
4
+ Problem Statement: You are given an integer array pref of size n. Find and return the array arr of size n that satisfies:
5
+
6
+ pref[i] = arr[0] ^ arr[1] ^ ... ^ arr[i].
7
+ Note that ^ denotes the bitwise-xor operation.
8
+
9
+ It can be proven that the answer is unique.
10
+
11
+ Solution Approach:
12
+ A XOR B = C
13
+ A XOR C = B
14
+ the above property will only work in the context of writing a program.
15
+ Using this property we can calculate the ans array
16
+
17
+ */
18
+
19
+ /* ------------CODE---------------- */
20
+ class Solution {
21
+ public int [] findArray (int [] pref ) {
22
+
23
+ int n = pref .length ;
24
+ int arr [] = new int [n ];
25
+
26
+ arr [0 ] = pref [0 ];
27
+
28
+ // pref[1] = arr[0]^arr[1]
29
+ int xor = arr [0 ];
30
+ for (int i =1 ; i <n ; i ++) {
31
+ arr [i ] = pref [i ]^xor ;
32
+ xor = xor ^arr [i ];
33
+ }
34
+ return arr ;
35
+
36
+ }
37
+ }
38
+
39
+ /*
40
+ Time Complexity: O(n)
41
+ Space Complexity: O(n)
42
+ */
You can’t perform that action at this time.
0 commit comments