-
-
Notifications
You must be signed in to change notification settings - Fork 46.6k
Data structures/arrays/triplet sum #11134
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
Changes from 5 commits
343c851
bb75172
15aaa77
b7208c0
86c9285
ec8f30d
04c64b7
02a3f16
ee15c34
8c4b8a6
5ca6e1f
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
@@ -1,7 +1,7 @@ | ||||||
from itertools import combinations | ||||||
|
||||||
|
||||||
def find_triplets_with_0_sum(nums: list[int]) -> list[list[int]]: | ||||||
def find_triplets_with_0_sum(nums: list) -> list: | ||||||
""" | ||||||
Given a list of integers, return elements a, b, c such that a + b + c = 0. | ||||||
Args: | ||||||
|
@@ -22,3 +22,60 @@ def find_triplets_with_0_sum(nums: list[int]) -> list[list[int]]: | |||||
list(x) | ||||||
for x in sorted({abc for abc in combinations(sorted(nums), 3) if not sum(abc)}) | ||||||
] | ||||||
|
||||||
|
||||||
def find_triplets_with_0_sum_hashing(arr: list) -> list: | ||||||
""" | ||||||
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_hashing([-1, 0, 1, 2, -1, -4]) | ||||||
[[-1, 0, 1], [-1, -1, 2]] | ||||||
>>> find_triplets_with_0_sum_hashing([]) | ||||||
[] | ||||||
>>> find_triplets_with_0_sum_hashing([0, 0, 0]) | ||||||
[[0, 0, 0]] | ||||||
>>> find_triplets_with_0_sum_hashing([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(N) | ||||||
Comment on lines
+47
to
+48
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. How do we prove these claims? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Time complexity: O(N^2) |
||||||
|
||||||
""" | ||||||
target_sum = 0 | ||||||
|
||||||
# Initialize the final output array with blank. | ||||||
output_arr = [] | ||||||
|
||||||
# Set the initial element as arr[i]. | ||||||
for i in range(len(arr) - 2): | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. updated as suggested. |
||||||
# to store second elements that can complement the final sum. | ||||||
set_initialize = set() | ||||||
|
||||||
# current sum needed for reaching the target sum | ||||||
current_sum = target_sum - arr[i] | ||||||
|
||||||
# Traverse the subarray arr[i+1:]. | ||||||
for j in range(i + 1, len(arr)): | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. updated as suggested. |
||||||
# required value for the second element | ||||||
required_value = current_sum - arr[j] | ||||||
|
||||||
# Verify if the desired value exists in the set. | ||||||
if required_value in set_initialize: | ||||||
# finding triplet elements combination. | ||||||
combination_array = sorted([arr[i], arr[j], required_value]) | ||||||
if combination_array not in output_arr: | ||||||
output_arr.append(combination_array) | ||||||
|
||||||
# Include the current element in the set | ||||||
# for subsequent complement verification. | ||||||
set_initialize.add(arr[j]) | ||||||
|
||||||
# Return all the triplet combinations. | ||||||
return output_arr |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why?!?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Since after updating the code in the existing file, "(nums: list[int]) -> list[list[int]]:" was failing in ruff test.
(ruff find_triplets_with_0_sum.py was throwing error so I changed to ( nums: list) -> list: )