Off-by-one on range boundaries
Wrong move: Loop endpoints miss first/last candidate.
Usually fails on: Fails on minimal arrays and exact-boundary answers.
Fix: Re-derive loops from inclusive/exclusive ranges before coding.
Move from brute-force thinking to an efficient approach using array strategy.
An integer array original is transformed into a doubled array changed by appending twice the value of every element in original, and then randomly shuffling the resulting array.
Given an array changed, return original if changed is a doubled array. If changed is not a doubled array, return an empty array. The elements in original may be returned in any order.
Example 1:
Input: changed = [1,3,4,2,6,8] Output: [1,3,4] Explanation: One possible original array could be [1,3,4]: - Twice the value of 1 is 1 * 2 = 2. - Twice the value of 3 is 3 * 2 = 6. - Twice the value of 4 is 4 * 2 = 8. Other original arrays could be [4,3,1] or [3,1,4].
Example 2:
Input: changed = [6,3,0,1] Output: [] Explanation: changed is not a doubled array.
Example 3:
Input: changed = [1] Output: [] Explanation: changed is not a doubled array.
Constraints:
1 <= changed.length <= 1050 <= changed[i] <= 105Problem summary: An integer array original is transformed into a doubled array changed by appending twice the value of every element in original, and then randomly shuffling the resulting array. Given an array changed, return original if changed is a doubled array. If changed is not a doubled array, return an empty array. The elements in original may be returned in any order.
Start with the most direct exhaustive search. That gives a correctness anchor before optimizing.
Pattern signal: Array · Hash Map · Greedy
[1,3,4,2,6,8]
[6,3,0,1]
[1]
array-of-doubled-pairs)recover-the-original-array)Source-backed implementations are provided below for direct study and interview prep.
// Accepted solution for LeetCode #2007: Find Original Array From Doubled Array
class Solution {
public int[] findOriginalArray(int[] changed) {
int n = changed.length;
Arrays.sort(changed);
int[] cnt = new int[changed[n - 1] + 1];
for (int x : changed) {
++cnt[x];
}
int[] ans = new int[n >> 1];
int i = 0;
for (int x : changed) {
if (cnt[x] == 0) {
continue;
}
--cnt[x];
int y = x << 1;
if (y >= cnt.length || cnt[y] <= 0) {
return new int[0];
}
--cnt[y];
ans[i++] = x;
}
return ans;
}
}
// Accepted solution for LeetCode #2007: Find Original Array From Doubled Array
func findOriginalArray(changed []int) (ans []int) {
sort.Ints(changed)
cnt := make([]int, changed[len(changed)-1]+1)
for _, x := range changed {
cnt[x]++
}
for _, x := range changed {
if cnt[x] == 0 {
continue
}
cnt[x]--
y := x << 1
if y >= len(cnt) || cnt[y] <= 0 {
return []int{}
}
cnt[y]--
ans = append(ans, x)
}
return
}
# Accepted solution for LeetCode #2007: Find Original Array From Doubled Array
class Solution:
def findOriginalArray(self, changed: List[int]) -> List[int]:
changed.sort()
cnt = Counter(changed)
ans = []
for x in changed:
if cnt[x] == 0:
continue
cnt[x] -= 1
if cnt[x << 1] <= 0:
return []
cnt[x << 1] -= 1
ans.append(x)
return ans
// Accepted solution for LeetCode #2007: Find Original Array From Doubled Array
/**
* [2007] Find Original Array From Doubled Array
*
* An integer array original is transformed into a doubled array changed by appending twice the value of every element in original, and then randomly shuffling the resulting array.
* Given an array changed, return original if changed is a doubled array. If changed is not a doubled array, return an empty array. The elements in original may be returned in any order.
*
* Example 1:
*
* Input: changed = [1,3,4,2,6,8]
* Output: [1,3,4]
* Explanation: One possible original array could be [1,3,4]:
* - Twice the value of 1 is 1 * 2 = 2.
* - Twice the value of 3 is 3 * 2 = 6.
* - Twice the value of 4 is 4 * 2 = 8.
* Other original arrays could be [4,3,1] or [3,1,4].
*
* Example 2:
*
* Input: changed = [6,3,0,1]
* Output: []
* Explanation: changed is not a doubled array.
*
* Example 3:
*
* Input: changed = [1]
* Output: []
* Explanation: changed is not a doubled array.
*
*
* Constraints:
*
* 1 <= changed.length <= 10^5
* 0 <= changed[i] <= 10^5
*
*/
pub struct Solution {}
// problem: https://leetcode.com/problems/find-original-array-from-doubled-array/
// discuss: https://leetcode.com/problems/find-original-array-from-doubled-array/discuss/?currentPage=1&orderBy=most_votes&query=
// submission codes start here
impl Solution {
pub fn find_original_array(changed: Vec<i32>) -> Vec<i32> {
vec![]
}
}
// submission codes end
#[cfg(test)]
mod tests {
use super::*;
#[test]
#[ignore]
fn test_2007_example_1() {
let changed = vec![1, 3, 4, 2, 6, 8];
let result = vec![1, 3, 4];
assert_eq!(Solution::find_original_array(changed), result);
}
#[test]
#[ignore]
fn test_2007_example_2() {
let changed = vec![6, 3, 0, 1];
let result = vec![];
assert_eq!(Solution::find_original_array(changed), result);
}
#[test]
#[ignore]
fn test_2007_example_3() {
let changed = vec![1];
let result = vec![];
assert_eq!(Solution::find_original_array(changed), result);
}
}
// Accepted solution for LeetCode #2007: Find Original Array From Doubled Array
function findOriginalArray(changed: number[]): number[] {
changed.sort((a, b) => a - b);
const cnt: number[] = Array(changed.at(-1)! + 1).fill(0);
for (const x of changed) {
++cnt[x];
}
const ans: number[] = [];
for (const x of changed) {
if (cnt[x] === 0) {
continue;
}
cnt[x]--;
const y = x << 1;
if (y >= cnt.length || cnt[y] <= 0) {
return [];
}
cnt[y]--;
ans.push(x);
}
return ans;
}
Use this to step through a reusable interview workflow for this problem.
Try every possible combination of choices. With n items each having two states (include/exclude), the search space is 2ⁿ. Evaluating each combination takes O(n), giving O(n × 2ⁿ). The recursion stack or subset storage uses O(n) space.
Greedy algorithms typically sort the input (O(n log n)) then make a single pass (O(n)). The sort dominates. If the input is already sorted or the greedy choice can be computed without sorting, time drops to O(n). Proving greedy correctness (exchange argument) is harder than the implementation.
Review these before coding to avoid predictable interview regressions.
Wrong move: Loop endpoints miss first/last candidate.
Usually fails on: Fails on minimal arrays and exact-boundary answers.
Fix: Re-derive loops from inclusive/exclusive ranges before coding.
Wrong move: Zero-count keys stay in map and break distinct/count constraints.
Usually fails on: Window/map size checks are consistently off by one.
Fix: Delete keys when count reaches zero.
Wrong move: Locally optimal choices may fail globally.
Usually fails on: Counterexamples appear on crafted input orderings.
Fix: Verify with exchange argument or monotonic objective before committing.