package leetcode;

/**
 * https://leetcode.com/problems/array-of-doubled-pairs/
 */
public class Problem954 {
    public boolean canReorderDoubled(int[] A) {
        // TODO
        for (int i = 0; i < A.length / 2; i++) {
            System.out.println(i + ": " + A[2 * i + 1] + " --> " + (2 * A[2 * i]));
        }
        return false;
    }

    public static void main(String[] args) {
        Problem954 prob = new Problem954();
//        System.out.println(prob.canReorderDoubled(new int[]{3, 1, 3, 6})); // false
//        System.out.println(prob.canReorderDoubled(new int[]{2, 1, 2, 6})); // false
//        System.out.println(prob.canReorderDoubled(new int[]{4, -2, 2, -4})); // true
        System.out.println(prob.canReorderDoubled(new int[]{-2, -4, 2, 4})); // true
//        System.out.println(prob.canReorderDoubled(new int[]{1, 2, 4, 16, 8, 4})); // false
    }
}

package leetcode;

import java.util.HashMap;
import java.util.Map;
import java.util.Set;

/**
 * https://leetcode.com/problems/array-of-doubled-pairs/
 */
public class Problem954 {
    public boolean canReorderDoubled(int[] A) {
        Map<Integer, Integer> map = new HashMap<>();
        for (int a : A) {
            if (!map.containsKey(a)) {
                map.put(a, 1);
            } else {
                map.put(a, map.get(a) + 1);
            }
        }
        while (!map.isEmpty()) {
            Set<Integer> keys = map.keySet();
            if (!keys.isEmpty()) {
                Integer key1 = keys.iterator().next();
                Integer count1 = map.get(key1);
                count1--;
                if (count1 == 0) {
                    map.remove(key1);
                } else {
                    map.put(key1, count1);
                }
                Integer key2 = key1 * 2;
                Integer count2 = map.get(key2);
                if (count2 == null) {
                    if (key1 % 2 != 0) {
                        return false;
                    }
                    key2 = key1 / 2;
                    count2 = map.get(key2);
                    if (count2 == null) {
                        return false;
                    }
                }
                count2--;
                if (count2 == 0) {
                    map.remove(key2);
                } else {
                    map.put(key2, count2);
                }
            }
        }
        return true;
    }
}

Solve problem 954