Find Original Array From Doubled Array - Problem
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.
Input & Output
Example 1 — Basic Case
$
Input:
changed = [1,3,4,2,6,8]
›
Output:
[1,3,4]
💡 Note:
Original array [1,3,4] becomes [1,3,4,2,6,8] after doubling [2,6,8] and shuffling. We can recover [1,3,4] by pairing: 1↔2, 3↔6, 4↔8.
Example 2 — Impossible Case
$
Input:
changed = [6,3,0,1]
›
Output:
[]
💡 Note:
Cannot form valid pairs. We have 6 but no 12, and 3 but no 6 available after pairing other elements.
Example 3 — With Zeros
$
Input:
changed = [0,0,2,1]
›
Output:
[0,1]
💡 Note:
Zeros pair with themselves: 0↔0, and 1↔2. Original array could be [0,1].
Constraints
- 1 ≤ changed.length ≤ 105
- 0 ≤ changed[i] ≤ 105
Visualization
Tap to expand
Understanding the Visualization
1
Original Array
Start with array [1, 3, 4]
2
Double & Combine
Create [1, 3, 4, 2, 6, 8] then shuffle
3
Reverse Process
Find pairs and recover [1, 3, 4]
Key Takeaway
🎯 Key Insight: Sort first, then greedily pair each element with its double using frequency counting
💡
Explanation
AI Ready
💡 Suggestion
Tab
to accept
Esc
to dismiss
// Output will appear here after running code