0

You are given an array A of positive integers and some queries.

In each query, you are given positive integers X, Y, l and r.

For each query, you are to find the maximum element in range [l : r] of array A, which is more than X and less than Y. If no such element present, output -1.

I had an explanation of similar question where you are to find the maximum element less than K in a range of array. But here I am not able to apply that logic.

Expected time is O(log n) or polylogarithmic time.

Community
  • 1
  • 1
Sushil Verma
  • 659
  • 9
  • 23
  • 2
    What is your question? –  Nov 06 '17 at 05:01
  • 1
    _'Expected time is O(log n)'_ Hmm, expected time of what? ...of preparing metadata, of executing a single query, of executing all queries? And logarithmic with respect to what? ...to a length of the array, to number of distinct numbers in the array, so a sum of array's length and number of queries? – CiaPan Nov 06 '17 at 08:13

2 Answers2

0

You need a Heap datastructure. However i have always noted the timecomplexity with tilde notation instead of Big O. So a heap datastructure with N-item will not take more than ~1 + logN compares and for removal ~2logN and if you use big O then it's equivalent with O(LogN) .

Luai Ghunim
  • 976
  • 7
  • 14
0

I had an explanation of similar question where you are to find the maximum element less than K in a range of array. But here I am not able to apply that logic.

I don't know your explanation. But I think you can build on it.

Suppose you applied that logic and found the maximum element less than Y. Let that maximum element is u (-1 if no such value).

if (u > X)
{
    return u;
}
else
{
    return -1;
}