-
-
Notifications
You must be signed in to change notification settings - Fork 46.6k
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
Data structures/arrays/triplet sum #11134
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
@@ -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: |
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: )
Time complexity: O(N^2) | ||
Auxiliary Space: O(N) |
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.
How do we prove these claims?
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.
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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
for i in range(len(arr) - 2): | |
for i, item in enumerate(arr[:-2]): |
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.
updated as suggested.
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 comment
The reason will be displayed to describe this comment to others. Learn more.
for j in range(i + 1, len(arr)): | |
for other_item in arr[i+1:]: |
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.
updated as suggested.
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.
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>
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.
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>
* 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: