Smallest missing non-negative integer upto every array index
Last Updated :
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 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++
#include <bits/stdc++.h>
using namespace std;
void smlstNonNeg( int arr[], int N)
{
int smNonNeg = 0;
bool hash[N + 1] = { 0 };
for ( int i = 0; i < N; i++) {
if (arr[i] >= 0 and arr[i] < N) {
hash[arr[i]] = true ;
}
while (hash[smNonNeg]) {
smNonNeg++;
}
cout << smNonNeg << " " ;
}
}
int main()
{
int arr[] = { 0, 1, 2, 3, 5 };
int N = sizeof (arr) / sizeof (arr[0]);
smlstNonNeg(arr, N);
}
|
Java
import java.io.*;
import java.util.Arrays;
class GFG{
static void smlstNonNeg( int arr[], int N)
{
int smNonNeg = 0 ;
Boolean[] hash = new Boolean[N + 1 ];
Arrays.fill(hash, false );
for ( int i = 0 ; i < N; i++)
{
if (arr[i] >= 0 && arr[i] < N)
{
hash[arr[i]] = true ;
}
while (hash[smNonNeg])
{
smNonNeg++;
}
System.out.print(smNonNeg + " " );
}
}
public static void main (String[] args)
{
int arr[] = { 0 , 1 , 2 , 3 , 5 };
int N = arr.length;
smlstNonNeg(arr, N);
}
}
|
Python3
def smlstNonNeg(arr, N):
smNonNeg = 0
hash = [ 0 ] * (N + 1 )
for i in range (N):
if (arr[i] > = 0 and arr[i] < N):
hash [arr[i]] = True
while ( hash [smNonNeg]):
smNonNeg + = 1
print (smNonNeg, end = " " )
if __name__ = = '__main__' :
arr = [ 0 , 1 , 2 , 3 , 5 ]
N = len (arr)
smlstNonNeg(arr, N)
|
C#
using System;
class GFG{
static void smlstNonNeg( int [] arr, int N)
{
int smNonNeg = 0;
bool [] hash = new bool [N + 1];
for ( int i = 0; i < N; i++)
{
hash[i] = false ;
}
for ( int i = 0; i < N; i++)
{
if (arr[i] >= 0 && arr[i] < N)
{
hash[arr[i]] = true ;
}
while (hash[smNonNeg])
{
smNonNeg++;
}
Console.Write(smNonNeg + " " );
}
}
public static void Main ()
{
int [] arr = { 0, 1, 2, 3, 5 };
int N = arr.Length;
smlstNonNeg(arr, N);
}
}
|
Javascript
<script>
function smlstNonNeg(arr, N)
{
let smNonNeg = 0;
let hash = [];
for (let i = 0; i < N; i++)
{
hash[i] = false ;
}
for (let i = 0; i < N; i++)
{
if (arr[i] >= 0 && arr[i] < N)
{
hash[arr[i]] = true ;
}
while (hash[smNonNeg])
{
smNonNeg++;
}
document.write(smNonNeg + " " );
}
}
let arr = [ 0, 1, 2, 3, 5 ];
let N = arr.length;
smlstNonNeg(arr, N);
</script>
|
Time Complexity: O(N)
Auxiliary Space: O(N)
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...