18. 4Sum

In this problem, we use four pointers to get no repeating tuples, two for iteration and two for shrinking the solution space.

class Solution {
    public List<List<Integer>> fourSum(int[] nums, int target) {
        // corner case
        List<List<Integer>> result = new ArrayList<>();
        if (nums == null || nums.length < 4) {
            return result;
        }

        Arrays.sort(nums);
        int length = nums.length;
        // the range of i is [0, length - 3), 
        // that is because there are always three elements
        // nums[j], nums[left] and nums[right] lining up on the right of nums[i],
        // thus the right excluding endpoint is (length - 3)
        for (int i = 0; i < length - 3; i++) {
            // skips duplicated i
            if (i > 0 && nums[i] == nums[i - 1]) {
                continue;
            }
            // if the minimum of i loops is larger than target, skips the rest
            int iMin = nums[i] + nums[i + 1] + nums[i + 2] + nums[i + 3];
            if (iMin > target) {
                break;
            }
            // if the maximum of i loops is smaller than target, ignore this round
            int iMax = nums[i] + nums[length - 3] + nums[length - 2] + nums[length - 1];
            if (iMax < target) {
                continue;
            }
            // the range of j is [i + 1, length - 2), 
            // that is because there are always two elements
            // nums[left] and nums[right] lining up on the right of nums[j],
            // thus the right excluding endpoint is (length - 2)
            for (int j = i + 1; j < length - 2; j++) {
                if (j > i + 1 && nums[j] == nums[j - 1]) {
                    continue;
                }
                int left = j + 1;
                int right = length - 1;
                // for a fixed nums[i],
                // if the minimum of j loops is larger than target, ignore this round
                int jMin = nums[i] + nums[j] + nums[left] + nums[left + 1];
                if (jMin > target) {
                    continue;
                }
                // for a fixed nums[i],
                // if the maximum of j loops is smaller than target, ignore this round
                int jMax = nums[i] + nums[j] + nums[right - 1] + nums[right];
                if (jMax < target) {
                    continue;
                }
                while (left < right) {
                    int sum = nums[i] + nums[j] + nums[left] + nums[right];
                    if (sum == target) {
                        result.add(
                            Arrays.asList(nums[i], nums[j], nums[left], nums[right]));
                        while (left < right && nums[left] == nums[left - 1]) {
                            left++;
                        }
                        while (left < right && nums[right] == nums[right + 1]) {
                            right--;
                        }
                        left++;
                        right--;
                    } else if (sum > target) {
                        right--;
                    } else {
                        left++;
                    }
                }
            }
        }
        return result;
    }
}