Elements in Array After Removing and Replacing Elements - Problem

You are given a 0-indexed integer array nums. Initially on minute 0, the array is unchanged. Every minute, the leftmost element in nums is removed until no elements remain. Then, every minute, one element is appended to the end of nums, in the order they were removed in, until the original array is restored. This process repeats indefinitely.

For example, the array [0,1,2] would change as follows: [0,1,2] → [1,2] → [2] → [] → [0] → [0,1] → [0,1,2] → [1,2] → [2] → [] → [0] → [0,1] → [0,1,2] → ...

You are also given a 2D integer array queries of size n where queries[j] = [timej, indexj]. The answer to the jth query is:

  • nums[indexj] if indexj < nums.length at minute timej
  • -1 if indexj >= nums.length at minute timej

Return an integer array ans of size n where ans[j] is the answer to the jth query.

Input & Output

Example 1 — Basic Cycle
$ Input: nums = [0,1,2], queries = [[3,1],[4,2]]
Output: [-1,-1]
💡 Note: At time 3: array is [], so index 1 doesn't exist → -1. At time 4: array is [0], so index 2 doesn't exist → -1
Example 2 — Removal Phase
$ Input: nums = [2], queries = [[0,0],[1,0]]
Output: [2,-1]
💡 Note: At time 0: array is [2], index 0 exists → 2. At time 1: array is [], index 0 doesn't exist → -1
Example 3 — Multiple Cycles
$ Input: nums = [1,2], queries = [[4,0],[6,1]]
Output: [1,2]
💡 Note: Cycle length is 4. At time 4 (4%4=0): back to original [1,2], index 0 → 1. At time 6 (6%4=2): restoration phase → 2

Constraints

  • 1 ≤ nums.length ≤ 5000
  • -109 ≤ nums[i] ≤ 109
  • 1 ≤ queries.length ≤ 5000
  • 0 ≤ timej ≤ 109
  • 0 ≤ indexj ≤ nums.length - 1

Visualization

Tap to expand
Array Elements After Removing and Replacing INPUT nums array: 0 1 2 idx 0 idx 1 idx 2 Cycle Pattern (n=3): min 0: [0,1,2] min 1: [1,2] min 2: [2] min 3: [] min 4: [0] min 5: [0,1] min 6: [0,1,2] --repeat Queries: [[3,1], [4,2]] time=3, idx=1 time=4, idx=2 ALGORITHM STEPS 1 Find Cycle Length cycle = 2 * n = 2 * 3 = 6 2 Normalize Time t = time % cycle 3 Determine Phase Phase 1: t < n (removing) Phase 2: t >= n (adding) 4 Calculate Answer Phase 1: nums[t + idx] Phase 2: nums[idx] Processing Queries: Query 1: time=3, idx=1 t = 3 % 6 = 3 t >= n, len = 3-3 = 0 idx >= len --> -1 Query 2: time=4, idx=2 t = 4 % 6 = 4, len = 1 nums[2] --> 2 FINAL RESULT Answer Array: -1 2 ans[0] ans[1] Query 1 Result: -1 At minute 3, array is empty index 1 is out of bounds Query 2 Result: 2 At minute 4, array is [0] nums[2] = 2 (original) Output: [-1, 2] Key Insight: Mathematical Pattern Recognition The array follows a predictable cycle of length 2n. Use modular arithmetic to find the effective time within the cycle. Phase 1 (t < n): elements removed from left, length = n - t. Phase 2 (t >= n): elements added back from start, length = t - n. O(1) per query! TutorialsPoint - Elements in Array After Removing and Replacing Elements | Mathematical Pattern Recognition
Asked in
Google 15 Amazon 12 Microsoft 8
8.5K Views
Medium Frequency
~25 min Avg. Time
284 Likes
Ln 1, Col 1
Smart Actions
💡 Explanation
AI Ready
💡 Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen