MediumNo. 2113LeetCode Premium
Elements in Array After Removing and Replacing Elements — LeetCode 2113 Python Solution
- Topics
- Array
- Reading time
- 2 min
- Source
- leetcode.com
The problem
You are given a 0-indexed integer array nums. Initially on minute 0, the array is unchanged.
Example
- Input
- nums = [0,1,2], queries = [[0,2],[2,0],[3,2],[5,0]]
- Output
- [2,2,-1,0]
- Explanation
- Minute 0: [0,1,2] - All elements are in the nums.
Python solution
class Solution:
def elementInNums(self, nums: List[int], queries: List[List[int]]) -> List[int]:
n, m = len(nums), len(queries)
ans = [-1] * m
for j, (t, i) in enumerate(queries):
t %= 2 * n
if t < n and i < n - t:
ans[j] = nums[i + t]
elif t > n and i < t - n:
ans[j] = nums[i]
return ansComplexity
- Time
- O(m)where m is the length of the array queries
- Space · auxiliary
- O(1)
Related problems
| No. | Problem | Difficulty |
|---|---|---|
| 0057 | Insert Interval | Medium |
| 0228 | Summary Ranges | Easy |
| 0485 | Max Consecutive Ones | Easy |
| 0665 | Non-decreasing Array | Medium |
| 0674 | Longest Continuous Increasing Subsequence | Easy |
| 0699 | Falling Squares | Hard |
Frequently asked questions
- How hard is LeetCode 2113. Elements in Array After Removing and Replacing Elements?
LeetCode 2113. Elements in Array After Removing and Replacing Elements is rated Medium on LeetCode.
- What is the time complexity of LeetCode 2113. Elements in Array After Removing and Replacing Elements?
The Python solution on this page runs in O(m), where m is the length of the array queries.
- What is the space complexity of LeetCode 2113. Elements in Array After Removing and Replacing Elements?
The Python solution on this page uses O(1) auxiliary space.
- What topics does LeetCode 2113. Elements in Array After Removing and Replacing Elements cover?
LeetCode 2113. Elements in Array After Removing and Replacing Elements is tagged Array on LeetCode.
- Is LeetCode 2113. Elements in Array After Removing and Replacing Elements a premium problem?
Yes. LeetCode 2113. Elements in Array After Removing and Replacing Elements is a LeetCode Premium problem, so the full statement and test cases require a paid LeetCode subscription.