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 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
// 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
# 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#
// 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 |
Javascript
<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)
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 neveropen!
