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

POTD_27_OCT_2024_TripletFamily #214

Merged
merged 1 commit into from
Oct 31, 2024
Merged

Conversation

Avnee29
Copy link
Contributor

@Avnee29 Avnee29 commented Oct 28, 2024

Efficient approach: The idea is similar to Find a triplet that sum to a given value.  

Sort the given array first.

Start fixing the greatest element of three from the back and traverse the array to find the other two numbers which sum up to the third element.

Take two pointers j(from front) and k(initially i-1) to find the smallest of the two number and from i-1 to find the largest of the two remaining numbers

If the addition of both the numbers is still less than A[i], then we need to increase the value of the summation of two numbers, thereby increasing the j pointer, so as to increase the value of A[j] + A[k].

If the addition of both the numbers is more than A[i], then we need to decrease the value of the summation of two numbers, thereby decrease the k pointer so as to decrease the overall value of A[j] + A[k]. Time Complexity: O(N^2)
Auxiliary Space: O(1)

Description

Please include a summary of the changes and the related issue(s) this pull request addresses. Include any relevant context or background information.

Fixes: #[issue_number] (replace with the issue number, if applicable)

Use [x] to represent a checked (ticked) box.✅
Use [ ] to represent an unchecked box.❌

Type of Change

  • Question Added
  • Solution Added
  • Other (please specify):

Checklist

  • I have performed a self-review of my code.
  • I have commented my code, particularly in hard-to-understand areas.
  • I have made corresponding changes to the documentation (if applicable).
  • My changes generate no new warnings.
  • I have added tests to cover my changes (if applicable).
  • All new and existing tests pass.

Additional Notes

Please add any other information that is relevant to this pull request, including potential risks, alternative solutions considered, or future improvements.

Efficient approach: The idea is similar to Find a triplet that sum to a given value.  

Sort the given array first.

Start fixing the greatest element of three from the back and traverse the array to find the other two numbers which sum up to the third element.

Take two pointers j(from front) and k(initially i-1) to find the smallest of the two number and from i-1 to find the largest of the two remaining numbers

If the addition of both the numbers is still less than A[i], then we need to increase the value of the summation of two numbers, thereby increasing the j pointer, so as to increase the value of A[j] + A[k].

If the addition of both the numbers is more than A[i], then we need to decrease the value of the summation of two numbers, thereby decrease the k pointer so as to decrease the overall value of A[j] + A[k].
Time Complexity: O(N^2)
Auxiliary Space: O(1)
Copy link

@github-actions github-actions bot left a comment

Choose a reason for hiding this comment

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

Great job, @Avnee29! Thanks for creating the pull request.
Soon the maintainers/owner will review it and provide you with feedback/suggestions.
Make sure to star this awesome repository and follow the account!

@Gyanthakur Gyanthakur merged commit 369c5e9 into Gyanthakur:main Oct 31, 2024
6 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants