The problem's phrasing is a bit ambiguous: what counts as part of the operations to be done in complexity O(t+klogk)
?
I see 3 options in answering this. For the first two, which I think are wrong ways to interpret the original question, I present answers. For the third and last option I have no solution. Still I thought it necessary to make the question clear and add my answers to the first two. Additionally, adding this long discussion of the matter in a comment would be confusing if not impossible.
Here are the options I see for answering the question I started with:
Option 1 - data structure build (using the sections) NOT included
From the current phrasing of the original question it sounds like the problem asks for a data structure that can be built beforehand, using the array A
and the sections [L1,R1],[L2,R2],...,[Lt,Rt]
and the only operations considered for the complexity requirement would be the operations done on the data structure to extract the k
highest values.
That doesn't make a lot of sense as it's unclear why would the t
be put in the complexity in this case? From this answer, which you linked to in your comment to Juan's answer, you can see that by building beforehand a heap using only the relevant elements you can get O(klogk)
for getting the k
highest value elements from the data structure (once it exists).
Option 2 - ALL the operations included
If we read it as if the problem asks for the data structure to be built as part of the operations considered for the complexity requirement (we only get the array and the list of indices [L1,R1,L2,R2,...,Lt,Rt]
and have to work with that) then the complexity requirement is not possible to achieve. If you want to build a data structure you will at least need to go over all the elements that need to be inserted into the data structure, and if you don't want to build a data structure and you intend to use the array as it is then you will have to go over all the elements between which you require to find the max. Either way in case you get t=1
and L1=1, R1=n
where n
is the number of elements in the array A
you will have to go over all the elements in A
(you have no assumptions regarding A
's elements' order). That means you need at least O(n)
and the requirement is O(t+klogk)
. To make it even clearer that these are totally unrelated quantities - you can consider the case for k=1
, meaning finding only the highest element, while A
can be of arbitrary length (let's say 10,000).
Option 3 - data structure build only with array exempt
Which brings me to my guess of the original meaning behind the question: build a data structure from the given array A
beforehand (the building of which will not be included in the complexity requirement) so that when later on you are given sections' indices [L1,R1],[L2,R2],...[Lt,Rt]
and an integer k
the data structure you built before will allow you to get the k
highest elements among the sections defined by the newly given indices (L1,R1,L2,R2,...,Lt,Rt
) in the original array A
in complexity O(t+klogk)
.
Unfortunately I did not come up with a solution to this problem yet - or with a proof that such a solution does not exist, but at least now the problem makes sense.
I would love to know which of the options was the original intention of the question, and if I come up with a solution to option 3 I shall edit this answer accordingly.