Write a function threeSum(nums)
that takes in an array of integers nums
and returns a list of unique triplets [a, b, c]
such that a + b + c = 0
. The function should return all unique triplets in ascending order.
print(threeSum([-1, 0, 1, 2, -1, -4]))
# Output: [[-1, -1, 2], [-1, 0, 1]]
print(threeSum([]))
# Output: []
print(threeSum([0]))
# Output: []
To solve this problem, we can use the Two Pointers approach along with sorting the given array.
result
, to store the unique triplets that sum up to zero.nums
, using a loop with index variable i
. We iterate until the second-to-last element, as we need at least three elements for a triplet.nums[i]
and skip the current iteration if nums[i]
is equal to nums[i-1]
. This helps us to avoid duplicate triplets.
nums[i]
with its previous element, nums[i-1]
. When i
is 0, there is no previous element, so we skip the duplicate check in that case.nums[i]
, we create two pointers, left
and right
, pointing to the elements on the right side of nums[i]
. The left
pointer starts from i+1
(next element) and the right
pointer starts from the last element of the array.left
is less than right
, we enter a loop and perform the following steps:
nums[i]
, nums[left]
, and nums[right]
.result
list.left
pointer one step to the right, increasing the sum.right
pointer one step to the left, decreasing the sum.left
or right
pointer), we also skip over any duplicate values by incrementing left
until it's greater than its previous index and decrementing right
until it's less than its next index.result
list containing all the unique triplets that sum up to zero.Here's the implementation of the threeSum
function in Python:
def threeSum(nums):
nums.sort() # Sort the input array in ascending order
n = len(nums)
result = []
for i in range(n - 2):
if i > 0 and nums[i] == nums[i - 1]:
continue # Skip duplicate values
left = i + 1
right = n - 1
while left < right:
# Calculate the sum of the three elements
total = nums[i] + nums[left] + nums[right]
if total == 0:
result.append([nums[i], nums[left], nums[right]])
# Skip duplicates
while left < right and nums[left] == nums[left + 1]:
left += 1
while left < right and nums[right] == nums[right - 1]:
right -= 1
# Move the pointers
left += 1
right -= 1
elif total < 0:
left += 1 # Increment left pointer for a smaller sum
else:
right -= 1 # Decrement right pointer for a larger sum
return result
The time complexity of this solution is O(n^2), where n is the length of the nums array. Sorting the array takes O(n log n) time, and the two-pointer approach takes O(n) time.