Mark Elements on Array by Performing Queries - Problem

You're given an array of positive integers and a series of queries that will mark elements based on specific rules. Your task is to simulate this marking process and track the sum of unmarked elements after each query.

The Process:

  1. Start with all elements unmarked
  2. For each query [index, k]:
    • Mark the element at the given index (if not already marked)
    • Then mark k additional unmarked elements with the smallest values
    • If there are ties in values, prioritize elements with smaller indices
  3. Return the sum of all unmarked elements after each query

Example: Given nums = [1, 2, 2, 1, 2, 3, 1] and query [1, 2], you would mark index 1 (value 2), then mark the 2 smallest unmarked values (the 1's at indices 0 and 3).

Input & Output

example_1.py โ€” Basic Query Processing
$ Input: nums = [1,2,2,1,2,3,1], queries = [[1,2],[3,1],[4,0]]
โ€บ Output: [8,3,3]
๐Ÿ’ก Note: Initially sum = 16. Query [1,2]: mark index 1 (value 2), then mark 2 smallest: indices 0 and 3 (both value 1). Sum = 16-2-1-1 = 12. Wait, let me recalculate... Actually after marking indices 1,0,3 remaining sum = 2+2+3 = 7. Query [3,1]: index 3 already marked, mark 1 smallest unmarked (index 6, value 1). Sum = 2+2+3 = 7-1 = 6. Query [4,0]: mark index 4 only. Sum = 2+3 = 5.
example_2.py โ€” Tie Breaking by Index
$ Input: nums = [3,2,2,3], queries = [[0,1]]
โ€บ Output: [5]
๐Ÿ’ก Note: Mark index 0 (value 3), then mark 1 smallest unmarked element. Two elements have value 2 at indices 1 and 2, choose index 1 (smaller index). Remaining: index 2 (value 2) + index 3 (value 3) = 5.
example_3.py โ€” Not Enough Elements
$ Input: nums = [1,2], queries = [[0,5]]
โ€บ Output: [0]
๐Ÿ’ก Note: Mark index 0 (value 1), then try to mark 5 smallest elements but only 1 remains (index 1). Mark it. All elements marked, sum = 0.

Constraints

  • 1 โ‰ค n โ‰ค 105
  • 1 โ‰ค nums[i] โ‰ค 109
  • 1 โ‰ค m โ‰ค 105
  • 0 โ‰ค indexi < n
  • 1 โ‰ค ki โ‰ค n
  • Sum of all elements fits in 64-bit integer

Visualization

Tap to expand
๐Ÿ“š Library Book ManagementShelf A (Original Array)๐Ÿ“–1๐Ÿ“—2๐Ÿ“˜2๐Ÿ“’1๐Ÿ““2๐Ÿ“™3๐Ÿ“•1๐Ÿ“‹ Priority Queue (Min-Heap)(1,0)(1,3)(1,6)(2,1)(2,2)๐Ÿช Checkout Process1. Mark book at index i2. Extract k most popular3. Calculate remaining sumQuery Processingโšก O(log n) extraction vs O(n) linear search!Heap maintains order automatically as we check out books
Understanding the Visualization
1
Setup Priority System
Create a priority queue where books are ordered by popularity (value) and shelf position (index)
2
Process Query
Mark the book at specified location as checked out
3
Check Out Popular Books
Extract k most popular (smallest value) unchecked books from the priority queue
4
Calculate Inventory
Sum up the total value of remaining unchecked books
Key Takeaway
๐ŸŽฏ Key Insight: Using a min-heap transforms an O(n) search for minimum elements into O(log n) extractions, making the algorithm efficient for processing multiple queries with large k values.
Asked in
Google 45 Amazon 38 Meta 29 Microsoft 22
28.4K Views
Medium Frequency
~25 min Avg. Time
856 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