package leetcode;

import java.util.Arrays;

/**
 * https://leetcode.com/problems/fair-candy-swap/
 */
public class Problem888 {
    public int[] fairCandySwap(int[] A, int[] B) {
        int sumA = 0;
        for (int a : A) {
            sumA += a;
        }
        int sumB = 0;
        for (int b : B) {
            sumB += b;
        }
        int[] result = new int[2];
        outer:
        for (int a : A) {
            for (int b : B) {
                if (sumA - a + b == sumB - b + a) {
                    result[0] = a;
                    result[1] = b;
                    break outer;
                }
            }
        }
        return result;
    }

    public static void main(String[] args) {
        Problem888 prob = new Problem888();
        System.out.println(Arrays.toString(prob.fairCandySwap(
            new int[]{1, 1}, new int[]{2, 2}))); // [1,2]
        System.out.println(Arrays.toString(prob.fairCandySwap(
            new int[]{1, 2}, new int[]{2, 3}))); // [1,2]
        System.out.println(Arrays.toString(prob.fairCandySwap(
            new int[]{2}, new int[]{1, 3}))); // [2,3]
        System.out.println(Arrays.toString(prob.fairCandySwap(
            new int[]{1, 2, 5}, new int[]{2, 4}))); // [5,4]
    }
}

@@ -29,22 +29,45 @@
 va.util.
-Arrays
+HashSet;%0Aimport java.util.Set
 ;%0A%0A/**%0A 
@@ -269,32 +269,77 @@
 += a;%0A        %7D%0A
+        Set%3CInteger%3E setB = new HashSet%3C%3E();%0A
         int sumB
@@ -405,43 +405,23 @@
     
-%7D%0A
     
-    int%5B%5D result = new int%5B2%5D
+setB.add(b)
 ;%0A  
@@ -430,14 +430,9 @@
     
-outer:
+%7D
 %0A   
@@ -440,76 +440,31 @@
     
-for (int a : A) %7B%0A            for (int b : B) %7B%0A                if (
+// Formula:%0A        // 
 sumA
@@ -473,17 +473,16 @@
  a + b =
-=
  sumB - 
@@ -486,19 +486,16 @@
  - b + a
-) %7B
 %0A       
@@ -499,53 +499,79 @@
     
-            result%5B0%5D = a;%0A          
+// 2b = sumB - sumA + 2a%0A        // b = (sumB - sumA + 2a) / 2%0A
         
   re
@@ -570,57 +570,49 @@
     
- 
+int%5B%5D
  result
-%5B1%5D = b;%0A                    break
+ = new int%5B2%5D;%0A       
  outer
-;
+:
 %0A   
@@ -620,644 +620,242 @@
     
-        %7D%0A            %7D%0A        %7D%0A        return result;%0A
+for (int a : A) %7B%0A            int b = (sumB - sumA + (2 * a)) / 2;%0A    
     
-%7D%0A%0A
     
-public static void main(String%5B%5D args) %7B%0A        Problem888 prob = new Problem888();%0A        System.out.println(Arrays.toString(prob.fairCandySwap(%0A            new int%5B%5D%7B1, 1%7D, new int%5B%5D%7B2, 2%7D))); // %5B1,2%5D%0A        System.out.println(Arrays.toString(prob.fairCandySwap(%0A            new int%5B%5D%7B1, 2%7D, new int%5B%5D%7B2, 3%7D))); // %5B1,2%5D%0A        System.out.println(Arrays.toString(prob.fairCandySwap(%0A            new int%5B%5D%7B2%7D, new int%5B%5D%7B1, 3%7D))); // %5B2,3%5D%0A        System.out.println(Arrays.toString(prob.fairCandySwap(%0A            new int%5B%5D%7B1, 2, 5%7D, new int%5B%5D%7B2, 4%7D))); // %5B5,4%5D
+if (setB.contains(b)) %7B%0A                result%5B0%5D = a;%0A                result%5B1%5D = b;%0A                break;%0A            %7D%0A        %7D%0A        return result;
 %0A   

Solve problem 888