3Sum
Array, Two Pointers
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:
Example 2:
Example 3:
Assumptions:
Each input have multiple solutions.
The same element cannot be used twice.
The answer can be returned in any order.
Questions to Clarify:
Negative Numbers: Can the input array contain negative numbers? Yes.
Duplicate Numbers: Can the input array contain duplicate numbers? Yes.
Unique Solution: Is it guaranteed that there will always be exactly one solution? No.
Multiple Solutions: Should we return all possible solutions, or just one? All possible unique solutions.
Indices Required: Do we need to return the actual indices of the solution numbers, or just the numbers themselves? Actual indices of the solution numbers.
Additional Considerations:
Array Size: What are the expected size limits for the input array?
Time and Space Complexity: Are there any specific time or space complexity requirements for the solution?
Algorithm Choice: Would a specific algorithm (e.g., hash table-based or sorting-based) be preferred?
Solutions
Check sum of three elements of an array.
Approach – Brute Force Technique
Run two nested loops to check sum of three elements is equal to 0
. Also avoid duplicate solution.
Steps
Sort the array: The algorithm starts by sorting the input array. This is done to allow us to skip over duplicate values in the array, which ensures that the solution set does not contain duplicate triplets.
Iterate over the array: The algorithm then iterates over the sorted array with three nested loops, each starting from a different index. The outer loop (
i
) goes from0
tonums.Length - 2
, the middle loop (j
) goes fromi + 1
tonums.Length - 1
, and the inner loop (k
) goes fromj + 1
tonums.Length
.Skip duplicates: At the start of each loop, the algorithm checks if the current number is the same as the previous one. If it is, the algorithm skips to the next iteration. This is done to avoid adding duplicate triplets to the result.
Check for zero sum: For each triplet (
nums[i]
,nums[j]
,nums[k]
), the algorithm checks if their sum is zero. If it is, the triplet is added to the result list.Return the result: After all triplets have been checked, the algorithm returns the list of triplets that sum to zero.
Complexity
Time Complexity: O(n^3)
Auxiliary Space: O(1)
This algorithm works by checking every possible triplet in the array. Because of the sorting and the check for duplicates, each triplet in the result is unique. However, because it uses three nested loops, its time complexity is O(n^3), which means it can be slow for large inputs.
Approach – Two Poniters Technique
As we know that two-poiner approach is good if if array in sorted order. here's a step-by-step explanation of the optimized algorithm:
Steps
Sort the array: The algorithm starts by sorting the input array. This is done to allow us to skip over duplicate values in the array, which ensures that the solution set does not contain duplicate triplets.
Iterate over the array: The algorithm then iterates over the sorted array with a single loop (
i
), which goes from0
tonums.Length - 2
.Skip duplicates: At the start of the loop, the algorithm checks if the current number is the same as the previous one. If it is, the algorithm skips to the next iteration. This is done to avoid adding duplicate triplets to the result.
Initialize two pointers: For each
i
, the algorithm initializes two pointers,low
andhigh
.low
is initialized toi + 1
andhigh
is initialized tonums.Length - 1
. The variablesum
is initialized to0 - nums[i]
.Move the pointers: While
low
is less thanhigh
, the algorithm checks the sum ofnums[low]
andnums[high]
. If their sum is equal tosum
, it means we have found a triplet that sums to zero, so it adds the triplet to the result list, incrementslow
, and decrementshigh
. If their sum is less thansum
, it incrementslow
, and if their sum is greater thansum
, it decrementshigh
.Skip duplicates: After finding a triplet, the algorithm again checks for duplicates by comparing
nums[low]
andnums[high]
with their adjacent elements. Ifnums[low]
is the same asnums[low + 1]
, it incrementslow
. Ifnums[high]
is the same asnums[high - 1]
, it decrementshigh
.Return the result: After all triplets have been checked, the algorithm returns the list of triplets that sum to zero.
Complexity
Time Complexity: O(n^2)
Auxiliary Space: O(1)
Last updated