I had this question recently in an interview and I failed, and now search for the answer.
Let's say I have a big array of n integers, all differents.
If this array was ordered, I could subdivide it in x smaller arrays, all of size y, except maybe the last one, which could be less. I could than extract the nth subarray and return it, already sorted.
Example : Array 4 2 5 1 6 3. If y=2 and I want the 2nd array, it would be 3 4.
Now what I did is simply sort the array and return the nth subarray, which takes O(n log n
). But it was said to me that there exists a way to do it in O(n + y log y)
. I searched on internet and didn't find anything. Ideas ?