Laki
Laki Technology enthusiast

Leetcode problem 15 - 3Sum | Problem and solution

Leetcode problem 15 - 3Sum | Problem and solution

3Sum

Description

Given an integer array nums, return all the triplets [nums[i], nums[j], nums[k]] such that i != j, i != k, and j != k, and nums[i] + nums[j] + nums[k] == 0.

Notice that the solution set must not contain duplicate triplets.

Example 1:

1
2
3
4
5
6
7
8
Input: nums = [-1,0,1,2,-1,-4]
Output: [[-1,-1,2],[-1,0,1]]
Explanation: 
nums[0] + nums[1] + nums[2] = (-1) + 0 + 1 = 0.
nums[1] + nums[2] + nums[4] = 0 + 1 + (-1) = 0.
nums[0] + nums[3] + nums[4] = (-1) + 2 + (-1) = 0.
The distinct triplets are [-1,0,1] and [-1,-1,2].
Notice that the order of the output and the order of the triplets does not matter.

Example 2:

1
2
3
Input: nums = [0,1,1]
Output: []
Explanation: The only possible triplet does not sum up to 0.

Example 3:

1
2
3
Input: nums = [0,0,0]
Output: [[0,0,0]]
Explanation: The only possible triplet sums up to 0.

Solutions

Let’s walk through the problem-solving process in Java, starting from a basic brute-force solution and gradually improving performance and readability.

✅ Brute Force (Time Complexity: O(n³), Space: O(1))

This approach checks every possible triplet and filters valid ones. It’s not efficient but helps understand the problem structure.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
class Solution {
    public List<List<Integer>> threeSum(int[] nums) {
        Set<List<Integer>> result = new HashSet<>();
        int n = nums.length;

        for (int i = 0; i < n - 2; i++) {
            for (int j = i + 1; j < n - 1; j++) {
                for (int k = j + 1; k < n; k++) {
                    if (nums[i] + nums[j] + nums[k] == 0) {
                        List<Integer> triplet = Arrays.asList(nums[i], nums[j], nums[k]);
                        Collections.sort(triplet);
                        result.add(triplet);
                    }
                }
            }
        }

        return new ArrayList<>(result);
    }
}

This solution works but is too slow for large input sizes. Let’s do better.

🚀 Optimized Using Sorting + Two Pointers (Time: O(n²), Space: O(n) for result)

We sort the array and fix one number, then use two pointers to find pairs that complete the triplet. This is the most commonly accepted optimal approach.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
class Solution {
    public List<List<Integer>> threeSum(int[] nums) {
        List<List<Integer>> res = new ArrayList<>();
        Arrays.sort(nums);

        for (int i = 0; i < nums.length - 2; i++) {
            if (i > 0 && nums[i] == nums[i - 1]) continue; // skip duplicates

            int j = i + 1;
            int k = nums.length - 1;

            while (j < k) {
                int total = nums[i] + nums[j] + nums[k];

                if (total < 0) {
                    j++;
                } else if (total > 0) {
                    k--;
                } else {
                    res.add(Arrays.asList(nums[i], nums[j], nums[k]));
                    j++;
                    k--;

                    while (j < k && nums[j] == nums[j - 1]) j++; // skip duplicates
                    while (j < k && nums[k] == nums[k + 1]) k--; // skip duplicates
                }
            }
        }

        return res;
    }
}

This solution is efficient and avoids duplicates cleanly using two-pointer logic after sorting.

🧩 Alternate with HashSet to Track Triplets (Time: O(n²), Space: O(n²))

We use a Set to track unique triplets and avoid manual duplicate removal.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
class Solution {
    public List<List<Integer>> threeSum(int[] nums) {
        Set<List<Integer>> result = new HashSet<>();
        Arrays.sort(nums);
        int n = nums.length;

        for (int i = 0; i < n - 2; i++) {
            int j = i + 1;
            int k = n - 1;

            while (j < k) {
                int sum = nums[i] + nums[j] + nums[k];

                if (sum == 0) {
                    result.add(Arrays.asList(nums[i], nums[j], nums[k]));
                    j++;
                    k--;
                } else if (sum < 0) {
                    j++;
                } else {
                    k--;
                }
            }
        }

        return new ArrayList<>(result);
    }
}

While this version is straightforward, using Set adds a bit of overhead, and you still sort beforehand. It’s a good balance between simplicity and performance.

Summary

Approach Time Complexity Space Complexity Notes
Brute Force (Backtracking) O(n³) O(1) Simple but slow
Two Pointers O(n) O(m * n) Best mix of speed and clarity
HashSet Version O(n²) O(n²) Avoids duplicate logic manually

comments powered by Disqus