package com.fishercoder.solutions;

import java.util.Arrays;

/**
 * Given an array S of n integers, find three integers in S such that the sum is closest to a given number, target. Return the sum of the three integers. You may assume that each input would have exactly one solution.
 * For example, given array S = {-1 2 1 -4}, and target = 1.
 * The sum that is closest to the target is 2. (-1 + 2 + 1 = 2).
 */

public class _16 {

    public int threeSumClosest(int[] nums, int target) {
        if (nums == null || nums.length == 0) {
            return 0;
        }
        Arrays.sort(nums);
        int len = nums.length;
        if (len < 3) {
            int sum = 0;
            for (int i : nums) {
                sum += i;
            }
            return sum;
        }
        int sum = nums[0] + nums[1] + nums[2];
        for (int i = 0; i < nums.length - 2; i++) {
            int left = i + 1;
            int right = nums.length - 1;
            while (left < right) {
                int thisSum = nums[i] + nums[left] + nums[right];
                if (Math.abs(target - thisSum) < Math.abs(target - sum)) {
                    sum = thisSum;
                    if (sum == target) {
                        return sum;
                    }
                } else if (target > thisSum) {
                    left++;
                } else {
                    right--;
                }
            }
        }
        return sum;
    }

}

@@ -59,16 +59,39 @@
 s;%0A%0A/**%0A
+ * 16. 3Sum Closest%0A *%0A
  * Given
@@ -197,16 +197,19 @@
  target.
+%0A *
  Return 
@@ -454,16 +454,56 @@
  _16 %7B%0A%0A
+    public static class Solution1 %7B%0A    
     publ
@@ -555,24 +555,28 @@
 ) %7B%0A        
+   
+ 
 if (nums == 
@@ -611,24 +611,28 @@
             
+    
 return 0;%0A  
@@ -629,34 +629,42 @@
 turn 0;%0A        
+ 
-%7D%0A
+   %7D%0A    
         Arrays.s
@@ -678,24 +678,28 @@
 s);%0A        
+    
 int len = nu
@@ -713,24 +713,28 @@
 th;%0A        
+  
+  
 if (len %3C 3)
@@ -732,24 +732,28 @@
 (len %3C 3) %7B%0A
+    
             
@@ -769,32 +769,36 @@
  0;%0A            
+    
 for (int i : num
@@ -814,24 +814,28 @@
             
+  
+  
 sum += i;%0A  
@@ -836,32 +836,36 @@
  i;%0A            
+    
 %7D%0A            re
@@ -854,32 +854,36 @@
   %7D%0A            
+ 
+   
 return sum;%0A    
@@ -878,34 +878,42 @@
 rn sum;%0A        
+   
+ 
 %7D%0A
+    
         int sum 
@@ -943,32 +943,36 @@
 ums%5B2%5D;%0A        
+ 
+   
 for (int i = 0; 
@@ -1007,24 +1007,28 @@
             
+   
+ 
 int left = i
@@ -1029,24 +1029,28 @@
 ft = i + 1;%0A
+    
             
@@ -1090,16 +1090,20 @@
         
+  
+  
 while (l
@@ -1113,24 +1113,28 @@
  %3C right) %7B%0A
+    
             
@@ -1199,24 +1199,28 @@
             
+    
 if (Math.abs
@@ -1282,24 +1282,28 @@
             
+    
 sum = thisSu
@@ -1301,24 +1301,28 @@
  = thisSum;%0A
+    
             
@@ -1366,32 +1366,36 @@
                 
+    
 return sum;%0A    
@@ -1402,34 +1402,42 @@
                 
-%7D%0A
+    %7D%0A    
                 
@@ -1491,16 +1491,20 @@
     
+ 
+   
 left++;%0A
     
@@ -1499,16 +1499,20 @@
 left++;%0A
+    
         
@@ -1548,16 +1548,20 @@
         
+    
 right--;
@@ -1569,34 +1569,42 @@
                 
+ 
-%7D%0A
+   %7D%0A    
             %7D%0A  
@@ -1609,26 +1609,32 @@
         
-%7D%0A
+    %7D%0A  
         
 return s
@@ -1617,32 +1617,34 @@
     %7D%0A          
+  
 return sum;%0A    
@@ -1635,21 +1635,31 @@
 return sum;%0A
+        %7D%0A
     %7D%0A%0A%7D%0A

refactor 16