package leetcode;

/**
 * https://leetcode.com/problems/matchsticks-to-square/
 */
public class Problem473 {
    public boolean makesquare(int[] nums) {
        int sum = 0;
        for (int num : nums) {
            sum += num;
        }
        int length = sum / 4;
        int remainder = sum % 4;
        if (remainder != 0) {
            return false;
        }

        return true;
    }

    private static boolean makeSquare(int[] nums, int length) {
        if (length < 0) {
            return false;
        }
        if (length == 0) {
            return true;
        }
        boolean valid = false;
        for (int i = 0; i < nums.length; i++) {
            valid |= makeSquare(nums, length - nums[i]);
        }
        return valid;
    }

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

@@ -12,16 +12,42 @@
 tcode;%0A%0A
+import java.util.Arrays;%0A%0A
 /**%0A * h
@@ -347,16 +347,35 @@
 der != 0
+ %7C%7C nums.length %3C 4
 ) %7B%0A    
@@ -410,100 +410,218 @@
   %7D%0A
-%0A        return true;%0A    %7D%0A%0A    private static boolean makeSquare(int%5B%5D nums, int 
+        Arrays.sort(nums);%0A        reverse(nums);%0A        return makeSquare(nums, 0, length, length, length, length);%0A    %7D%0A%0A    private static void reverse(int%5B%5D nums) %7B%0A        int i = 0, j = nums.
 length
-) %7B
+ - 1;
 %0A   
@@ -629,22 +629,20 @@
     
-if (length %3C 0
+while (i %3C j
 ) %7B%0A
@@ -657,58 +657,86 @@
     
-return false;%0A        %7D%0A        if (length == 0) %7B
+int temp = nums%5Bi%5D;%0A            nums%5Bi%5D = nums%5Bj%5D;%0A            nums%5Bj%5D = temp;
 %0A   
@@ -748,19 +748,16 @@
     
-return true
+i++; j--
 ;%0A  
@@ -772,79 +772,158 @@
     
+%7D%0A%0A
     
-boolean valid = false;%0A        for (int i = 0; i %3C nums.
+private static boolean makeSquare(int%5B%5D nums, int idx, int length1, int length2, int length3,%0A                                      int 
 length
-; i++
+4
 ) %7B%0A
@@ -934,605 +934,657 @@
     
-    valid %7C= makeSquare(nums, length - nums%5Bi%5D);%0A 
+if (length1 %3C 0 %7C%7C length2 %3C 0 %7C%7C length3 %3C 0 %7C%7C length4 %3C 0) %7B%0A
      
-  %7D%0A
        
- 
 return 
-valid
+false
 ;%0A    
+    
 %7D
-%0A
 %0A    
-public static void main(String%5B%5D args) %7B%0A        Problem473 prob = new Problem473();%0A        System.out.println(prob.makesquare(new int%5B%5D%7B1, 1, 2, 2, 2%7D)); // true%0A        System.out.println(prob.
+    if (idx == nums.length) %7B%0A            if (length1 == 0 && length2 == 0 && length3 == 0 && length4 == 0) %7B%0A                return true;%0A            %7D%0A            return false;%0A        %7D%0A        return 
 make
-s
+S
 quare(n
-ew int%5B%5D%7B3, 3, 3, 3, 4%7D)); // false%0A        System.out.println(prob.
+ums, idx + 1, length1 - nums%5Bidx%5D, length2, length3, length4) %7C%7C%0A            
 make
-s
+S
 quare(n
-ew int%5B%5D%7B3, 3, 3, 3, 3%7D)); // false%0A        System.out.println(prob.
+ums, idx + 1, length1, length2 - nums%5Bidx%5D, length3, length4) %7C%7C%0A            
 make
-s
+S
 quare(n
-ew int%5B%5D%7B1, 1, 1, 1, 1, 1, 1, 1%7D)); // true%0A        System.out.println(prob.
+ums, idx + 1, length1, length2, length3 - nums%5Bidx%5D, length4) %7C%7C%0A            
 make
-s
+S
 quare(n
-ew int%5B%5D%7B3, 5, 4, 4, 6, 2, 1, 1, 1, 5%7D)); // true
+ums, idx + 1, length1, length2, length3, length4 - nums%5Bidx%5D);
 %0A   

Solve problem 473