Yea almost everyone did, getting AC in one attempt is applaudible, at least for people like me.
Donât use quicksort, it has a bad worst case. Try using merge sort or the built in sort function.
use inline sort i.e.:
sort(arr,arr+n)
Quick Sort worst case goes to O(n^2) and thus exceeds time
Essentially similar kind of approach (y)
What is your question exactly ?
The time complexity should be O(nlogn) because you have to sort all the values at least.
And the greedy algorithm needs proving (though I donât know how to prove it).
I donât find anything faulty in your approach, this SHOULD definitely work.
Maybe youâre missing some corner case still testing your code for counter cases.
What you are saying is true, donât know the exact reason. Still it wonât hurt to try other sorting methods once.
Right, in case of randomised pivot probability of running into worst case is less but not nill
I have tested it on many cases, spent hours, donât know why it is failing. Please do inform if you find any.
Sorting will make it quite convenient to solve. Just one loop from end of array upto the -ive numbers which increases the happiness.
The real thing, in my opinion, was that to realize Q mentioned âsubsetâ instead of âsubarraysâ. I just looked at sample I/O, confused subset with subarrays and banged my head on how to solve it. Lol.
We dont have to do much with average time. You need to make sure your WORST time complexity is meeting requirement.
Looking to improve the editorial with a demonstration of the greedy approach of adding negative elements to the initial non-negative-elements-subset ⌠right now it just like âuse it, it worksââŚ
Okay, I appreciate the suggestion
So tell me the statements you want me to add to the explanation.
This is what i was talking about ⌠very goodâŚ
Probably you want me to explain the logic of adding negative elements ? We can add that something like âLesser negative elements should be added first to the selected subset only then is the possibility to increase happinessâ, satisfactory ?