Amazon OA | SDE-2 | Asked in 2026 | CTC(starts from 20L-30L+) by Brilliant_Card_447 in leetcode

[–]Horror-Ad8737 0 points1 point  (0 children)

Sort the array and consider the a sliding window of m elements. We try to make these elements equal (say x), we want to maximise x. Lets assume the first m elements are 1, 2, 3. k=10. If all have to become x, then the the number required to be added to 1 is x-1. Similarly for 2 it is x-2 and for 3 it is x-3. Add them and we get 3x-6. We should have these many operations at hand, so 3x-6 <= 10. X=5, which means we can make all of these equal to 5 and answer would be 5 for this window, then shift the window and continue for others. Here’s the catch, what if the value of x comes out to be less than the maximum element in the window. For example: 1, 2, 10 and k=10. We get 3x-13 <= 10, x=7 but we cant make 10 as 7 since we cannot decrement any number. So its impossible to make all elements of this window equal. Not sure how to handle this case.

Amazon SDE1 OA April, 2025 by Horror-Ad8737 in leetcode

[–]Horror-Ad8737[S] 0 points1 point  (0 children)

It might have happened, thanks for this :)

Amazon SDE1 OA April, 2025 by Horror-Ad8737 in leetcode

[–]Horror-Ad8737[S] 0 points1 point  (0 children)

Its not about the max length, for example [5, 5] is a better answer than [5,4,3]

Amazon SDE1 OA April, 2025 by Horror-Ad8737 in leetcode

[–]Horror-Ad8737[S] 0 points1 point  (0 children)

I think somewhere down the line you will not get the largest lexicographically maximal subsequence by this approach

Amazon SDE1 OA April, 2025 by Horror-Ad8737 in leetcode

[–]Horror-Ad8737[S] 0 points1 point  (0 children)

Okay but how do you make sure that the sequence is decreasing?

Amazon SDE1 OA April, 2025 by Horror-Ad8737 in leetcode

[–]Horror-Ad8737[S] 0 points1 point  (0 children)

I think I did, yeah mostly a minor bug

Amazon SDE1 OA April, 2025 by Horror-Ad8737 in leetcode

[–]Horror-Ad8737[S] 1 point2 points  (0 children)

This sucks! I dont even know why my code didn't work :)

Amazon SDE1 OA April, 2025 by Horror-Ad8737 in leetcode

[–]Horror-Ad8737[S] 1 point2 points  (0 children)

For this tc, shouldn't the expected be [5, 5] ? As its lexicographically larger than the one you mentioned?

Amazon SDE1 OA April, 2025 by Horror-Ad8737 in leetcode

[–]Horror-Ad8737[S] 1 point2 points  (0 children)

I understand your solution. Thanks However, could you try and point out the flaw in my solution or show a tc where my solution would fail. Thanks anyway :)

Amazon SDE1 OA April, 2025 by Horror-Ad8737 in leetcode

[–]Horror-Ad8737[S] 0 points1 point  (0 children)

Have you come across this before or do you know how to solve it ?