From f44509b5c45fe76f2620b22766c35d1da89f9928 Mon Sep 17 00:00:00 2001 From: alekh08 <137406174+alekh08@users.noreply.github.com> Date: Sun, 23 Feb 2025 17:34:29 +0530 Subject: [PATCH 1/4] Added Two pointer approach. --- .../arrays/find_triplets_with_0_sum.py | 75 +++++++++++++++++++ 1 file changed, 75 insertions(+) diff --git a/data_structures/arrays/find_triplets_with_0_sum.py b/data_structures/arrays/find_triplets_with_0_sum.py index 52e521906873..7d598c088483 100644 --- a/data_structures/arrays/find_triplets_with_0_sum.py +++ b/data_structures/arrays/find_triplets_with_0_sum.py @@ -81,7 +81,82 @@ def find_triplets_with_0_sum_hashing(arr: list[int]) -> list[list[int]]: return output_arr +# Two pointer approach to find triplets with zero sum + +""" +Why is it the best? +1. Faster than brute force due to sorting and then two pointers scanning. + +2. No extra space needed (except for sorting). + +3. Handles Duplicate efficiently by skipping repeated values. +""" +def find_triplets_with_0_sum_two_pointer(arr: list[int]) -> list[list[int]]: + """ + Function for finding the triplets with a given sum in the array using hashing. + + Given a list of integers, return elements a, b, c such that a + b + c = 0. + + Args: + nums: list of integers + Returns: + list of lists of integers where sum(each_list) == 0 + Examples: + >>> find_triplets_with_0_sum_two_pointer([-1, 0, 1, 2, -1, -4]) + [[-1, 0, 1], [-1, -1, 2]] + >>> find_triplets_with_0_sum_two_pointer([]) + [] + >>> find_triplets_with_0_sum_two_pointer([0, 0, 0]) + [[0, 0, 0]] + >>> find_triplets_with_0_sum_two_pointer([1, 2, 3, 0, -1, -2, -3]) + [[-1, 0, 1], [-3, 1, 2], [-2, 0, 2], [-2, -1, 3], [-3, 0, 3]] + + Time complexity: O(N^2) + Auxiliary Space: O(1) (excluding output storage) + + """ + + # Step 1: Sort the array to facilitate the two pointer approach + nums.sort() + triplets = [] # list to store the valid triplets + n = len(nums) + + # Step 2: iterate through the array, fixing one number at a time + for i in range(n - 2): # We need atleast 3 elements for a triplet + # skip duplicate elements for the first number + if i > 0 and nums[i] == nums[i - 1]: + continue #Move to the distinct number + + # Step 3: Use the two pointer technique to find pairs that sum up to -nums[i] + left, right = i + 1, n - 1 # Initialize two pointers + while left < right: + total = nums[i] + nums[left] + nums[right] #Calculate sum of three numbers + + if total == 0: + # If the sum is zero, we found a valid triplet + triplets.append([nums[i], nums[left], nums[right]]) + + # Move both pointers to look for the next unique pair + left += 1 + right -= 1 + + # skip duplicate values for the second and third numbers + while left < right and nums[left] == nums[left - 1]: + left += 1 + while left < right and nums[right] == nums[right + 1]: + right -= 1 + + elif total < 0: + # If sum is less than zero, move the left pointer to the right + left += 1 + else: + # If sum is greater than zero, move the right pointer to the left + right -= 1 + return triplets + + if __name__ == "__main__": from doctest import testmod testmod() + From ad7f983149ea475cc5951687a2a98b49a6e78f36 Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Sun, 23 Feb 2025 12:10:02 +0000 Subject: [PATCH 2/4] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- data_structures/arrays/find_triplets_with_0_sum.py | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/data_structures/arrays/find_triplets_with_0_sum.py b/data_structures/arrays/find_triplets_with_0_sum.py index 7d598c088483..5ebc42d6ca7f 100644 --- a/data_structures/arrays/find_triplets_with_0_sum.py +++ b/data_structures/arrays/find_triplets_with_0_sum.py @@ -91,6 +91,8 @@ def find_triplets_with_0_sum_hashing(arr: list[int]) -> list[list[int]]: 3. Handles Duplicate efficiently by skipping repeated values. """ + + def find_triplets_with_0_sum_two_pointer(arr: list[int]) -> list[list[int]]: """ Function for finding the triplets with a given sum in the array using hashing. @@ -122,15 +124,15 @@ def find_triplets_with_0_sum_two_pointer(arr: list[int]) -> list[list[int]]: n = len(nums) # Step 2: iterate through the array, fixing one number at a time - for i in range(n - 2): # We need atleast 3 elements for a triplet + for i in range(n - 2): # We need atleast 3 elements for a triplet # skip duplicate elements for the first number if i > 0 and nums[i] == nums[i - 1]: - continue #Move to the distinct number + continue # Move to the distinct number # Step 3: Use the two pointer technique to find pairs that sum up to -nums[i] left, right = i + 1, n - 1 # Initialize two pointers while left < right: - total = nums[i] + nums[left] + nums[right] #Calculate sum of three numbers + total = nums[i] + nums[left] + nums[right] # Calculate sum of three numbers if total == 0: # If the sum is zero, we found a valid triplet @@ -159,4 +161,3 @@ def find_triplets_with_0_sum_two_pointer(arr: list[int]) -> list[list[int]]: from doctest import testmod testmod() - From 478cc36b433964162bd120c40584255b40da9ca9 Mon Sep 17 00:00:00 2001 From: alekh08 <137406174+alekh08@users.noreply.github.com> Date: Sun, 23 Feb 2025 18:01:21 +0530 Subject: [PATCH 3/4] Refactor: Renamed variable arr to nums for clarity --- data_structures/arrays/find_triplets_with_0_sum.py | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/data_structures/arrays/find_triplets_with_0_sum.py b/data_structures/arrays/find_triplets_with_0_sum.py index 5ebc42d6ca7f..0791fa4188a6 100644 --- a/data_structures/arrays/find_triplets_with_0_sum.py +++ b/data_structures/arrays/find_triplets_with_0_sum.py @@ -91,9 +91,7 @@ def find_triplets_with_0_sum_hashing(arr: list[int]) -> list[list[int]]: 3. Handles Duplicate efficiently by skipping repeated values. """ - - -def find_triplets_with_0_sum_two_pointer(arr: list[int]) -> list[list[int]]: +def find_triplets_with_0_sum_two_pointer(nums: list[int]) -> list[list[int]]: """ Function for finding the triplets with a given sum in the array using hashing. From 54ccb53361a47a18b93a0f0b645881861e3457c2 Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Sun, 23 Feb 2025 12:35:53 +0000 Subject: [PATCH 4/4] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- data_structures/arrays/find_triplets_with_0_sum.py | 2 ++ 1 file changed, 2 insertions(+) diff --git a/data_structures/arrays/find_triplets_with_0_sum.py b/data_structures/arrays/find_triplets_with_0_sum.py index 0791fa4188a6..a5fdda097e60 100644 --- a/data_structures/arrays/find_triplets_with_0_sum.py +++ b/data_structures/arrays/find_triplets_with_0_sum.py @@ -91,6 +91,8 @@ def find_triplets_with_0_sum_hashing(arr: list[int]) -> list[list[int]]: 3. Handles Duplicate efficiently by skipping repeated values. """ + + def find_triplets_with_0_sum_two_pointer(nums: list[int]) -> list[list[int]]: """ Function for finding the triplets with a given sum in the array using hashing.