package com.fishercoder.solutions;

import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;

public class _954 {
    public static class Solution1 {
        public boolean canReorderDoubled(int[] A) {
            for (int i = 0; i < A.length; i++) {
                if (A[i] < 0) {
                    A[i] = -A[i];
                }
            }
            Arrays.sort(A);
            Map<Integer, Integer> map = new HashMap<>();
            for (int num : A) {
                map.put(num, map.getOrDefault(num, 0) + 1);
            }
            for (int num : A) {
                if (num != 0) {
                    if (map.get(num) < 0) {
                        return false;
                    } else if (map.get(num) == 0) {
                        continue;
                    } else {
                        int count = map.get(num);
                        map.put(num, 0);
                        int doubleNum = num * 2;
                        if (!map.containsKey(doubleNum)) {
                            return false;
                        } else {
                            map.put(doubleNum, map.get(doubleNum) - count);
                        }
                    }
                } else if (map.get(num) % 2 != 0) {
                    return false;
                }
            }
            return true;
        }
    }
}

@@ -54,16 +54,45 @@
 Arrays;%0A
+import java.util.Comparator;%0A
 import j
@@ -200,226 +200,157 @@
     
-public boolean canReorderDoubled(int%5B%5D A) %7B%0A            for (int i = 0; i %3C A.length; i++) %7B%0A                if (A%5Bi%5D %3C 0) %7B%0A                    A%5Bi%5D = -A%5Bi%5D;%0A                %7D%0A            %7D%0A            Arrays.sort(A);
+/**%0A         * credit: https://leetcode.com/problems/array-of-doubled-pairs/solution/%0A         */%0A        public boolean canReorderDoubled(int%5B%5D A) %7B
 %0A   
@@ -416,35 +416,33 @@
        for (int 
-num
+i
  : A) %7B%0A        
@@ -457,19 +457,17 @@
 map.put(
-num
+i
 , map.ge
@@ -477,19 +477,17 @@
 Default(
-num
+i
 , 0) + 1
@@ -519,140 +519,131 @@
     
-for (int num : A) %7B%0A                if (num != 0) %7B%0A                    if (map.get(num) %3C 0) %7B%0A                        return false
+Integer%5B%5D sorted = new Integer%5BA.length%5D;%0A            for (int i = 0; i %3C A.length; i++) %7B%0A                sorted%5Bi%5D = A%5Bi%5D
 ;%0A  
@@ -648,32 +648,34 @@
             
+%7D%0A
         
 %7D else if (m
@@ -666,110 +666,106 @@
     
-%7D else if (map.get(num) == 0) %7B%0A                        continue;%0A                    %7D else %7B%0A       
+    Arrays.sort(sorted, Comparator.comparingInt(Math::abs));%0A            for (int num : sorted) %7B%0A
     
@@ -780,21 +780,12 @@
     
- int count = 
+if (
 map.
@@ -792,17 +792,24 @@
 get(num)
-;
+ == 0) %7B
 %0A       
@@ -825,145 +825,110 @@
     
-    map.put(num, 0);%0A                        int doubleNum = num * 2;%0A                        if (!map.containsKey(doubleNum)) %7B%0A        
+continue;%0A                %7D%0A                if (!map.containsKey(2 * num) %7C%7C map.get(2 * num) %3C= 0) %7B%0A
     
@@ -977,36 +977,10 @@
     
-        %7D else %7B%0A           
+%7D%0A
     
@@ -991,25 +991,24 @@
         
- 
 map.put(
 doubleNu
@@ -999,23 +999,17 @@
 map.put(
-doubleN
+n
 um, map.
@@ -1016,26 +1016,16 @@
 get(
-doubleN
+n
 um) - 
-count
+1
 );%0A 
@@ -1043,75 +1043,33 @@
     
-        %7D%0A                    %7D%0A                %7D else if (
+map.put(2 * num, 
 map.get(
 num)
@@ -1068,76 +1068,22 @@
 get(
+2 * 
 num) 
-%25 2 != 0) %7B%0A                    return false;%0A                %7D
+- 1);
 %0A   

refactor 954