Is the input array assumed to always be sorted ?
No. There is a test case which is unsorted. I implemented a quick sort.
Does anyone have a better solution without sorting the input?
the result is in non-descending order, so we cant go over sorting part. so sorting input is the most efficient way.
I used sort(S.begin(), S.end());
Is this a little tricky?
Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.