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 <= 105
0 <= changed[i] <= 105
We notice that if the array changed
is a double array, then the smallest element in the array changed
must also be an element in the original array. Therefore, we can first sort the array changed
, and then start from the first element to traverse the array changed
in ascending order.
We use a hash table or array changed
. For each element changed
, we first check whether
After the traversal, we return the answer array.
The time complexity is changed
.
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
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;
}
}
class Solution {
public:
vector<int> findOriginalArray(vector<int>& changed) {
sort(changed.begin(), changed.end());
vector<int> cnt(changed.back() + 1);
for (int x : changed) {
++cnt[x];
}
vector<int> ans;
for (int x : changed) {
if (cnt[x] == 0) {
continue;
}
--cnt[x];
int y = x << 1;
if (y >= cnt.size() || cnt[y] <= 0) {
return {};
}
--cnt[y];
ans.push_back(x);
}
return ans;
}
};
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
}
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;
}