Leetcode 991 Solution

This article provides solution to leetcode question 991 (array-of-doubled-pairs)

https://leetcode.com/problems/array-of-doubled-pairs

Solution

class Solution {
public:
    bool canReorderDoubled(vector<int>& A) {
        map<int, int> m;

        for (auto a: A)
            m[abs(a)]++;

        for (auto it = m.begin(); it != m.end(); it++)
        {
            while (it->second)
            {
                it->second--;

                if (m[2 * it->first] == 0)
                    return false;

                m[2 * it->first]--;
            }
        }

        return true;
    }
};