Number of Unique Elements After Modifications π
Updated
Problem statement
You are given an integer array nums of length n and a list queries of length m. Process the queries in order. For each query [L, x]:
- Update
nums[L - 1] = nums[L - 1] - x. Each update is applied to the array produced by all previous updates. - Build an array
Bfrom the updatednums: setB[0] = nums[0], and for every1 <= i < n, setB[i] = min(B[i - 1], nums[i]).
After each query, count the number of distinct values in B. Return these counts in order.
Function
getUniqueElementCounts(nums: int[], queries: int[][]) β int[]Examples
Example 1
nums = [5, 7, 2, 2, 4]queries = [[2, 2], [5, 4]]return = [2, 3]After query [2, 2], nums becomes [5, 5, 2, 2, 4]. Its prefix-minimum array is [5, 5, 2, 2, 2], which has 2 distinct values.
After query [5, 4], nums becomes [5, 5, 2, 2, 0]. Its prefix-minimum array is [5, 5, 2, 2, 0], which has 3 distinct values. Therefore the answer is [2, 3].
Constraints
1 <= nums.length <= 10^51 <= queries.length <= 10^51 <= nums[i] <= 10^9before any updates- Every query is
[L, x]with1 <= L <= nums.lengthand1 <= x <= 10^5.
More Infosys interview questions
- At Most K Jumps with Quadratic Transition CostOA Β· Seen Nov 2026
- Hiking Club Fastest PaceOA Β· Seen Nov 2026
- Lowest Common Ancestor with a Changing RootOA Β· Seen Nov 2026
- Minimum Coins for a TargetONSITE INTERVIEW Β· Seen Nov 2026
- Minimum Leaf Path Sum over All RootsOA Β· Seen Nov 2026
- Split Array to Minimize the Largest SumOA Β· Seen Nov 2026
- Cherry PickupOA Β· Seen Nov 2026
- Delete Duplicate Employee RecordsONSITE INTERVIEW Β· Seen Nov 2026
- Find Duplicate Employee RecordsONSITE INTERVIEW Β· Seen Nov 2026
- Lexicographically Smallest Grid Path with One Letter ChangeOA Β· Seen Nov 2026
Similar questions at other companies
- Count Subarrays with a Perfect-Square ProductMicrosoft Β· OAMicrosoft interview questions
- Count Zero-Sum FragmentsTesla Β· OATesla interview questions
- Count Stable Server SubsegmentsScale AI Β· OAScale AI interview questions
- Count Paired Subarrays Reaching a TargetZipRecruiter Β· OAZipRecruiter interview questions
- Count Subarrays with Sum KIndeed Β· OAIndeed interview questions
- Count Subarrays With Given XORInMobi Β· OAInMobi interview questions
- Count Subarrays with Sum Divisible by KVisa Β· OAVisa interview questions
- Beautiful SubarraysAtlassian Β· OAAtlassian interview questions
- Find Security LevelAmazon Β· OA
- Subarray Sum Equals KServiceNow Β· ONSITE INTERVIEWServiceNow interview questions