Find Original Array From Doubled

Medium

Description

Given a changed array formed by appending doubled values of original array and shuffling, return the original array or empty if impossible.

Examples

Input:changed = [1,3,4,2,6,8]
Output:[1,3,4]
Explanation:

Original array doubled.

Input:changed = [6,3,0,1]
Output:[]
Explanation:

Edge case with empty result.

Input:changed = [0,0,0,0]
Output:[0,0]
Explanation:

When the original array contains zeros, each zero pairs with another zero (since 0 * 2 = 0). With 4 zeros total, it is possible to form 2 pairs, so the original array is [0,0].

Constraints

  • 1 ≤ changed.length ≤ 10⁵

Ready to solve this problem?

Practice solo or challenge other developers in a real-time coding battle!