Back to problems
COMP2521
2-3-4 Node: which child
2-3-4 Node: which child does a value go to?
A 2-3-4 node holds k keys in ascending order, splitting the number line into k+1 ranges, each mapped to one child (numbered from 0). Given a node's keys and several query values, output the child index each query should descend into.
The trick: the index equals how many keys are smaller than the query. E.g. node [20 | 50]: 10 is smaller than 0 keys -> child 0; 35 is smaller than 1 key (20) -> child 1; 99 -> child 2.
This is exactly the per-step locate done while searching/inserting. Queries never equal an existing key.
Input
- Line 1: number of keys
k(1..3). - Line 2:
kascending integers. - Line 3: number of queries
q. - Line 4:
qquery integers.
Output
qlines, each a child index (0..k).
Example: input 2 / 20 50 / 3 / 10 35 99 -> 0 / 1 / 2.
Samples
stdin
2 20 50 3 10 35 99
expected
0 1 2
stdin
1 30 2 5 40
expected
0 1
c-dcc
14