Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Master #12589

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open

Master #12589

Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
75 changes: 75 additions & 0 deletions data_structures/arrays/find_triplets_with_0_sum.py
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,82 @@
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]]:

Check failure on line 94 in data_structures/arrays/find_triplets_with_0_sum.py

View workflow job for this annotation

GitHub Actions / ruff

Ruff (ARG001)

data_structures/arrays/find_triplets_with_0_sum.py:94:42: ARG001 Unused function argument: `arr`
"""
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()

Check failure on line 120 in data_structures/arrays/find_triplets_with_0_sum.py

View workflow job for this annotation

GitHub Actions / ruff

Ruff (F821)

data_structures/arrays/find_triplets_with_0_sum.py:120:5: F821 Undefined name `nums`
triplets = [] # list to store the valid triplets
n = len(nums)

Check failure on line 122 in data_structures/arrays/find_triplets_with_0_sum.py

View workflow job for this annotation

GitHub Actions / ruff

Ruff (F821)

data_structures/arrays/find_triplets_with_0_sum.py:122:13: F821 Undefined name `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]:

Check failure on line 127 in data_structures/arrays/find_triplets_with_0_sum.py

View workflow job for this annotation

GitHub Actions / ruff

Ruff (F821)

data_structures/arrays/find_triplets_with_0_sum.py:127:22: F821 Undefined name `nums`

Check failure on line 127 in data_structures/arrays/find_triplets_with_0_sum.py

View workflow job for this annotation

GitHub Actions / ruff

Ruff (F821)

data_structures/arrays/find_triplets_with_0_sum.py:127:33: F821 Undefined name `nums`
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

Check failure on line 133 in data_structures/arrays/find_triplets_with_0_sum.py

View workflow job for this annotation

GitHub Actions / ruff

Ruff (F821)

data_structures/arrays/find_triplets_with_0_sum.py:133:21: F821 Undefined name `nums`

Check failure on line 133 in data_structures/arrays/find_triplets_with_0_sum.py

View workflow job for this annotation

GitHub Actions / ruff

Ruff (F821)

data_structures/arrays/find_triplets_with_0_sum.py:133:31: F821 Undefined name `nums`

Check failure on line 133 in data_structures/arrays/find_triplets_with_0_sum.py

View workflow job for this annotation

GitHub Actions / ruff

Ruff (F821)

data_structures/arrays/find_triplets_with_0_sum.py:133:44: F821 Undefined name `nums`

Check failure on line 133 in data_structures/arrays/find_triplets_with_0_sum.py

View workflow job for this annotation

GitHub Actions / ruff

Ruff (E501)

data_structures/arrays/find_triplets_with_0_sum.py:133:89: E501 Line too long (91 > 88)

if total == 0:
# If the sum is zero, we found a valid triplet
triplets.append([nums[i], nums[left], nums[right]])

Check failure on line 137 in data_structures/arrays/find_triplets_with_0_sum.py

View workflow job for this annotation

GitHub Actions / ruff

Ruff (F821)

data_structures/arrays/find_triplets_with_0_sum.py:137:34: F821 Undefined name `nums`

# 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()

Loading