Find the Document Token Wrap Point
Context
A document index stores token-priority numbers in reading order. The index was created by cutting a nondecreasing priority list at one position and moving its beginning to the end. To restore the canonical starting point, locate the first place where a priority is greater than the next one.
Problem
Implement a function that returns the zero-based position where the given token-priority sequence should begin so that its cyclic rotation is nondecreasing. Scan neighboring values from left to right. The first pair in which the left value is greater than the right value marks the wrap: return the index of the right value. If no such pair exists, return 0 because the sequence is already nondecreasing. An empty sequence and a sequence with one element also return 0. The input is guaranteed to represent a cyclic shift of a nondecreasing sequence, so the first descent is the canonical boundary.
Examples
priorities = [7, 9, 12, 2, 4]3priorities = [-5, -2, 0, 0, 6]0priorities = [3, 3, 1, 2, 2]2Constraints
0 <= len(priorities) <= 100000-1000000000 <= priorities[i] <= 1000000000 for every i- Types:
prioritiesis int[]; result is int
Function signature
def find_token_wrap_point(priorities: list[int]) -> int
prioritieslist[int]- Token-priority values in their stored cyclic order.
- returns int
- The zero-based index at which the canonical nondecreasing rotation begins; return 0 when no descent exists.
Adapted from MBPP problem task_802 (CC BY 4.0). Rewritten, extended and verified by Iksha.
Notes
- The sequence is guaranteed to be a cyclic rotation of a nondecreasing sequence.
- Equal neighboring values never form a descent.