How to find the index of an element in sorted set?
-
30-10-2019 - |
Question
I can find an element in a sorted set (backed by BST) in O(logN)
. Now I would like the index of this element. For example, in set {1, 3, 4, 10}
, the index of 4
is 2
and the index of 1
is 0
.
Obviously, I can just iterate over the set, so the trivial solution is O(N)
. Can we do better using probably BST properties and/or auxiliary data structures ?
No correct solution
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow