A binary search tree (BST) is built from an empty tree by inserting the given keys one at a time, in the order they appear (go left for smaller keys, right for larger, dropping the key at the first empty spot). All inserted keys are distinct.\n\nThen you are given several independent queries. For each query value x, report the floor of x: the largest key stored in the tree that is less than or equal to x. If no stored key is <= x, output the word NONE for that query instead.\n\n## Input format\n\nLine 1: two integers n and q.\nLine 2: n distinct space-separated integers giving the insertion order.\nLine 3: q space-separated integers, the query values.\n\n## Output format\n\nq lines. Line i is the floor of the i-th query, or NONE if no key is <= x.\n\n## Constraints\n\n- 1 <= n <= 100000\n- 1 <= q <= 100000\n- -1000000000 <= each key <= 1000000000, and all keys are distinct.\n- -1000000000 <= each query value <= 1000000000.
Example 1
Input
5 3 8 3 10 1 6 7 2 0
Expected
6 1 NONE
Explanation
Keys are {1, 3, 6, 8, 10}. Floor of 7 is 6 (largest key <= 7). Floor of 2 is 1. Floor of 0 is NONE because every key is greater than 0.
Example 2
Input
4 2 20 10 30 25 5 27
Expected
NONE 25
Explanation
Keys are {10, 20, 25, 30}. Floor of 5 is NONE (all keys exceed 5). Floor of 27 is 25.
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 →