---
# System prepended metadata

title: 2113. Elements in Array After Removing and Replacing Elements
tags: [Leetcode, Medium, Math]

---

# 2113. Elements in Array After Removing and Replacing Elements
###### tags: `Leetcode` `Medium` `Math`
Link: https://leetcode.com/problems/elements-in-array-after-removing-and-replacing-elements/description/
## 思路
找规律 ```2*len(nums)```minutes重复一次 所以我们对time取余数 这样只要研究第一次重复即可
每次分成前半段和后半段 前半段的长度为```len(nums)```(逐渐减少element) 后半段```len(nums)-1```(逐渐增加element)
如果time处于前半段 那么我们要的element就是nums[time+index]
如果time处于后半段 那么我们要的element就是nums[index]
## Code
```java=
class Solution:
    def elementInNums(self, nums: List[int], queries: List[List[int]]) -> List[int]:
        ans = []
        for time, index in queries:
            time %= 2*len(nums)
            if time<=len(nums):
                if time+index>=len(nums):
                    ans.append(-1)
                else: ans.append(nums[time+index])
            else:
                if index>=time-len(nums):
                    ans.append(-1)
                else: ans.append(nums[index])
        return ans
```