Imagine an array that undergoes a cyclical transformation process every minute! You're given a 0-indexed integer array nums that follows a fascinating pattern:
Phase 1 (Removal): Starting at minute 0, every minute the leftmost element is removed until the array becomes empty.
Phase 2 (Restoration): Then, elements are added back one by one to the end of the array in the same order they were removed, until the original array is fully restored.
This process repeats indefinitely!
Example: Array [0,1,2] transforms as:
[0,1,2] → [1,2] → [2] → [] → [0] → [0,1] → [0,1,2] → [1,2] → ...
You're also given a 2D array queries where queries[j] = [time_j, index_j]. For each query, you need to determine:
- If
index_jis valid attime_j: returnnums[index_j] - If
index_jis out of bounds: return-1
Goal: Return an array containing the answer to each query.
Input & Output
Constraints
- 1 ≤ nums.length ≤ 100
- 1 ≤ nums[i] ≤ 100
- 1 ≤ queries.length ≤ 1000
- queries[j].length == 2
- 0 ≤ timej ≤ 109
- 0 ≤ indexj < nums.length