In the question Given an unsorted array of size n, find the total no of elements between two elements i and j (both inclusive).
Examples:
if the array is
arr = [1, 3,3,10,4]
i1 = 1, j1 = 4,
i2 = 9, j2 = 12
Output is: 4 and 2
The numbers are: 1,3,3,4 for the first query
The numbers are: 9,10 for the second query
the first approach will be:
A simple approach is to run a for loop to check whether each element is in the given range and maintain its count. The time complexity for running each case will be O(n).
The Time Complexity is O(n), big O of n.
Auxiliary Space: O(1) big O of 1.
The second approach will be:
An Efficient Approach will be to first sort the given array and after then using a modified binary search function find two indices, one of the first element greater than or equal to the lower bound of range and the other of the last element less than or equal to upper bound. The time for running each query will be O(logn) and for sorting the array once will be O(nlogn).
The Time Complexity of this approach is O(n log n),
the Auxiliary Space is: O(1)
To know more about Binary Search:
https://brainly.com/question/12946457
#SPJ4