# Complexity of finding the kth smallest element of all the elements in two order statistics binary search trees

What is the time complexity of finding the kth smallest element of all the elements in two order statistics binary search trees? An order statistics tree is a binary search tree where the size of a subtree at each node is stored at that node. Taking the root of the first tree and trying to find the largest element not greater than it in the second tree and doing a binary search on the rank of the element till the $$k^{th}$$ rank is reached for an element in the first tree, gives a complexity of $$O(h1∗h2)$$ If an element with rank $$k$$ is not found in the first tree, repeat the process with the trees switched.

• What are your thoughts? What is the best algorithm you've found on your own?
– D.W.
May 25, 2022 at 5:57
• Taking the root of the first tree and trying to find the largest element not greater than it in the second tree and doing a binary search on the rank of the element in the first tree gives a complexity of $\mathcal{O}(h1*h2)$ May 25, 2022 at 6:02
• @NeelKaria next time, don’t delete the question from cs.theory stack exchange. Instead, migrate the question. My answer in the comments is now gone. Did you have time to read it? And if so, what are you still stuck at? May 25, 2022 at 6:39
• @AspiringMat, sorry for not migrating it; I was not aware of that. I understand the logic with sorted arrays, but I have trouble with accessing the $i^{th}$ element of the tree in $O(1)$ time, which is required. May 25, 2022 at 8:59