Tap here to switch tabs
Problem
Submissions

Problem: Elements in Array After Removing and Replacing Elements

Elements in Array After Removing and Replacing Elements asks for the value at each queried array index and time as elements are removed from the front, then restored in original order, or -1 when absent.

Statement

You are given a 00 indexed integer array nums that undergoes a continuous transformation process.

Initially, at minute 00, the array is nums.

  • During the first nums.length minutes, the leftmost element is removed from the array at each minute until the array becomes empty.

  • During the subsequent nums.length minutes, elements from the original nums are appended back to the array one by one in their original order until the array is fully restored.

  • This cycle repeats indefinitely, alternating between removing all elements from left to right and appending all original elements from left to right.

You are also given a 2D2 {D} integer array queries, where queries[j] = [time_j, index_j]. For the jthj^{ {th}} query, determine the element located at index index_j of the array at minute time_j. If the index is out of bounds or the array does not contain an element at index_j at that minute, the answer is −1-1.

Return an array of integers representing the answers to all queries in the given order.

Constraints:

  • 1≤1 \leq nums.length ≤100\leq 100

  • 0≤0 \leq nums[i] ≤100\leq 100

  • n ==== queries.length

  • 1≤1 \leq n ≤105\leq 10^5

  • queries[j].length ==2== 2

  • 0≤0 \leq time_j ≤105\leq 10^5

  • 0≤0 \leq index_j << nums.length

⋮
Tap here to switch tabs
Problem
Submissions

Problem: Elements in Array After Removing and Replacing Elements

Elements in Array After Removing and Replacing Elements asks for the value at each queried array index and time as elements are removed from the front, then restored in original order, or -1 when absent.

Statement

You are given a 00 indexed integer array nums that undergoes a continuous transformation process.

Initially, at minute 00, the array is nums.

  • During the first nums.length minutes, the leftmost element is removed from the array at each minute until the array becomes empty.

  • During the subsequent nums.length minutes, elements from the original nums are appended back to the array one by one in their original order until the array is fully restored.

  • This cycle repeats indefinitely, alternating between removing all elements from left to right and appending all original elements from left to right.

You are also given a 2D2 {D} integer array queries, where queries[j] = [time_j, index_j]. For the jthj^{ {th}} query, determine the element located at index index_j of the array at minute time_j. If the index is out of bounds or the array does not contain an element at index_j at that minute, the answer is −1-1.

Return an array of integers representing the answers to all queries in the given order.

Constraints:

  • 1≤1 \leq nums.length ≤100\leq 100

  • 0≤0 \leq nums[i] ≤100\leq 100

  • n ==== queries.length

  • 1≤1 \leq n ≤105\leq 10^5

  • queries[j].length ==2== 2

  • 0≤0 \leq time_j ≤105\leq 10^5

  • 0≤0 \leq index_j << nums.length