Data structures/arrays/triplet sum#11134
Conversation
Signed-off-by: Skyad <777.sunnykumar@gmail.com>
Signed-off-by: Sunny Kumar <sunny.kumar09@ad.infosys.com>
Signed-off-by: Skyad <777.sunnykumar@gmail.com>
for more information, see https://pre-commit.ci
Signed-off-by: Skyad <777.sunnykumar@gmail.com>
…om/Skyad/Python into data_structures/arrays/triplet_sum
|
|
||
|
|
||
| def find_triplets_with_0_sum(nums: list[int]) -> list[list[int]]: | ||
| def find_triplets_with_0_sum(nums: list) -> list: |
There was a problem hiding this comment.
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: )
| Time complexity: O(N^2) | ||
| Auxiliary Space: O(N) |
There was a problem hiding this comment.
Time complexity: O(N^2)
--> This approach has two nested for loops, outer loop runs from start till end of the array length and inner loop from start+1 to end, that's why it has N^2 time complexity.
Auxiliary Space: O(N)
--> This method Creates a hashmap to store the elements so n extra space has been used.
| output_arr = [] | ||
|
|
||
| # Set the initial element as arr[i]. | ||
| for i in range(len(arr) - 2): |
There was a problem hiding this comment.
| for i in range(len(arr) - 2): | |
| for i, item in enumerate(arr[:-2]): |
| 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.
| for j in range(i + 1, len(arr)): | |
| for other_item in arr[i+1:]: |
cclauss
left a comment
There was a problem hiding this comment.
Let's not use less specific type hints.
…om/Skyad/Python into data_structures/arrays/triplet_sum
Signed-off-by: Sunny Kumar <sunny.kumar09@ad.infosys.com>
for more information, see https://pre-commit.ci
Signed-off-by: Skyad <777.sunnykumar@gmail.com>
cclauss
left a comment
There was a problem hiding this comment.
I put the more explicit type hints back in place.
|
Thanks !! Christian Clauss for reviewing and accepting my code contribution. |
* updated code for find triplets with 0 sum Signed-off-by: Skyad <777.sunnykumar@gmail.com> * extra line added at the end of file Signed-off-by: Sunny Kumar <sunny.kumar09@ad.infosys.com> * extra line added at the end of file Signed-off-by: Skyad <777.sunnykumar@gmail.com> * [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci * file updated with comments Signed-off-by: Skyad <777.sunnykumar@gmail.com> * updated the comments as suggested by community Signed-off-by: Sunny Kumar <sunny.kumar09@ad.infosys.com> * [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci * file updated according to community comments Signed-off-by: Skyad <777.sunnykumar@gmail.com> * Update find_triplets_with_0_sum.py --------- Signed-off-by: Skyad <777.sunnykumar@gmail.com> Signed-off-by: Sunny Kumar <sunny.kumar09@ad.infosys.com> Co-authored-by: Sunny Kumar <sunny.kumar09@ad.infosys.com> Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com> Co-authored-by: Christian Clauss <cclauss@me.com>
Describe your change:
This contribution includes the hashing approach for finding the triplet sum as 0.
Checklist: