public int threeSumClosest(int[] num, int target) {
        Arrays.sort(num);

        int n = num.length;
        if(n < 3) {
            int sum = 0;
            for(int i : num) {
                sum+=i;
            }
            return sum;
        }

        int closest = 0;
        for(int i=0; i<3; i++){
            closest+=num[i];
        }

        for(int i=0; i<=n-3; i++) {
            int a = num[i];
            int start = i+1;
            int end = n-1;
            while(start < end) {
                int b = num[start];
                int c = num[end];

                if(a+b+c==target) {
                    return target;
                } else if(a+b+c > target) {
                    end--;
                } else {
                    start ++;
                }

                if(Math.abs(a + b + c - target) < Math.abs(closest-target)) {
                    closest = a + b + c;
                }
            }
        }

        return closest;
    }