Smallest missing non-negative integer upto every array index - GeeksforGeeks

10 Nov, 2021
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 4Explanation: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++
Java
Python3
C#
Javascript
// 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 indicesvoid 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 Codeint main(){    int arr[] = { 0, 1, 2, 3, 5 };    int N = sizeof(arr) / sizeof(arr[0]);    smlstNonNeg(arr, N);}
// Java program to implement// the above approachimport java.io.*;import java.util.Arrays; class GFG{  // Function to print the smallest// missing non-negative integer// up to every array indicesstatic 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 Codepublic 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 program to implement# the above approach # Function to print smallest# missing non-negative integer# up to every array indicesdef 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 Codeif __name__ == '__main__':         arr = [ 0, 1, 2, 3, 5 ]    N = len(arr)         smlstNonNeg(arr, N) # This code is contributed by mohit kumar 29
// C# program to implement// the above approachusing System;  class GFG{   // Function to print the smallest// missing non-negative integer// up to every array indicesstatic 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 Codepublic static void Main (){    int[] arr = { 0, 1, 2, 3, 5 };    int N = arr.Length;          smlstNonNeg(arr, N);}} // This code is contributed by code_hunt
<script> // Javascript program to implement// the above approach // Function to print the smallest// missing non-negative integer// up to every array indicesfunction 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 Codelet 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)
mohit kumar 29
sanjoy_62
code_hunt
target_2
simranarora5sos
khushboogoyal499
ankita_saini
frequency-counting
Arrays
Greedy
Hash
Mathematical
Arrays
Hash
Greedy
Mathematical
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Comments
Old Comments
Stack Data Structure (Introduction and Program)
Top 50 Array Coding Problems for Interviews
Introduction to Arrays
Multidimensional Arrays in Java
Linear Search
Dijkstra's shortest path algorithm | Greedy Algo-7
Prim’s Minimum Spanning Tree (MST) | Greedy Algo-5
Kruskal’s Minimum Spanning Tree Algorithm | Greedy Algo-2
Huffman Coding | Greedy Algo-3
Write a program to print all permutations of a given string