Skip to content

Commit ad7f983

Browse files
[pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
1 parent f44509b commit ad7f983

File tree

1 file changed

+5
-4
lines changed

1 file changed

+5
-4
lines changed

data_structures/arrays/find_triplets_with_0_sum.py

+5-4
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,8 @@ def find_triplets_with_0_sum_hashing(arr: list[int]) -> list[list[int]]:
9191
9292
3. Handles Duplicate efficiently by skipping repeated values.
9393
"""
94+
95+
9496
def find_triplets_with_0_sum_two_pointer(arr: list[int]) -> list[list[int]]:
9597
"""
9698
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]]:
122124
n = len(nums)
123125

124126
# Step 2: iterate through the array, fixing one number at a time
125-
for i in range(n - 2): # We need atleast 3 elements for a triplet
127+
for i in range(n - 2): # We need atleast 3 elements for a triplet
126128
# skip duplicate elements for the first number
127129
if i > 0 and nums[i] == nums[i - 1]:
128-
continue #Move to the distinct number
130+
continue # Move to the distinct number
129131

130132
# Step 3: Use the two pointer technique to find pairs that sum up to -nums[i]
131133
left, right = i + 1, n - 1 # Initialize two pointers
132134
while left < right:
133-
total = nums[i] + nums[left] + nums[right] #Calculate sum of three numbers
135+
total = nums[i] + nums[left] + nums[right] # Calculate sum of three numbers
134136

135137
if total == 0:
136138
# 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]]:
159161
from doctest import testmod
160162

161163
testmod()
162-

0 commit comments

Comments
 (0)