Off-by-one on range boundaries
Wrong move: Loop endpoints miss first/last candidate.
Usually fails on: Fails on minimal arrays and exact-boundary answers.
Fix: Re-derive loops from inclusive/exclusive ranges before coding.
Move from brute-force thinking to an efficient approach using array strategy.
You are given a 0-indexed array nums of size n consisting of positive integers.
You are also given a 2D array queries of size m where queries[i] = [indexi, ki].
Initially all elements of the array are unmarked.
You need to apply m queries on the array in order, where on the ith query you do the following:
indexi if it is not already marked.ki unmarked elements in the array with the smallest values. If multiple such elements exist, mark the ones with the smallest indices. And if less than ki unmarked elements exist, then mark all of them.Return an array answer of size m where answer[i] is the sum of unmarked elements in the array after the ith query.
Example 1:
Input: nums = [1,2,2,1,2,3,1], queries = [[1,2],[3,3],[4,2]]
Output: [8,3,0]
Explanation:
We do the following queries on the array:
1, and 2 of the smallest unmarked elements with the smallest indices if they exist, the marked elements now are nums = [1,2,2,1,2,3,1]. The sum of unmarked elements is 2 + 2 + 3 + 1 = 8.3, since it is already marked we skip it. Then we mark 3 of the smallest unmarked elements with the smallest indices, the marked elements now are nums = [1,2,2,1,2,3,1]. The sum of unmarked elements is 3.4, since it is already marked we skip it. Then we mark 2 of the smallest unmarked elements with the smallest indices if they exist, the marked elements now are nums = [1,2,2,1,2,3,1]. The sum of unmarked elements is 0.Example 2:
Input: nums = [1,4,2,3], queries = [[0,1]]
Output: [7]
Explanation: We do one query which is mark the element at index 0 and mark the smallest element among unmarked elements. The marked elements will be nums = [1,4,2,3], and the sum of unmarked elements is 4 + 3 = 7.
Constraints:
n == nums.lengthm == queries.length1 <= m <= n <= 1051 <= nums[i] <= 105queries[i].length == 20 <= indexi, ki <= n - 1Problem summary: You are given a 0-indexed array nums of size n consisting of positive integers. You are also given a 2D array queries of size m where queries[i] = [indexi, ki]. Initially all elements of the array are unmarked. You need to apply m queries on the array in order, where on the ith query you do the following: Mark the element at index indexi if it is not already marked. Then mark ki unmarked elements in the array with the smallest values. If multiple such elements exist, mark the ones with the smallest indices. And if less than ki unmarked elements exist, then mark all of them. Return an array answer of size m where answer[i] is the sum of unmarked elements in the array after the ith query.
Start with the most direct exhaustive search. That gives a correctness anchor before optimizing.
Pattern signal: Array · Hash Map
[1,2,2,1,2,3,1] [[1,2],[3,3],[4,2]]
[1,4,2,3] [[0,1]]
Source-backed implementations are provided below for direct study and interview prep.
// Accepted solution for LeetCode #3080: Mark Elements on Array by Performing Queries
class Solution {
public long[] unmarkedSumArray(int[] nums, int[][] queries) {
int n = nums.length;
long s = Arrays.stream(nums).asLongStream().sum();
boolean[] mark = new boolean[n];
int[][] arr = new int[n][0];
for (int i = 0; i < n; ++i) {
arr[i] = new int[] {nums[i], i};
}
Arrays.sort(arr, (a, b) -> a[0] == b[0] ? a[1] - b[1] : a[0] - b[0]);
int m = queries.length;
long[] ans = new long[m];
for (int i = 0, j = 0; i < m; ++i) {
int index = queries[i][0], k = queries[i][1];
if (!mark[index]) {
mark[index] = true;
s -= nums[index];
}
for (; k > 0 && j < n; ++j) {
if (!mark[arr[j][1]]) {
mark[arr[j][1]] = true;
s -= arr[j][0];
--k;
}
}
ans[i] = s;
}
return ans;
}
}
// Accepted solution for LeetCode #3080: Mark Elements on Array by Performing Queries
func unmarkedSumArray(nums []int, queries [][]int) []int64 {
n := len(nums)
var s int64
for _, x := range nums {
s += int64(x)
}
mark := make([]bool, n)
arr := make([][2]int, 0, n)
for i, x := range nums {
arr = append(arr, [2]int{x, i})
}
sort.Slice(arr, func(i, j int) bool {
if arr[i][0] == arr[j][0] {
return arr[i][1] < arr[j][1]
}
return arr[i][0] < arr[j][0]
})
ans := make([]int64, len(queries))
j := 0
for i, q := range queries {
index, k := q[0], q[1]
if !mark[index] {
mark[index] = true
s -= int64(nums[index])
}
for ; k > 0 && j < n; j++ {
if !mark[arr[j][1]] {
mark[arr[j][1]] = true
s -= int64(arr[j][0])
k--
}
}
ans[i] = s
}
return ans
}
# Accepted solution for LeetCode #3080: Mark Elements on Array by Performing Queries
class Solution:
def unmarkedSumArray(self, nums: List[int], queries: List[List[int]]) -> List[int]:
n = len(nums)
s = sum(nums)
mark = [False] * n
arr = sorted((x, i) for i, x in enumerate(nums))
j = 0
ans = []
for index, k in queries:
if not mark[index]:
mark[index] = True
s -= nums[index]
while k and j < n:
if not mark[arr[j][1]]:
mark[arr[j][1]] = True
s -= arr[j][0]
k -= 1
j += 1
ans.append(s)
return ans
// Accepted solution for LeetCode #3080: Mark Elements on Array by Performing Queries
// Rust example auto-generated from java reference.
// Replace the signature and local types with the exact LeetCode harness for this problem.
impl Solution {
pub fn rust_example() {
// Port the logic from the reference block below.
}
}
// Reference (java):
// // Accepted solution for LeetCode #3080: Mark Elements on Array by Performing Queries
// class Solution {
// public long[] unmarkedSumArray(int[] nums, int[][] queries) {
// int n = nums.length;
// long s = Arrays.stream(nums).asLongStream().sum();
// boolean[] mark = new boolean[n];
// int[][] arr = new int[n][0];
// for (int i = 0; i < n; ++i) {
// arr[i] = new int[] {nums[i], i};
// }
// Arrays.sort(arr, (a, b) -> a[0] == b[0] ? a[1] - b[1] : a[0] - b[0]);
// int m = queries.length;
// long[] ans = new long[m];
// for (int i = 0, j = 0; i < m; ++i) {
// int index = queries[i][0], k = queries[i][1];
// if (!mark[index]) {
// mark[index] = true;
// s -= nums[index];
// }
// for (; k > 0 && j < n; ++j) {
// if (!mark[arr[j][1]]) {
// mark[arr[j][1]] = true;
// s -= arr[j][0];
// --k;
// }
// }
// ans[i] = s;
// }
// return ans;
// }
// }
// Accepted solution for LeetCode #3080: Mark Elements on Array by Performing Queries
function unmarkedSumArray(nums: number[], queries: number[][]): number[] {
const n = nums.length;
let s = nums.reduce((acc, x) => acc + x, 0);
const mark: boolean[] = Array(n).fill(false);
const arr = nums.map((x, i) => [x, i]);
arr.sort((a, b) => (a[0] === b[0] ? a[1] - b[1] : a[0] - b[0]));
let j = 0;
const ans: number[] = [];
for (let [index, k] of queries) {
if (!mark[index]) {
mark[index] = true;
s -= nums[index];
}
for (; k && j < n; ++j) {
if (!mark[arr[j][1]]) {
mark[arr[j][1]] = true;
s -= arr[j][0];
--k;
}
}
ans.push(s);
}
return ans;
}
Use this to step through a reusable interview workflow for this problem.
Two nested loops check every pair or subarray. The outer loop fixes a starting point, the inner loop extends or searches. For n elements this gives up to n²/2 operations. No extra space, but the quadratic time is prohibitive for large inputs.
Most array problems have an O(n²) brute force (nested loops) and an O(n) optimal (single pass with clever state tracking). The key is identifying what information to maintain as you scan: a running max, a prefix sum, a hash map of seen values, or two pointers.
Review these before coding to avoid predictable interview regressions.
Wrong move: Loop endpoints miss first/last candidate.
Usually fails on: Fails on minimal arrays and exact-boundary answers.
Fix: Re-derive loops from inclusive/exclusive ranges before coding.
Wrong move: Zero-count keys stay in map and break distinct/count constraints.
Usually fails on: Window/map size checks are consistently off by one.
Fix: Delete keys when count reaches zero.