2007. Find Original Array From Doubled Array

Updated: 2024-03-12
1 min read
[]

On This Page

LeetCode problem 2007

class Solution:
    def findOriginalArray(self, changed: List[int]) -> List[int]:
        n = len(changed)
        if n & 1:
            return []
        cnt = Counter(changed)
        changed.sort()
        res = []
        for x in changed:
            if cnt[x] == 0:
                continue
            if cnt[x * 2] <= 0:
                return []
            res.append(x)
            cnt[x] -= 1
            cnt[x * 2] -= 1
        return res if len(res) == n // 2 else []