Interview Questions/Coding/Book Reading Unlock Order

Book Reading Unlock Order

Preview mode. Log in to edit, run, submit, and save progress.

Medium

Book Reading Unlock Order

Bob has n book volumes numbered from 1 to n. To maintain continuity, he can only read volume i after finishing all volumes from 1 to i - 1. Each day, exactly one volume unlocks. The unlocking order is given as a permutation of size n, where the i-th element represents the volume unlocked on day i. For each day, determine how many new volumes Bob can read. If the unlocked volume allows reading multiple consecutive unread volumes, count all of them. Otherwise, output 0.

Examples

Example 1
Input:
2 1 4 3
Output:
0 2 0 2

Explanation: On day 2, Bob can read volumes 1 and 2. On day 4, Bob can read volumes 3 and 4.

Approach hint

Keep track of the smallest volume number that Bob has not read yet.

Common mistake

Skipping assumptions, edge cases, or trade-offs can make an otherwise good answer feel incomplete.

solution.cpp