MediumNo. 2113LeetCode Premium

Elements in Array After Removing and Replacing Elements — LeetCode 2113 Python Solution

Topics
Array
Reading time
2 min

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

PythonSolution
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 ans

Complexity

Time
O(m)where m is the length of the array queries
Space · auxiliary
O(1)

Related problems

Related problems
No.ProblemDifficulty
0057Insert IntervalMedium
0228Summary RangesEasy
0485Max Consecutive OnesEasy
0665Non-decreasing ArrayMedium
0674Longest Continuous Increasing SubsequenceEasy
0699Falling SquaresHard

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.

Stuck on problems like this in a live interview?

Stealth Interview is a desktop app for macOS and Windows. It reads the problem off your screen and returns a working solution with a step-by-step explanation and its time and space complexity, invisible to screen sharing.

Free to download. Check it runs on your machine, then pick a plan.