Skip to content
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

Merged

Conversation

Skyad
Copy link
Contributor

@Skyad Skyad commented Nov 1, 2023

Describe your change:

This contribution includes the hashing approach for finding the triplet sum as 0.

  • Add an algorithm?
  • Fix a bug or typo in an existing algorithm?
  • Add or change doctests? -- Note: Please avoid changing both code and tests in a single pull request.
  • Documentation change?

Checklist:

  • I have read CONTRIBUTING.md.
  • This pull request is all my own work -- I have not plagiarized.
  • I know that pull requests will not be merged if they fail the automated tests.
  • This PR only changes one algorithm file. To ease review, please open separate PRs for separate algorithms.
  • All new Python files are placed inside an existing directory.
  • All filenames are in all lowercase characters with no spaces or dashes.
  • All functions and variable names follow Python naming conventions.
  • All function parameters and return values are annotated with Python type hints.
  • All functions have doctests that pass the automated testing.
  • All new algorithms include at least one URL that points to Wikipedia or another similar explanation.
  • If this pull request resolves one or more open issues then the description above includes the issue number(s) with a closing keyword: "Fixes #ISSUE-NUMBER".

Skyad and others added 4 commits October 21, 2023 22:37
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>
@algorithms-keeper algorithms-keeper bot added the tests are failing Do not merge until tests pass label Nov 1, 2023
Skyad added 2 commits November 1, 2023 11:08
Signed-off-by: Skyad <777.sunnykumar@gmail.com>
@algorithms-keeper algorithms-keeper bot removed the tests are failing Do not merge until tests pass label Nov 1, 2023
@@ -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:
Copy link
Member

@cclauss cclauss Nov 5, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why?!?

Copy link
Contributor Author

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: )

Comment on lines +47 to +48
Time complexity: O(N^2)
Auxiliary Space: O(N)
Copy link
Member

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?

Copy link
Contributor Author

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):
Copy link
Member

@cclauss cclauss Nov 5, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
for i in range(len(arr) - 2):
for i, item in enumerate(arr[:-2]):

Copy link
Contributor Author

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)):
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
for j in range(i + 1, len(arr)):
for other_item in arr[i+1:]:

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

updated as suggested.

Copy link
Member

@cclauss cclauss left a 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.

@algorithms-keeper algorithms-keeper bot added the awaiting changes A maintainer has requested changes to this PR label Nov 5, 2023
Sunny Kumar added 2 commits November 6, 2023 15:44
Signed-off-by: Sunny Kumar <sunny.kumar09@ad.infosys.com>
@algorithms-keeper algorithms-keeper bot added awaiting reviews This PR is ready to be reviewed and removed awaiting changes A maintainer has requested changes to this PR labels Nov 6, 2023
@algorithms-keeper algorithms-keeper bot added the tests are failing Do not merge until tests pass label Nov 6, 2023
Signed-off-by: Skyad <777.sunnykumar@gmail.com>
@algorithms-keeper algorithms-keeper bot removed the tests are failing Do not merge until tests pass label Nov 6, 2023
@cclauss cclauss enabled auto-merge (squash) November 6, 2023 12:09
Copy link
Member

@cclauss cclauss left a 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.

@Skyad
Copy link
Contributor Author

Skyad commented Nov 8, 2023

Thanks !! Christian Clauss for reviewing and accepting my code contribution.

cclauss added a commit to cclauss/Python that referenced this pull request Jan 13, 2024
* 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>
sedatguzelsemme pushed a commit to sedatguzelsemme/Python that referenced this pull request Sep 15, 2024
* 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>
@isidroas isidroas mentioned this pull request Jan 25, 2025
14 tasks
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants