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 - Hash Map Approach INPUT nums array: 2 3 2 1 idx: 0 1 2 3 freq array: +3 +2 -3 +1 Data Structures: 1. HashMap: ID --> count 2. Max-heap or sorted set for tracking max freq n = 4 (array length) Track freq of each ID ALGORITHM STEPS 1 i=0: Add 3 of ID 2 count[2]=3, max=3 ans[0]=3 2 i=1: Add 2 of ID 3 count[3]=2, max=3 ans[1]=3 3 i=2: Remove 3 of ID 2 count[2]=0, max=2 ans[2]=2 4 i=3: Add 1 of ID 1 count[1]=1, max=2 ans[3]=2 Final HashMap State: ID | Count 1 | 1 2 | 0 3 | 2 (max) OK FINAL RESULT Output Array (ans): 3 3 2 2 idx: 0 1 2 3 Step Results: Step 0: {2:3} max freq = 3 Step 1: {2:3, 3:2} max = 3 Step 2: {2:0, 3:2} max = 2 Step 3: {2:0, 3:2, 1:1} max = 2 (ID 2 removed, ID 3 is max) Output: [3,3,2,2] -- VERIFIED OK -- Key Insight: Use a HashMap to track count of each ID, and a Max-Heap (or TreeMap) to efficiently find the maximum frequency after each operation. At each step, update the count and query the max in O(log n) time. Total Time Complexity: O(n log n) | Space Complexity: O(n) for storing ID counts and heap entries. TutorialsPoint - Most Frequent IDs | Hash Map + Max-Heap Approach
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