You are given an array containing N integers and you have to answer K queries. Each query contains an integer X which is the index of the (1 based index) element of the array.
Calculate the following for each query:
The number of segments containing the index X as the leftmost or the
rightmost element and the number at the index `X` is `>=` each element
of that segment.
Segment formation example:
You have array {1, 2, 3}.
The possible segments for 3 are [2,3] and [1,2,3] and [3].
The possible segments for 2 are [2] and [1,2]
I got solution by brute force. Worst case Time Complexity is O(n * k)
Input: Array[] = {4,2,1,3}, Queries[] = {1,4}
Output:
4
3
Explanation:
For first query 1 all possible valid segments are [4], [4,2] , [4,2,1] and [4,2,1,3]
hence answer is 4.
For second query 4 all possible valid segments are [3], [1,3] and [2,1,3]
hence answer is 3.