File tree 1 file changed +5
-4
lines changed
1 file changed +5
-4
lines changed Original file line number Diff line number Diff line change @@ -91,6 +91,8 @@ def find_triplets_with_0_sum_hashing(arr: list[int]) -> list[list[int]]:
91
91
92
92
3. Handles Duplicate efficiently by skipping repeated values.
93
93
"""
94
+
95
+
94
96
def find_triplets_with_0_sum_two_pointer (arr : list [int ]) -> list [list [int ]]:
95
97
"""
96
98
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]]:
122
124
n = len (nums )
123
125
124
126
# 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
126
128
# skip duplicate elements for the first number
127
129
if i > 0 and nums [i ] == nums [i - 1 ]:
128
- continue # Move to the distinct number
130
+ continue # Move to the distinct number
129
131
130
132
# Step 3: Use the two pointer technique to find pairs that sum up to -nums[i]
131
133
left , right = i + 1 , n - 1 # Initialize two pointers
132
134
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
134
136
135
137
if total == 0 :
136
138
# 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]]:
159
161
from doctest import testmod
160
162
161
163
testmod ()
162
-
You can’t perform that action at this time.
0 commit comments