2598. Smallest Missing Non-negative Integer After Operations

T: O(nums.length + value)
S: O(value)
```java
class Solution {
    public int findSmallestInteger(int[] nums, int value) {
        int[] freq = new int[value];
        for (int num : nums) {
            freq[(((num%value) + value)%value)]++; // avoid negtive
        }
        int minFreq = Integer.MAX_VALUE;
        int remainder = -1;
        for (int i = 0; i < freq.length; i++) {
            if (minFreq > freq[i]) { // i will be smaller one, ex: count[3] = 1, count[4] = 1, i is 3 will match
                minFreq = freq[i];
                remainder = i;
            }
        }
        return value * minFreq + remainder;
    }
}

/**
T: O(nums.length + value)
S: O(value)

The MEX (minimum excluded) of an array is the smallest missing non-negative integer in it.

= smallest integer and >= 0 


 [1,2,3] substract or plus value = 2 -> 
 [-1, 2, 3] ans is 0 (missing 0, we have 2, 3)

 but ask for maximum 
 1, 2, 3
 -> 1, 0, 3 -> ans is 2 (missing 2, we have 0, 1, 3)


 nums = [1,1,3,4,-10, -10, 7, 7], value = 5


 0(-10): 2
 1: 2
 2(7): 2 freq = 2
 3: freq = 1  -> 2 -> 5*1 + 3
 4: 1


 -> 8 is missing num (freq*5 + remainder = 1*5 + 3 = 8)



 */
```

Last updated