You are given n distinct integers. Starting from an empty binary search tree, insert them in the order given, using the standard BST rule: to insert a key x, walk down from the root, going left if x is smaller than the current node and right if x is larger, until an empty spot is reached, where a new node is created. No rebalancing is performed.
You are then given two integers lo and hi (lo ≤ hi). Count how many keys x in the tree satisfy lo < x < hi (the bounds themselves are excluded, even if they equal a key in the tree).
Line 1: an integer n.
Line 2: n space-separated distinct integers — the keys, in insertion order.
Line 3: two space-separated integers lo hi.
A single integer: the count of keys strictly between lo and hi.
Example 1
Input
5 5 3 8 1 4 1 5
Expected
2
Explanation
Keys are {1,3,4,5,8}. Strictly between 1 and 5 are 3 and 4 (both 1 and 5 themselves are excluded), so the count is 2.
Example 2
Input
4 10 20 5 15 5 20
Expected
2
Explanation
Keys are {5,10,15,20}. Strictly between 5 and 20 are 10 and 15, so the count is 2.
Ready to solve this?
Sign in to open the editor, run your code against the sample tests, and submit against the full test suite.
Sign in to solve →