Um, this is not O(n) but O(nlogn), unless you can sort in linear time.
And there are a number of others with the same upper limit.
It seems to me that this cannot be done linearly, but I wanted to ask what's your thoughts on this.
I got your point. I found this as the only optimal way (As far as I found)
Are you sure you want to hide this comment? It will become hidden in your post, but will still be visible via the comment's permalink.
Hide child comments as well
Confirm
For further actions, you may consider blocking this person and/or reporting abuse
We're a place where coders share, stay up-to-date and grow their careers.
Um, this is not O(n) but O(nlogn), unless you can sort in linear time.
And there are a number of others with the same upper limit.
It seems to me that this cannot be done linearly, but I wanted to ask what's your thoughts on this.
I got your point. I found this as the only optimal way (As far as I found)