FastPrepNumber of Unique Elements After Modifications 🍁
Problems/Number of Unique Elements After Modifications 🍁

Number of Unique Elements After Modifications 🍁

Infosys logoInfosys● MediumOA

Updated Sep 2026

QuestionFunctionExamplesConstraints

Problem statement

Source note: Updated 07-18-2026: The source's first query does not match its own explanation. The query [3, 5] would update the third element, but the shown array updates the second element from 7 to 5. FastPrep corrects that query to [2, 2], which matches the stated rule, explanation, and output.

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]:

  1. Update nums[L - 1] = nums[L - 1] - x. Each update is applied to the array produced by all previous updates.
  2. Build an array B from the updated nums: set B[0] = nums[0], and for every 1 <= i < n, set B[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^5
  • 1 <= queries.length <= 10^5
  • 1 <= nums[i] <= 10^9 before any updates
  • Every query is [L, x] with 1 <= L <= nums.length and 1 <= 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
All Infosys interview questionsSee Infosys hiring insights
Saved
class Solution {
    public int[] getUniqueElementCounts(int[] nums, int[][] queries) {
        // write your code here
    }

}
Checking account…