Given an array arr[] of size N, the task is for every array indices is to find the smallest missing non-negative integer upto that index of the given array.
Examples:
Input: arr[] = {1, 3, 0, 2}
Output: 0 0 2 4
Explanation:
Smallest missing non-negative integer from index 0 to 0 is 0.
Smallest missing non-negative integer from index 0 to 1 is 0.
Smallest missing non-negative integer from index 0 to 2 is 2.
Smallest missing non-negative integer from index 0 to 3 is 4.Input: arr[] = {0, 1, 2, 3, 5}
Output: 1 2 3 4 4
Approach: This problem can be solved using Hashing. Follow the steps below to solve the problem:
- Initialize a variable, say smNonNeg to store the smallest missing non-negative integers between the start index and the current index of the given array.
- Initialize an array, say hash[N] to check if smNonNeg present between the start index and the current index or not.
- Traverse the given array and check if hash[smNonNeg] equal to 0 or not. If found to be true, then print the value of smNonNeg.
- Otherwise, increment the value of smNonNeg while hash[smNonNeg] not equal to 0.
Below is the implementation of the above approach:
C++
// C++ program to implement // the above approach #include <bits/stdc++.h> using namespace std; // Function to print the smallest // missing non-negative integer // up to every array indices void smlstNonNeg( int arr[], int N) { // Stores the smallest missing // non-negative integers between // start index to current index int smNonNeg = 0; // Store the boolean value to check // smNonNeg present between start // index to each index of the array bool hash[N + 1] = { 0 }; // Traverse the array for ( int i = 0; i < N; i++) { // Since output always lies // in the range[0, N - 1] if (arr[i] >= 0 and arr[i] < N) { hash[arr[i]] = true ; } // Check if smNonNeg is // present between start index // and current index or not while (hash[smNonNeg]) { smNonNeg++; } // Print smallest missing // non-negative integer cout << smNonNeg << " " ; } } // Driver Code int main() { int arr[] = { 0, 1, 2, 3, 5 }; int N = sizeof (arr) / sizeof (arr[0]); smlstNonNeg(arr, N); } |
Java
// Java program to implement // the above approach import java.io.*; import java.util.Arrays; class GFG{ // Function to print the smallest // missing non-negative integer // up to every array indices static void smlstNonNeg( int arr[], int N) { // Stores the smallest missing // non-negative integers between // start index to current index int smNonNeg = 0 ; // Store the boolean value to check // smNonNeg present between start // index to each index of the array Boolean[] hash = new Boolean[N + 1 ]; Arrays.fill(hash, false ); // Traverse the array for ( int i = 0 ; i < N; i++) { // Since output always lies // in the range[0, N - 1] if (arr[i] >= 0 && arr[i] < N) { hash[arr[i]] = true ; } // Check if smNonNeg is // present between start index // and current index or not while (hash[smNonNeg]) { smNonNeg++; } // Print smallest missing // non-negative integer System.out.print(smNonNeg + " " ); } } // Driver Code public static void main (String[] args) { int arr[] = { 0 , 1 , 2 , 3 , 5 }; int N = arr.length; smlstNonNeg(arr, N); } } // This code is contributed by sanjoy_62 |
Python3
# Python3 program to implement # the above approach # Function to print smallest # missing non-negative integer # up to every array indices def smlstNonNeg(arr, N): # Stores the smallest missing # non-negative integers between # start index to current index smNonNeg = 0 # Store the boolean value to check # smNonNeg present between start # index to each index of the array hash = [ 0 ] * (N + 1 ) # Traverse the array for i in range (N): # Since output always lies # in the range[0, N - 1] if (arr[i] > = 0 and arr[i] < N): hash [arr[i]] = True # Check if smNonNeg is # present between start index # and current index or not while ( hash [smNonNeg]): smNonNeg + = 1 # Print smallest missing # non-negative integer print (smNonNeg, end = " " ) # Driver Code if __name__ = = '__main__' : arr = [ 0 , 1 , 2 , 3 , 5 ] N = len (arr) smlstNonNeg(arr, N) # This code is contributed by mohit kumar 29 |
C#
// C# program to implement // the above approach using System; class GFG{ // Function to print the smallest // missing non-negative integer // up to every array indices static void smlstNonNeg( int [] arr, int N) { // Stores the smallest missing // non-negative integers between // start index to current index int smNonNeg = 0; // Store the boolean value to check // smNonNeg present between start // index to each index of the array bool [] hash = new bool [N + 1]; for ( int i = 0; i < N; i++) { hash[i] = false ; } // Traverse the array for ( int i = 0; i < N; i++) { // Since output always lies // in the range[0, N - 1] if (arr[i] >= 0 && arr[i] < N) { hash[arr[i]] = true ; } // Check if smNonNeg is // present between start index // and current index or not while (hash[smNonNeg]) { smNonNeg++; } // Print smallest missing // non-negative integer Console.Write(smNonNeg + " " ); } } // Driver Code public static void Main () { int [] arr = { 0, 1, 2, 3, 5 }; int N = arr.Length; smlstNonNeg(arr, N); } } // This code is contributed by code_hunt |
Javascript
<script> // Javascript program to implement // the above approach // Function to print the smallest // missing non-negative integer // up to every array indices function smlstNonNeg(arr, N) { // Stores the smallest missing // non-negative integers between // start index to current index let smNonNeg = 0; // Store the boolean value to check // smNonNeg present between start // index to each index of the array let hash = []; for (let i = 0; i < N; i++) { hash[i] = false ; } // Traverse the array for (let i = 0; i < N; i++) { // Since output always lies // in the range[0, N - 1] if (arr[i] >= 0 && arr[i] < N) { hash[arr[i]] = true ; } // Check if smNonNeg is // present between start index // and current index or not while (hash[smNonNeg]) { smNonNeg++; } // Print smallest missing // non-negative integer document.write(smNonNeg + " " ); } } // Driver Code let arr = [ 0, 1, 2, 3, 5 ]; let N = arr.length; smlstNonNeg(arr, N); // This code is contributed by target_2 </script> |
1 2 3 4 4
Time Complexity: O(N)
Auxiliary Space: O(N)
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 neveropen!