From 343c85165b41bd57f7b09758ffc803f6b872328e Mon Sep 17 00:00:00 2001 From: Skyad <777.sunnykumar@gmail.com> Date: Sat, 21 Oct 2023 22:37:25 +0530 Subject: [PATCH 1/9] updated code for find triplets with 0 sum Signed-off-by: Skyad <777.sunnykumar@gmail.com> --- .../arrays/find_triplets_with_0_sum.py | 58 ++++++++++++++++++- 1 file changed, 57 insertions(+), 1 deletion(-) diff --git a/data_structures/arrays/find_triplets_with_0_sum.py b/data_structures/arrays/find_triplets_with_0_sum.py index 8217ff857e3d..b4d12fad2a1b 100644 --- a/data_structures/arrays/find_triplets_with_0_sum.py +++ b/data_structures/arrays/find_triplets_with_0_sum.py @@ -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,59 @@ 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]) + [[-3, 1, 2], [-3, 0, 3], [-2, 0, 2], [-2, -1, 3], [-1, 0, 1]] + + Time complexity: O(N^2) + Auxiliary Space: O(N) + + """ + 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): + # 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)): + # 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 \ No newline at end of file From bb75172a28dd06284f4f8550e459c7bf1e14804f Mon Sep 17 00:00:00 2001 From: Sunny Kumar Date: Wed, 1 Nov 2023 10:30:37 +0530 Subject: [PATCH 2/9] extra line added at the end of file Signed-off-by: Sunny Kumar --- data_structures/arrays/find_triplets_with_0_sum.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/data_structures/arrays/find_triplets_with_0_sum.py b/data_structures/arrays/find_triplets_with_0_sum.py index b4d12fad2a1b..5b435d14ba1f 100644 --- a/data_structures/arrays/find_triplets_with_0_sum.py +++ b/data_structures/arrays/find_triplets_with_0_sum.py @@ -77,4 +77,4 @@ def find_triplets_with_0_sum_hashing(arr: list) -> list: set_initialize.add(arr[j]) # Return all the triplet combinations. - return output_arr \ No newline at end of file + return output_arr From 15aaa77ddea19b09ee233541ac2b284ce15839a1 Mon Sep 17 00:00:00 2001 From: Skyad <777.sunnykumar@gmail.com> Date: Wed, 1 Nov 2023 10:35:30 +0530 Subject: [PATCH 3/9] extra line added at the end of file Signed-off-by: Skyad <777.sunnykumar@gmail.com> --- data_structures/arrays/find_triplets_with_0_sum.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/data_structures/arrays/find_triplets_with_0_sum.py b/data_structures/arrays/find_triplets_with_0_sum.py index b4d12fad2a1b..5b435d14ba1f 100644 --- a/data_structures/arrays/find_triplets_with_0_sum.py +++ b/data_structures/arrays/find_triplets_with_0_sum.py @@ -77,4 +77,4 @@ def find_triplets_with_0_sum_hashing(arr: list) -> list: set_initialize.add(arr[j]) # Return all the triplet combinations. - return output_arr \ No newline at end of file + return output_arr From b7208c042dfff52a367e2ae7328b6400b3e4880f Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Wed, 1 Nov 2023 05:16:24 +0000 Subject: [PATCH 4/9] [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 | 1 + 1 file changed, 1 insertion(+) diff --git a/data_structures/arrays/find_triplets_with_0_sum.py b/data_structures/arrays/find_triplets_with_0_sum.py index 5b435d14ba1f..959b3b4f0898 100644 --- a/data_structures/arrays/find_triplets_with_0_sum.py +++ b/data_structures/arrays/find_triplets_with_0_sum.py @@ -23,6 +23,7 @@ def find_triplets_with_0_sum(nums: list) -> list: 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. From 86c9285b3a085f1a7c6c6d11a61b8933f223ce9e Mon Sep 17 00:00:00 2001 From: Skyad <777.sunnykumar@gmail.com> Date: Wed, 1 Nov 2023 11:08:06 +0530 Subject: [PATCH 5/9] file updated with comments Signed-off-by: Skyad <777.sunnykumar@gmail.com> --- data_structures/arrays/find_triplets_with_0_sum.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/data_structures/arrays/find_triplets_with_0_sum.py b/data_structures/arrays/find_triplets_with_0_sum.py index 5b435d14ba1f..e5282a0f2da3 100644 --- a/data_structures/arrays/find_triplets_with_0_sum.py +++ b/data_structures/arrays/find_triplets_with_0_sum.py @@ -41,7 +41,7 @@ def find_triplets_with_0_sum_hashing(arr: list) -> list: >>> 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]) - [[-3, 1, 2], [-3, 0, 3], [-2, 0, 2], [-2, -1, 3], [-1, 0, 1]] + [[-1, 0, 1], [-3, 1, 2], [-2, 0, 2], [-2, -1, 3], [-3, 0, 3]] Time complexity: O(N^2) Auxiliary Space: O(N) From 02a3f16e386344b36fe852eb73d0d3275cc5b4c8 Mon Sep 17 00:00:00 2001 From: Sunny Kumar Date: Mon, 6 Nov 2023 15:54:58 +0530 Subject: [PATCH 6/9] updated the comments as suggested by community Signed-off-by: Sunny Kumar --- data_structures/arrays/find_triplets_with_0_sum.py | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/data_structures/arrays/find_triplets_with_0_sum.py b/data_structures/arrays/find_triplets_with_0_sum.py index 53dada23a56a..692659fb0ae6 100644 --- a/data_structures/arrays/find_triplets_with_0_sum.py +++ b/data_structures/arrays/find_triplets_with_0_sum.py @@ -54,7 +54,7 @@ def find_triplets_with_0_sum_hashing(arr: list) -> list: output_arr = [] # Set the initial element as arr[i]. - for i in range(len(arr) - 2): + for i, item in enumerate(arr[:-2]): # to store second elements that can complement the final sum. set_initialize = set() @@ -62,20 +62,20 @@ def find_triplets_with_0_sum_hashing(arr: list) -> list: current_sum = target_sum - arr[i] # Traverse the subarray arr[i+1:]. - for j in range(i + 1, len(arr)): + for other_item in arr[i+1:]: # required value for the second element - required_value = current_sum - arr[j] + required_value = current_sum - arr[other_item] # 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]) + combination_array = sorted([arr[i], arr[other_item], 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]) + set_initialize.add(arr[other_item]) # Return all the triplet combinations. return output_arr From ee15c3463263d55cf4d84e0e1bc68b695da0e320 Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Mon, 6 Nov 2023 10:26:22 +0000 Subject: [PATCH 7/9] [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, 1 insertion(+), 1 deletion(-) diff --git a/data_structures/arrays/find_triplets_with_0_sum.py b/data_structures/arrays/find_triplets_with_0_sum.py index 692659fb0ae6..728b45693639 100644 --- a/data_structures/arrays/find_triplets_with_0_sum.py +++ b/data_structures/arrays/find_triplets_with_0_sum.py @@ -62,7 +62,7 @@ def find_triplets_with_0_sum_hashing(arr: list) -> list: current_sum = target_sum - arr[i] # Traverse the subarray arr[i+1:]. - for other_item in arr[i+1:]: + for other_item in arr[i + 1 :]: # required value for the second element required_value = current_sum - arr[other_item] From 8c4b8a67655d784913f3c96526436327c7c13030 Mon Sep 17 00:00:00 2001 From: Skyad <777.sunnykumar@gmail.com> Date: Mon, 6 Nov 2023 16:23:55 +0530 Subject: [PATCH 8/9] file updated according to community comments Signed-off-by: Skyad <777.sunnykumar@gmail.com> --- data_structures/arrays/find_triplets_with_0_sum.py | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/data_structures/arrays/find_triplets_with_0_sum.py b/data_structures/arrays/find_triplets_with_0_sum.py index 728b45693639..0962f2cf1156 100644 --- a/data_structures/arrays/find_triplets_with_0_sum.py +++ b/data_structures/arrays/find_triplets_with_0_sum.py @@ -54,28 +54,28 @@ def find_triplets_with_0_sum_hashing(arr: list) -> list: output_arr = [] # Set the initial element as arr[i]. - for i, item in enumerate(arr[:-2]): + for index, item in enumerate(arr[:-2]): # 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] + current_sum = target_sum - item # Traverse the subarray arr[i+1:]. - for other_item in arr[i + 1 :]: + for other_item in arr[index + 1 :]: # required value for the second element - required_value = current_sum - arr[other_item] + required_value = current_sum - other_item # 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[other_item], required_value]) + combination_array = sorted([item, other_item, 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[other_item]) + set_initialize.add(other_item) # Return all the triplet combinations. return output_arr From 5ca6e1f9f7e4d399f68b902076c53ce13b2ac22b Mon Sep 17 00:00:00 2001 From: Christian Clauss Date: Mon, 6 Nov 2023 18:01:18 +0600 Subject: [PATCH 9/9] Update find_triplets_with_0_sum.py --- data_structures/arrays/find_triplets_with_0_sum.py | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/data_structures/arrays/find_triplets_with_0_sum.py b/data_structures/arrays/find_triplets_with_0_sum.py index 0962f2cf1156..52e521906873 100644 --- a/data_structures/arrays/find_triplets_with_0_sum.py +++ b/data_structures/arrays/find_triplets_with_0_sum.py @@ -1,7 +1,7 @@ from itertools import combinations -def find_triplets_with_0_sum(nums: list) -> list: +def find_triplets_with_0_sum(nums: list[int]) -> list[list[int]]: """ Given a list of integers, return elements a, b, c such that a + b + c = 0. Args: @@ -24,7 +24,7 @@ def find_triplets_with_0_sum(nums: list) -> list: ] -def find_triplets_with_0_sum_hashing(arr: list) -> list: +def find_triplets_with_0_sum_hashing(arr: list[int]) -> list[list[int]]: """ Function for finding the triplets with a given sum in the array using hashing. @@ -79,3 +79,9 @@ def find_triplets_with_0_sum_hashing(arr: list) -> list: # Return all the triplet combinations. return output_arr + + +if __name__ == "__main__": + from doctest import testmod + + testmod()