Skip to content

Commit 14d95a3

Browse files
Create find_original_array_from_doubled_array.cpp
1 parent 9b5e565 commit 14d95a3

File tree

1 file changed

+23
-0
lines changed

1 file changed

+23
-0
lines changed
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
class Solution {
2+
public:
3+
vector<int> findOriginalArray(vector<int>& changed) {
4+
unordered_map<int, int> freq;
5+
for (auto num : changed) freq[num]++;
6+
7+
sort(changed.begin(), changed.end());
8+
9+
vector<int> res;
10+
for (auto num : changed) {
11+
if (freq[num] && freq[num*2]) {
12+
freq[num]--;
13+
freq[num*2]--;
14+
res.push_back(num);
15+
}
16+
}
17+
18+
for (auto [a, b] : freq)
19+
if (b) return {};
20+
21+
return res;
22+
}
23+
};

0 commit comments

Comments
 (0)