Posted on

Description

Submission

class Solution {
public:
    bool canReorderDoubled(vector<int>& arr) {
        map<int, int> cnt;
        int n = arr.size();
        
        for(auto a: arr) {
            cnt[a]++;
        }
        
        for(auto iter = cnt.begin(); iter != cnt.end(); iter++) {
            while(iter->second != 0) {
                if(iter->first < 0 && (abs(iter->first) % 2 == 1)) return false;
                int target;
                if(iter->first < 0) {
                    target = iter->first / 2;
                } else {
                    target = iter->first * 2;
                }
                auto match = cnt.lower_bound(target);
                if(match == cnt.end() || match->first != target || match->second == 0)  return false;
                match->second -= 1;
                iter->second -= 1;
                if(iter->second < 0) return false;
            }
        }
        
        return true;
    }
};

Leave a Reply

Your email address will not be published. Required fields are marked *