<p>Given an integer array of even length <code>arr</code>, return <code>true</code><em> if it is possible to reorder </em><code>arr</code><em> such that </em><code>arr[2 * i + 1] = 2 * arr[2 * i]</code><em> for every </em><code>0 &lt;= i &lt; len(arr) / 2</code><em>, or </em><code>false</code><em> otherwise</em>.</p>

<p>&nbsp;</p>
<p><strong class="example">Example 1:</strong></p>

<pre>
<strong>Input:</strong> arr = [3,1,3,6]
<strong>Output:</strong> false
</pre>

<p><strong class="example">Example 2:</strong></p>

<pre>
<strong>Input:</strong> arr = [2,1,2,6]
<strong>Output:</strong> false
</pre>

<p><strong class="example">Example 3:</strong></p>

<pre>
<strong>Input:</strong> arr = [4,-2,2,-4]
<strong>Output:</strong> true
<strong>Explanation:</strong> We can take two groups, [-2,-4] and [2,4] to form [-2,-4,2,4] or [2,4,-2,-4].
</pre>

<p>&nbsp;</p>
<p><strong>Constraints:</strong></p>

<ul>
	<li><code>2 &lt;= arr.length &lt;= 3 * 10<sup>4</sup></code></li>
	<li><code>arr.length</code> is even.</li>
	<li><code>-10<sup>5</sup> &lt;= arr[i] &lt;= 10<sup>5</sup></code></li>
</ul>

static const int _ = []() {
    ios::sync_with_stdio(false);
    cin.tie(nullptr);
    cout.tie(nullptr);
    return 0;
}();

class Solution {
public:
    bool canReorderDoubled(vector<int>& arr) {
        unordered_map<int, int> mp;

        int zero = 0;
        for (int i = 0; i < arr.size(); ++i) {
            if (arr[i] != 0) {
                mp[arr[i]]++;
            } else {
                zero++;
            }
        }

        if (zero % 2 != 0) {
            return false;
        }

        priority_queue<pair<int, int>, vector<pair<int, int>>,
                       decltype([](pair<int, int> a, pair<int, int> b) {
                           return a.first > b.first;
                       })>
            pq1; // small number at the top

        priority_queue<pair<int, int>, vector<pair<int, int>>,
                       decltype([](pair<int, int> a, pair<int, int> b) {
                           return a.first < b.first;
                       })>
            pq2; // large number at the top

        for (auto iter = mp.begin(); iter != mp.end(); ++iter) {
            auto key = iter->first;
            auto freq = iter->second;

            if (key > 0) {
                pq1.push({key, freq});
            } else {
                pq2.push({key, freq});
            }
        }

        auto check_pq1 = [&](int key, int freq) {
            vector<pair<int, int>> vec;
            bool found = false;
            while (!pq1.empty()) {
                auto [k, f] = pq1.top();
                pq1.pop();

                if (k != 2 * key) {
                    vec.push_back({k, f});
                } else {
                    found = true;
                    if (f < freq) {
                        return false;
                    }
                    f -= freq;
                    if (f != 0) {
                        vec.push_back({k, f});
                    }
                    break;
                }
            }

            for (auto& [f, k] : vec) {
                pq1.push({f, k});
            }

            if (!found) {
                return false;
            }
            return true;
        };

        auto check_pq2 = [&](int key, int freq) {
            vector<pair<int, int>> vec;
            bool found = false;
            while (!pq2.empty()) {
                auto [k, f] = pq2.top();
                pq2.pop();

                if (k != 2 * key) {
                    vec.push_back({k, f});
                } else {
                    found = true;
                    if (f < freq) {
                        return false;
                    }
                    f -= freq;
                    if (f != 0) {
                        vec.push_back({k, f});
                    }
                    break;
                }
            }

            for (auto& [f, k] : vec) {
                pq2.push({f, k});
            }

            if (!found) {
                return false;
            }
            return true;
        };

        while (!pq1.empty()) {
            auto [key, freq] = pq1.top();
            pq1.pop();

            if (!check_pq1(key, freq)) {
                return false;
            }
        }

        while (!pq2.empty()) {
            auto [key, freq] = pq2.top();
            pq2.pop();

            if (!check_pq2(key, freq)) {
                return false;
            }
        }

        return true;
    }
};
63600