Fenwick Tree (BIT)

Now we try to implement the 2nd additional operation of an order-statistic tree i.e. find the kth smallest element.

Algo :

kth smallest element lies in range [1,N].
if number of elements smaller than or equal to some x is >= k then kth smallest element lies in range [1,x]
else kth smallest element lies in range [x + 1, N].

Using these observations we apply binary search and to find number of elements less than some number x we use sum(1, x) operation of the BIT.

what is the time complexity?

@shahpankti931,
O(log(n)*log(n))
Complexity of Binary Search = log(n),
Complexity of BIT query = log(n).

I hope I’ve cleared your doubt. I ask you to please rate your experience here
Your feedback is very important. It helps us improve our platform and hence provide you
the learning experience you deserve.

On the off chance, you still have some questions or not find the answers satisfactory, you may reopen
the doubt.