Skip to content

Commit 55b62e3

Browse files
committed
Sync LeetCode submission Runtime - 43 ms (67.15%), Memory - 22.4 MB (48.90%)
1 parent 6a5a727 commit 55b62e3

File tree

2 files changed

+68
-0
lines changed

2 files changed

+68
-0
lines changed
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
<p>A <strong>0-indexed</strong> array <code>derived</code> with length <code>n</code> is derived by computing the <strong>bitwise XOR</strong>&nbsp;(&oplus;) of adjacent values in a <strong>binary array</strong> <code>original</code> of length <code>n</code>.</p>
2+
3+
<p>Specifically, for each index <code>i</code> in the range <code>[0, n - 1]</code>:</p>
4+
5+
<ul>
6+
<li>If <code>i = n - 1</code>, then <code>derived[i] = original[i] &oplus; original[0]</code>.</li>
7+
<li>Otherwise, <code>derived[i] = original[i] &oplus; original[i + 1]</code>.</li>
8+
</ul>
9+
10+
<p>Given an array <code>derived</code>, your task is to determine whether there exists a <strong>valid binary array</strong> <code>original</code> that could have formed <code>derived</code>.</p>
11+
12+
<p>Return <em><strong>true</strong> if such an array exists or <strong>false</strong> otherwise.</em></p>
13+
14+
<ul>
15+
<li>A binary array is an array containing only <strong>0&#39;s</strong> and <strong>1&#39;s</strong></li>
16+
</ul>
17+
18+
<p>&nbsp;</p>
19+
<p><strong class="example">Example 1:</strong></p>
20+
21+
<pre>
22+
<strong>Input:</strong> derived = [1,1,0]
23+
<strong>Output:</strong> true
24+
<strong>Explanation:</strong> A valid original array that gives derived is [0,1,0].
25+
derived[0] = original[0] &oplus; original[1] = 0 &oplus; 1 = 1
26+
derived[1] = original[1] &oplus; original[2] = 1 &oplus; 0 = 1
27+
derived[2] = original[2] &oplus; original[0] = 0 &oplus; 0 = 0
28+
</pre>
29+
30+
<p><strong class="example">Example 2:</strong></p>
31+
32+
<pre>
33+
<strong>Input:</strong> derived = [1,1]
34+
<strong>Output:</strong> true
35+
<strong>Explanation:</strong> A valid original array that gives derived is [0,1].
36+
derived[0] = original[0] &oplus; original[1] = 1
37+
derived[1] = original[1] &oplus; original[0] = 1
38+
</pre>
39+
40+
<p><strong class="example">Example 3:</strong></p>
41+
42+
<pre>
43+
<strong>Input:</strong> derived = [1,0]
44+
<strong>Output:</strong> false
45+
<strong>Explanation:</strong> There is no valid original array that gives derived.
46+
</pre>
47+
48+
<p>&nbsp;</p>
49+
<p><strong>Constraints:</strong></p>
50+
51+
<ul>
52+
<li><code>n == derived.length</code></li>
53+
<li><code>1 &lt;= n&nbsp;&lt;= 10<sup>5</sup></code></li>
54+
<li>The values in <code>derived</code>&nbsp;are either <strong>0&#39;s</strong> or <strong>1&#39;s</strong></li>
55+
</ul>
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
# Approach 2: Cumulative XOR
2+
3+
# Time: O(n)
4+
# Space: O(1)
5+
6+
class Solution:
7+
def doesValidArrayExist(self, derived: List[int]) -> bool:
8+
XOR = 0
9+
for element in derived:
10+
XOR ^= element
11+
12+
return XOR == 0
13+

0 commit comments

Comments
 (0)