LeetCode Challenge Day 31 β 2598. Smallest Missing Non-negative Integer After Operations
Nitin Ahirwal / October 16, 2025
Hey folks
This is Day 31 of my LeetCode streak π.
Todayβs problem is 2598. Smallest Missing Non-negative Integer After Operations β a medium hashmap + greedy problem where we want to find the smallest integer we cannot form after reducing numbers modulo value.
π Problem Statement
You are given an integer array nums and an integer value.
- You can replace any number in
numswithnum % value. - After replacement, you want to construct integers starting from
0. - Return the smallest non-negative integer that cannot be constructed.
π‘ Intuition
Each number num can be represented by its remainder modulo value.
If we know the frequency of each remainder, we can try to "use up" these remainders to build integers in increasing order:
- For integer
i, check its remainderi % value. - If we still have an unused element of that remainder, we can form
i. - If not, then
iis the answer.
π Approach
-
Build a hashmap that counts the frequency of each remainder
(num % value).
Normalize negatives with((num % value) + value) % value. -
Start with
i = 0and simulate construction:- Compute
mod = i % value. - If
count[mod] > 0, decrement and continue. - Otherwise, return
i.
- Compute
-
The loop guarantees termination because eventually, some
iwill map to an exhausted remainder.
β±οΈ Complexity Analysis
-
Time complexity:
- Counting frequencies: O(n).
- Simulation loop: worst-case up to
n + 1steps. - Overall O(n).
-
Space complexity:
- At most
valueunique remainders stored. - O(value) additional space.
- At most
π§βπ» Code (JavaScript)
/**
* @param {number[]} nums
* @param {number} value
* @return {number}
*/
var findSmallestInteger = function(nums, value) {
const count = new Map();
// Count frequency of each remainder mod value
for (let num of nums) {
let mod = ((num % value) + value) % value;
count.set(mod, (count.get(mod) || 0) + 1);
}
let i = 0;
while (true) {
let mod = i % value;
if (count.has(mod) && count.get(mod) > 0) {
count.set(mod, count.get(mod) - 1);
i++;
} else {
return i;
}
}
};
π§ͺ Example Walkthrough
Input:
nums = [1,2,3], value = 2
-
Remainders:
{1:2, 0:1} -
i = 0 β remainder 0 available β use one β counts
{1:2, 0:0} -
i = 1 β remainder 1 available β use one β counts
{1:1, 0:0} -
i = 2 β remainder 0 exhausted β return
2.
Output: 2.
π₯ Reflections
This problem is a neat example of modulo arithmetic + greedy simulation.
The trick was realizing that constructing integers boils down to consuming remainder frequencies.
Thatβs it for Day 31 of my LeetCode journey!
Onwards to the next challenge π₯
Happy Coding π¨βπ»