LeetCode 954 Array of Doubled Pairs

LeetCode 954 Array of Doubled Pairs Problem

Download Code
class Solution(object):
    def canReorderDoubled(self, A):
        """
        :type A: List[int]
        :rtype: bool
        """
        v_map = {}
        A.sort(key=lambda x: abs(x))
        for n in A:
            v_map[n] = v_map.get(n, 0) + 1
        for n in A:
            if v_map[n] <= 0:
                continue
            if 2 * n in v_map and v_map[2 * n] > 0:
                v_map[n] -= 1
                v_map[2 * n] -= 1
            else:
                return False
        return True


if __name__ == '__main__':
    s = Solution()
    print s.canReorderDoubled([3, 1, 3, 6])
    print s.canReorderDoubled([2, 1, 2, 6])
    print s.canReorderDoubled([4, -2, 2, -4])
    print s.canReorderDoubled([1, 2, 4, 16, 8, 4])
Download Array of Doubled Pairs.py

List of all Array of Doubled Pairs problems

Leetcode 954 Array of Doubled Pairs problem solution in python3 with explanation. This is the best place to expand your knowledge and get prepared for your next interview.

Feedback is the most important part of any website.

If you have any query, suggestion or feedback, Please feel free to contact us.