Recover the original array from a shuffled array of values and their doubles

Quick Overview

Given a shuffled array that holds every element of an unknown array plus twice each element, recover the original array in ascending order or return an empty array when none exists. It tests frequency counting, a greedy argument that starts from the smallest value, and careful handling of zeros and repeated values.

Recover the original array from a shuffled array of values and their doubles

Company: Huawei

Role: Software Engineer

Category: Coding & Algorithms

Difficulty: medium

Interview Round: Other

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. ### Function Signature ```python def find_original_array(changed: list[int]) -> list[int]: ``` ### Rules - Every element of `changed` must be used exactly once, either as an element of `original` or as the double of one. - Return the elements of `original` sorted in ascending order. When `changed` is a doubled array, the multiset of values in `original` is fully determined, so the sorted list is the only correct output. - Return `[]` when `changed` is not a doubled array. - Zero doubles to zero, so a 0 in `original` is matched by a second 0 in `changed`. ### Constraints - `2 <= len(changed) <= 10^5` - `0 <= changed[i] <= 10^5` ### Examples ```text Input: changed = [1,3,4,2,6,8] Output: [1,3,4] ``` Doubling `[1,3,4]` gives `[2,6,8]`, and the two lists together are a rearrangement of `changed`. ```text Input: changed = [6,3,0,1] Output: [] ``` The only 0 has no partner, since a 0 can be matched only by another 0. The 1 also has no 2 to pair with, so no `original` exists. ```text Input: changed = [0,4,1,0,2,8,2,4] Output: [0,1,2,4] ``` Sorted, `changed` is `[0,0,1,2,2,4,4,8]`. The two zeros give one 0, the 1 pairs with a 2, the other 2 pairs with a 4, and the other 4 pairs with the 8. The value 2 appears once as an element of `original` and once as a double.

Overview: Given a shuffled array that holds every element of an unknown array plus twice each element, recover the original array in ascending order or return an empty array when none exists. It tests frequency counting, a greedy argument that starts from the smallest value, and careful handling of zeros and repeated values.

Huawei logo
Huawei
Nov 25, 2026
mediumSoftware EngineerOtherCoding & Algorithms
0
0

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.

Function Signature

def find_original_array(changed: list[int]) -> list[int]:

Rules

  • Every element of changed must be used exactly once, either as an element of original or as the double of one.
  • Return the elements of original sorted in ascending order. When changed is a doubled array, the multiset of values in original is fully determined, so the sorted list is the only correct output.
  • Return [] when changed is not a doubled array.
  • Zero doubles to zero, so a 0 in original is matched by a second 0 in changed .

Constraints

  • 2 <= len(changed) <= 10^5
  • 0 <= changed[i] <= 10^5

Examples

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

Doubling [1,3,4] gives [2,6,8], and the two lists together are a rearrangement of changed.

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

The only 0 has no partner, since a 0 can be matched only by another 0. The 1 also has no 2 to pair with, so no original exists.

Input: changed = [0,4,1,0,2,8,2,4]
Output: [0,1,2,4]

Sorted, changed is [0,0,1,2,2,4,4,8]. The two zeros give one 0, the 1 pairs with a 2, the other 2 pairs with a 4, and the other 4 pairs with the 8. The value 2 appears once as an element of original and once as a double.