package com.fishercoder.solutions;

import java.util.Arrays;

public class _16 {

    public static class Solution1 {
        public int threeSumClosest(int[] nums, int target) {
            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 < len - 2; i++) {
                int left = i + 1;
                int right = len - 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;
        }
    }

}

@@ -207,226 +207,8 @@
 s);%0A
-            int len = nums.length;%0A            if (len %3C 3) %7B%0A                int sum = 0;%0A                for (int i : nums) %7B%0A                    sum += i;%0A                %7D%0A                return sum;%0A            %7D%0A
     
@@ -286,19 +286,27 @@
  0; i %3C 
-len
+nums.length
  - 2; i+
@@ -372,19 +372,27 @@
 right = 
-len
+nums.length
  - 1;%0A  
@@ -536,23 +536,23 @@
 bs(t
-arget - thisSum
+hisSum - target
 ) %3C 
@@ -564,20 +564,20 @@
 abs(
+sum - 
 target
- - sum
 )) %7B

refactor 16