Gyanthakur / GFG_POTD

Add GfG potd fcfs
26 stars 52 forks source link

POTD_27_OCT_2024_TripletFamily #214

Closed Avnee29 closed 3 weeks ago

Avnee29 commented 4 weeks ago

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

Checklist

Additional Notes

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