public int threeSumClosest(int[] num, int target) {
        Arrays.sort(num);
        if (num.length < 3) throw new IllegalArgumentException("No two sum solution");
        int closestsum = target - 1000000;

        for (int i = 0; i < num.length - 2; i++) {
            if (i >= 1 && num[i] == num[i - 1]) continue;
            int j = i + 1, k = num.length - 1;
            while (j < k) {
                int sum = num[i] + num[j] + num[k];
                if (abs(sum - target) < abs(closestsum - target))
                    closestsum = sum;
                if (sum > target)
                    k--;
                else j++;
            }
        }
        return closestsum;
    }