package leetcode;

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

/**
 * https://leetcode.com/problems/x-of-a-kind-in-a-deck-of-cards/
 */
public class Problem914 {
    public boolean hasGroupsSizeX(int[] deck) {
        Map<Integer, Integer> map = new HashMap<>();
        for (int val : deck) {
            int count = map.getOrDefault(val, 0) + 1;
            map.put(val, count);
        }
        int min = Integer.MAX_VALUE;
        for (int count : map.values()) {
            min = Math.min(min, count);
            if (min < 2) {
                return false;
            }
        }
        for (int count : map.values()) {
            if (count % min != 0) {
                return false;
            }
        }
        return true;
    }

    public static void main(String[] args) {
        Problem914 prob = new Problem914();
//        System.out.println(prob.hasGroupsSizeX(new int[]{1, 2, 3, 4, 4, 3, 2, 1})); // true
//        System.out.println(prob.hasGroupsSizeX(new int[]{1, 1, 1, 2, 2, 2, 3, 3})); // false
//        System.out.println(prob.hasGroupsSizeX(new int[]{1})); // false
//        System.out.println(prob.hasGroupsSizeX(new int[]{1, 1})); // true
//        System.out.println(prob.hasGroupsSizeX(new int[]{1, 1, 2, 2, 2, 2})); // true
//        System.out.println(prob.hasGroupsSizeX(new int[]{1, 1, 2, 2, 2, 2, 3, 3, 3, 3, 3, 3})); // true
//        System.out.println(prob.hasGroupsSizeX(new int[]{1, 1, 1, 2, 2, 2})); // true
//        System.out.println(prob.hasGroupsSizeX(new int[]{1, 1, 2, 2, 2, 2, 3, 3, 3, 3, 3})); // false
        System.out.println(prob.hasGroupsSizeX(new int[]{1, 1, 1, 1, 2, 2, 2, 2, 2, 2})); // true
//        System.out.println(prob.hasGroupsSizeX(new int[]{1, 1, 1, 2, 2, 2, 2, 2})); // false
    }
}

@@ -405,31 +405,16 @@
 int 
-min = Integer.MAX_VALUE
+gcd = -1
 ;%0A  
@@ -468,63 +468,241 @@
     
-min = Math.min(min, count);%0A            if (min %3C 2) %7B%0A
+if (count %3C 2) %7B%0A                return false;%0A            %7D%0A            if (gcd == -1) %7B%0A                gcd = count;%0A            %7D else if (gcd != count) %7B%0A                gcd = gcd(gcd, count);%0A                if (gcd == 1) %7B%0A    
     
@@ -719,32 +719,50 @@
   return false;%0A
+                %7D%0A
             %7D%0A  
@@ -838,11 +838,11 @@
 t %25 
-min
+gcd
  != 
@@ -937,21 +937,22 @@
    p
-ublic
+rivate
  static 
 void
@@ -951,996 +951,115 @@
 tic 
-void main(String%5B%5D args) %7B%0A        Problem914 prob = new Problem914();%0A//        System.out.println(prob.hasGroupsSizeX(new int%5B%5D%7B1, 2, 3, 4, 4, 3, 2, 1%7D)); // true%0A//        System.out.println(prob.hasGroupsSizeX(new int%5B%5D%7B1, 1, 1, 2, 2, 2, 3, 3%7D)); // false%0A//        System.out.println(prob.hasGroupsSizeX(new int%5B%5D%7B1%7D)); // false%0A//        System.out.println(prob.hasGroupsSizeX(new int%5B%5D%7B1, 1%7D)); // true%0A//        System.out.println(prob.hasGroupsSizeX(new int%5B%5D%7B1, 1, 2, 2, 2, 2%7D)); // true%0A//        System.out.println(prob.hasGroupsSizeX(new int%5B%5D%7B1, 1, 2, 2, 2, 2, 3, 3, 3, 3, 3, 3%7D)); // true%0A//        System.out.println(prob.hasGroupsSizeX(new int%5B%5D%7B1, 1, 1, 2, 2, 2%7D)); // true%0A//        System.out.println(prob.hasGroupsSizeX(new int%5B%5D%7B1, 1, 2, 2, 2, 2, 3, 3, 3, 3, 3%7D)); // false%0A        System.out.println(prob.hasGroupsSizeX(new int%5B%5D%7B1, 1, 1, 1, 2, 2, 2, 2, 2, 2%7D)); // true%0A//        System.out.println(prob.hasGroupsSizeX(new int%5B%5D%7B1, 1, 1, 2, 2, 2, 2, 2%7D)); // false
+int gcd(int a, int b) %7B%0A        if (b == 0) %7B%0A            return a;%0A        %7D%0A        return gcd(b, a %25 b);
 %0A   

Solve problem 914