package leetcode;

import java.util.Arrays;

/**
 * https://leetcode.com/problems/equal-sum-arrays-with-minimum-number-of-operations/
 */
public class Problem1775 {
    public int minOperations(int[] nums1, int[] nums2) {
        if (nums1.length < nums2.length && 6 * nums1.length < nums2.length) {
            return -1;
        } else if (nums1.length > nums2.length && nums1.length > 6 * nums2.length) {
            return -1;
        }
        int sum1 = 0;
        for (int num : nums1) {
            sum1 += num;
        }
        int sum2 = 0;
        for (int num : nums2) {
            sum2 += num;
        }
        int answer = 0;
        if (sum1 < sum2) {
            Arrays.sort(nums1);
            for (int num : nums1) {
                sum1 += 6 - num;
                answer++;
                if (sum1 >= sum2) {
                    break;
                }
            }
        } else if (sum1 > sum2) {
            Arrays.sort(nums2);
            for (int num : nums2) {
                sum2 += 6 - num;
                answer++;
                if (sum2 >= sum1) {
                    break;
                }
            }
        }
        return answer;
    }

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

@@ -624,538 +624,79 @@
     
-int answer = 0;%0A        if (sum1 %3C sum2) %7B%0A            Arrays.sort(nums1);%0A            for (int num : nums1) %7B%0A                sum1 += 6 - num;%0A                answer++;%0A                if (sum1 %3E= sum2) %7B%0A                    break;%0A                %7D%0A            %7D%0A        %7D else if (sum1 %3E sum2) %7B%0A            Arrays.sort(nums2);%0A            for (int num : nums2) %7B%0A                sum2 += 6 - num;%0A                answer++;%0A                if (sum2 %3E= sum1) %7B%0A                    break;%0A                %7D%0A            %7D%0A        %7D
+Arrays.sort(nums1);%0A        Arrays.sort(nums2);%0A        int answer = 0;
 %0A   
@@ -813,16 +813,18 @@
 1775();%0A
+//
         
@@ -912,24 +912,26 @@
 ,2%7D)); // 3%0A
+//
         Syst
@@ -1012,16 +1012,18 @@
 ; // -1%0A
+//
         
@@ -1097,16 +1097,396 @@
 ); // 3%0A
+//        System.out.println(prob.minOperations(new int%5B%5D%7B6,6,5%7D, new int%5B%5D%7B1,1%7D)); // 3%0A//        System.out.println(prob.minOperations(new int%5B%5D%7B1,1%7D, new int%5B%5D%7B6,6,5%7D)); // 3%0A//        System.out.println(prob.minOperations(new int%5B%5D%7B6,6,5%7D, new int%5B%5D%7B1,1,1%7D)); // 3%0A        System.out.println(prob.minOperations(new int%5B%5D%7B5,6,4,3,1,2%7D, new int%5B%5D%7B6,3,3,1,4,5,3,4,1,3,4%7D)); // 4%0A
     %7D%0A%7D%0A

Update problem 1775