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_12_OCT_2024_TwoSmallestsInEverySubarray #218

Merged
merged 1 commit into from
Oct 29, 2024

Conversation

Avnee29
Copy link
Contributor

@Avnee29 Avnee29 commented Oct 28, 2024

By maximizing consecutive element sum – O(n) time and O(1) auxiliary space

An efficient solution is based on the observation that this problem reduces to finding a maximum sum of two consecutive elements in array. If (x,y) is the pair such that (x+y) is the answer, then x and y must be consecutive elements in the array.

For a subarray with 2 elements, 1st and 2nd smallest elements are those 2 elements. Now, x and y are present in some subarray such that they are the endpoints. Now, x, y must be the smallest 2 elements of that subarray. If there are other elements Z1 , Z2, ……., ZK  between x and y, they are greater than or equal to x and y,

Case1 : 

If there is one element z between x and y, then the smaller subarray with the elements max(x,y) and z , should be the answer, because max(x,y) + z >= x + y

Case2:

If there are more than one elements between x and y, then the subarray within x and y will have all consecutive elements  (Zi + Zi+1) >= (x+y),  so (x,y) pair can’t be the answer. 

So, by contradictions, x and y must be consecutive elements in the array. Time Complexity: O(n), where n is the length of given array 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.

 By maximizing consecutive element sum – O(n) time and O(1) auxiliary space

An efficient solution is based on the observation that this problem reduces to finding a maximum sum of two consecutive elements in array. If (x,y) is the pair such that (x+y) is the answer, then x and y must be consecutive elements in the array.

For a subarray with 2 elements, 1st and 2nd smallest elements are those 2 elements. Now, x and y are present in some subarray such that they are the endpoints. Now, x, y must be the smallest 2 elements of that subarray. If there are other elements Z1 , Z2, ……., ZK  between x and y, they are greater than or equal to x and y,

Case1 : 

If there is one element z between x and y, then the smaller subarray with the elements max(x,y) and z , should be the answer, because max(x,y) + z >= x + y

Case2:

If there are more than one elements between x and y, then the subarray within x and y will have all consecutive elements  (Zi + Zi+1) >= (x+y),  so (x,y) pair can’t be the answer. 

So, by contradictions, x and y must be consecutive elements in the array.
Time Complexity: O(n), where n is the length of given array
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 66874a9 into Gyanthakur:main Oct 29, 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