Skip to content

Commit 1942a4f

Browse files
committed
LC daily
1 parent 6b01abb commit 1942a4f

File tree

1 file changed

+42
-0
lines changed

1 file changed

+42
-0
lines changed
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
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+
*/

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy