Loading...
Loading...
You are given an integer array next of length n, where next[i] is the position reached by making one jump from position i (possibly i itself), and a list of queries queries where each queries[j] = [x, k].
For each query, starting at position x, return the position reached after exactly k jumps.
Return an array with one answer per query, in the same order as queries.
Input: next = [1, 0, 0, 3]
queries = [[0, 2],
[2, 4],
[3, 1]]
Output: [0, 1, 3]
Explanation: Starting at 0, two jumps go 0 -> 1 -> 0. Starting at 2, four jumps go 2 -> 0 -> 1 -> 0 -> 1. Starting at 3, one jump stays at 3 since next[3] = 3.
Input: next = [1, 2, 3, 4, 0]
queries = [[0, 7]]
Output: [2]
Explanation: The five positions form a cycle. After seven jumps, position 0 has gone all the way around once (five jumps) plus two more, landing on 2.
Input: next = [0]
queries = [[0, 1000000000]]
Output: [0]
Explanation: The only position jumps to itself, so any number of jumps stays at 0.
next.lengthnext[i] <nqueries.length ≤2×105[x, k] in queries