Get Smallest Nonnegative Integer Not In The Array
- finds the smallest nonnegative integer that is NOT in the array.
Given an array arr
of unique nonnegative integers, implement a functiongetDifferentNumber
that finds the smallest nonnegative integer that is NOT in the array.
Even if your programming language of choice doesn’t have that restriction (like Python), assume that the maximum value an integer can have isMAX_INT = 2^31-1
. So, for instance, the operationMAX_INT + 1
would be undefined in our case.
Your algorithm should be efficient, both from a time and a space complexity perspectives.
Solve first for the case when you’re NOT allowed to modify the inputarr
. If successful and still have time, see if you can come up with an algorithm with an improved space complexity when modifyingarr
is allowed. Do so without trading off the time complexity.
Analyze the time and space complexities of your algorithm.
Example:
Constraints:
[time limit] 5000ms
[input] array.integer
arr
1 ≤ arr.length ≤ MAX_INT
0 ≤ arr[i] ≤ MAX_INT for every i, 0 ≤ i < MAX_INT
[output] integer
Analysis
Using HashSet
Using TreeMap/TreeSet
TreeSet/TreeMap is ordered, each add() (or put() for TreeMap) operation takes O(logn) time complexity.
Then iterate over the keys to find not matched integers
Sorting Array
A simple solution would be to create a copy
arr
, sort that copy in an ascending order, iterate over its values, and then return the first index for which the conditioni != arrSorted[i]
is met, wherearrSorted
is the sorted copy ofarr
. This approach works since all the values inarr
are nonnegative integers.
Time Complexity: duplicating the array is O(N), sorting it is O(N⋅log(N)), and then traversing the array is another O(N). The total time complexity is, therefore, O(N⋅log(N)).
Space Complexity: we used a single auxiliary array, arrSorted, whose size is the same as arr. The space complexity is O(N).
In-place Swaps
the fact that its values are all unique nonnegative integers allows us to use a special kind of sorting algorithm whose time complexity is linear and not the standard
O(N⋅log(N))
we typically associate with efficient sorting.The high-level idea is to push every number to its corresponding index in the array. The original number in the target index is “kicked out”, so we continue to find its target index using the same approach, until the target index is out of range.
Time: Each number is at most moved once, thus the total time complexity is therefore O(N)
.
Space: O(1) constant space
Solution
Related Problems:
Last updated