You are given two integer arrays nums and freq of equal length n. Each element nums[i] represents an ID, and the corresponding element freq[i] indicates how many times that ID should be added to or removed from the collection at step i.

Addition of IDs: If freq[i] is positive, it means freq[i] IDs with the value nums[i] are added to the collection at step i.

Removal of IDs: If freq[i] is negative, it means -freq[i] IDs with the value nums[i] are removed from the collection at step i.

Return an array ans of length n, where ans[i] represents the count of the most frequent ID in the collection after the i-th step. If the collection is empty at any step, ans[i] should be 0 for that step.

Input & Output

Example 1 — Basic Operations
$ Input: nums = [2,3,2,1], freq = [3,2,-3,1]
Output: [3,3,2,2]
💡 Note: Step 0: Add 3 of ID 2 → {2:3} → max = 3. Step 1: Add 2 of ID 3 → {2:3,3:2} → max = 3. Step 2: Remove 3 of ID 2 → {3:2} → max = 2. Step 3: Add 1 of ID 1 → {3:2,1:1} → max = 2.
Example 2 — Collection Becomes Empty
$ Input: nums = [5,5,3], freq = [2,-2,1]
Output: [2,0,1]
💡 Note: Step 0: Add 2 of ID 5 → {5:2} → max = 2. Step 1: Remove 2 of ID 5 → {} → max = 0. Step 2: Add 1 of ID 3 → {3:1} → max = 1.
Example 3 — Same ID Multiple Updates
$ Input: nums = [1,1,1], freq = [1,1,1]
Output: [1,2,3]
💡 Note: Step 0: Add 1 of ID 1 → {1:1} → max = 1. Step 1: Add 1 more of ID 1 → {1:2} → max = 2. Step 2: Add 1 more of ID 1 → {1:3} → max = 3.

Constraints

  • 1 ≤ nums.length == freq.length ≤ 105
  • 1 ≤ nums[i] ≤ 106
  • -105 ≤ freq[i] ≤ 105
  • freq[i] ≠ 0
  • The collection will never have a negative count for any ID

Visualization

Tap to expand
Most Frequent IDs: Track Maximum Frequency Over Time2321nums = [2, 3, 2, 1]+3+2-3+1freq = [3, 2, -3, 1]After each step, find the most frequent ID count3322Output: [3, 3, 2, 2]Step 0: {2:3}→3, Step 1: {2:3,3:2}→3, Step 2: {3:2}→2, Step 3: {3:2,1:1}→2
Understanding the Visualization
1
Input
Two arrays: IDs and their frequency changes
2
Process
Update collection and find maximum frequency
3
Output
Array of maximum frequencies after each step
Key Takeaway
🎯 Key Insight: Use heap to efficiently track maximum frequency as the collection changes over time
Asked in
Google 35 Meta 28 Amazon 22 Apple 18
23.4K Views
Medium-High Frequency
~35 min Avg. Time
892 Likes
Ln 1, Col 1
Smart Actions
💡 Explanation
AI Ready
💡 Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen