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
    }
}

@@ -12,16 +12,87 @@
 tcode;%0A%0A
+import java.util.HashMap;%0Aimport java.util.Map;%0Aimport java.util.Set;%0A%0A
 /**%0A * h
@@ -234,146 +234,146 @@
     
-// TODO%0A        for (int i = 0; i %3C A.length / 2; i++) %7B%0A            System.out.println(i + %22: %22 + A%5B2 * i + 1%5D + %22 --%3E %22 + (2 * A%5B2 * i%5D)
+Map%3CInteger, Integer%3E map = new HashMap%3C%3E();%0A        for (int a : A) %7B%0A            if (!map.containsKey(a)) %7B%0A                map.put(a, 1
 );%0A 
@@ -383,564 +383,1149 @@
     
+ 
-%7D%0A        return false;%0A    %7D%0A%0A    public static void main(String%5B%5D args) %7B%0A        Problem954 prob = new Problem954();%0A//        System.out.println(prob.canReorderDoubled(new int%5B%5D%7B3, 1, 3, 6%7D)); // false%0A//        System.out.println(prob.canReorderDoubled(new int%5B%5D%7B2, 1, 2, 6%7D)); // false%0A//        System.out.println(prob.canReorderDoubled(new int%5B%5D%7B4, -2, 2, -4%7D)); // true%0A        System.out.println(prob.canReorderDoubled(new int%5B%5D%7B-2, -4, 2, 4%7D)); // true%0A//        System.out.println(prob.canReorderDoubled(new int%5B%5D%7B1, 2, 4, 16, 8, 4%7D)); // false
+   %7D else %7B%0A                map.put(a, map.get(a) + 1);%0A            %7D%0A        %7D%0A        while (!map.isEmpty()) %7B%0A            Set%3CInteger%3E keys = map.keySet();%0A            if (!keys.isEmpty()) %7B%0A                Integer key1 = keys.iterator().next();%0A                Integer count1 = map.get(key1);%0A                count1--;%0A                if (count1 == 0) %7B%0A                    map.remove(key1);%0A                %7D else %7B%0A                    map.put(key1, count1);%0A                %7D%0A                Integer key2 = key1 * 2;%0A                Integer count2 = map.get(key2);%0A                if (count2 == null) %7B%0A                    if (key1 %25 2 != 0) %7B%0A                        return false;%0A                    %7D%0A                    key2 = key1 / 2;%0A                    count2 = map.get(key2);%0A                    if (count2 == null) %7B%0A                        return false;%0A                    %7D%0A                %7D%0A                count2--;%0A                if (count2 == 0) %7B%0A                    map.remove(key2);%0A                %7D else %7B%0A                    map.put(key2, count2);%0A                %7D%0A            %7D%0A        %7D%0A        return true;
 %0A   

Solve problem 954