Find the smallest positive number missing from an unsorted array | Set 2
Given an unsorted array with both positive and negative elements. Find the smallest positive number missing from the array in O(n) time using constant extra space. It is allowed to modify the original array.
Examples:
Input: {2, 3, 7, 6, 8, -1, -10, 15}
Output: 1
Input: { 2, 3, -7, 6, 8, 1, -10, 15 }
Output: 4
Input: {1, 1, 0, -1, -2}
Output: 2
We have discussed an O(n) time and O(1) extra space solution in the previous post. In this post, another alternative solution is discussed.
We make the value at the index corresponding to the given array element equal to an array element. For example: consider the array = {2, 3, 7, 6, 8, -1, -10, 15}. To mark the presence of element 2 in this array, we make arr[2-1] = 2. In array subscript [2-1], 2 is the element to be marked and 1 is subtracted because we are mapping an element value range [1, N] on index value range [0, N-1]. But if we make arr[1] = 2, we will lose data stored at arr[1]. To avoid this, we first store the value present at arr[1] and then update it. Next, we will mark the presence of element previously present at arr[1], i.e. 3. Clearly this lead to some type of random traversal over the array. Now we have to specify a condition to mark the end of this traversal.
There are three conditions that mark the end of this traversal:
- If the element to be marked is negative: No need to mark the presence of this element as we are interested in finding the first missing positive integer. So if a negative element is found, simply end the traversal as no more marking of the presence of an element is done.
- If the element to be marked is greater than N: No need to mark the presence of this element because if this element is present then certainly it has taken a place of an element in the range [1, N] in an array of size N and hence ensuring that our answer lies in the range[1, N]. So simply end the traversal as no more marking of the presence of an element is done.
- If the presence of the current element is already marked: Suppose the element to be marked present is val. If arr[val-1] = val, then we have already marked the presence of this element. So simply end the traversal as no more marking of the presence of an element is done.
Also note that it is possible that all the elements of an array in the range [1, N] are not marked present in the current traversal. To ensure that all the elements in the range are marked present, we check each element of the array lying in this range. If the element is not marked, then we start a new traversal beginning from that array element.
After we have marked the presence of all array elements lying in the range [1, N], we check which index value ind is not equal to ind+1. If arr[ind] is not equal to ind+1, then ind+1 is the smallest positive missing number. Recall that we are mapping index value range [0, N-1] to element value range [1, N], so 1 is added to ind. If no such ind is found, then all elements in the range [1, N] are present in the array. So the first missing positive number is N+1.
How does this solution work in O(n) time?
Observe that each element in the range [1, N] is traversed at most twice in the worst case. First while performing a traversal started from some other element in the range. Second when checking if a new traversal is required to be initiated from this element to mark the presence of unmarked elements. In the worst case, each element in the range [1, N] is present in the array and thus all N elements are traversed twice. So total computations are 2*n, and hence the time complexity is O(n).
Below is the implementation of above approach:
C++
#include <bits/stdc++.h>
using namespace std;
int findMissingNo( int arr[], int n)
{
int val;
int nextval;
for ( int i = 0; i < n; i++) {
if (arr[i] <= 0 || arr[i] > n)
continue ;
val = arr[i];
while (arr[val - 1] != val) {
nextval = arr[val - 1];
arr[val - 1] = val;
val = nextval;
if (val <= 0 || val > n)
break ;
}
}
for ( int i = 0; i < n; i++) {
if (arr[i] != i + 1) {
return i + 1;
}
}
return n + 1;
}
int main()
{
int arr[] = { 2, 3, 7, 6, 8, -1, -10, 15 };
int arr_size = sizeof (arr) / sizeof (arr[0]);
int missing = findMissingNo(arr, arr_size);
cout << "The smallest positive missing number is "
<< missing;
return 0;
}
|
Java
import java.io.*;
class GFG {
static int findMissingNo( int []arr, int n)
{
int val;
int nextval;
for ( int i = 0 ; i < n; i++) {
if (arr[i] <= 0 || arr[i] > n)
continue ;
val = arr[i];
while (arr[val - 1 ] != val) {
nextval = arr[val - 1 ];
arr[val - 1 ] = val;
val = nextval;
if (val <= 0 || val > n)
break ;
}
}
for ( int i = 0 ; i < n; i++) {
if (arr[i] != i + 1 ) {
return i + 1 ;
}
}
return n + 1 ;
}
public static void main (String[] args)
{
int arr[] = { 2 , 3 , 7 , 6 , 8 , - 1 , - 10 , 15 };
int arr_size = arr.length;
int missing = findMissingNo(arr, arr_size);
System.out.println( "The smallest positive"
+ " missing number is " + missing);
}
}
|
Python 3
def findMissingNo(arr, n):
for i in range (n) :
if (arr[i] < = 0 or arr[i] > n):
continue
val = arr[i]
while (arr[val - 1 ] ! = val):
nextval = arr[val - 1 ]
arr[val - 1 ] = val
val = nextval
if (val < = 0 or val > n):
break
for i in range (n):
if (arr[i] ! = i + 1 ) :
return i + 1
return n + 1
if __name__ = = "__main__" :
arr = [ 2 , 3 , 7 , 6 , 8 , - 1 , - 10 , 15 ]
arr_size = len (arr)
missing = findMissingNo(arr, arr_size)
print ( "The smallest positive" ,
"missing number is " , missing)
|
C#
using System;
class GFG
{
static int findMissingNo( int []arr,
int n)
{
int val;
int nextval;
for ( int i = 0; i < n; i++)
{
if (arr[i] <= 0 || arr[i] > n)
continue ;
val = arr[i];
while (arr[val - 1] != val)
{
nextval = arr[val - 1];
arr[val - 1] = val;
val = nextval;
if (val <= 0 || val > n)
break ;
}
}
for ( int i = 0; i < n; i++)
{
if (arr[i] != i + 1)
{
return i + 1;
}
}
return n + 1;
}
public static void Main (String[] args)
{
int []arr = {2, 3, 7, 6,
8, -1, -10, 15};
int arr_size = arr.Length;
int missing = findMissingNo(arr, arr_size);
Console.Write( "The smallest positive" +
" missing number is " +
missing);
}
}
|
PHP
<?php
function findMissingNo( $arr , $n )
{
$val ;
$nextval ;
for ( $i = 0; $i < $n ; $i ++)
{
if ( $arr [ $i ] <= 0 ||
$arr [ $i ] > $n )
continue ;
$val = $arr [ $i ];
while ( $arr [ $val - 1] != $val )
{
$nextval = $arr [ $val - 1];
$arr [ $val - 1] = $val ;
$val = $nextval ;
if ( $val <= 0 ||
$val > $n )
break ;
}
}
for ( $i = 0; $i < $n ; $i ++)
{
if ( $arr [ $i ] != $i + 1)
{
return $i + 1;
}
}
return $n + 1;
}
$arr = array (2, 3, 7, 6, 8,
-1, -10, 15);
$arr_size = sizeof( $arr ) /
sizeof( $arr [0]);
$missing = findMissingNo( $arr ,
$arr_size );
echo "The smallest positive " .
"missing number is " ,
$missing ;
?>
|
Javascript
<script>
function findMissingNo(arr, n)
{
var val;
var nextval;
for ( var i = 0; i < n; i++) {
if (arr[i] <= 0 || arr[i] > n)
continue ;
val = arr[i];
while (arr[val - 1] != val) {
nextval = arr[val - 1];
arr[val - 1] = val;
val = nextval;
if (val <= 0 || val > n)
break ;
}
}
for ( var i = 0; i < n; i++) {
if (arr[i] != i + 1) {
return i + 1;
}
}
return n + 1;
}
var arr = [ 2, 3, 7, 6, 8, -1, -10, 15 ];
var arr_size = arr.length;
var missing = findMissingNo(arr, arr_size);
document.write( "The smallest positive missing number is "
+ missing);
</script>
|
Output
The smallest positive missing number is 1
Complexity Analysis:
- Time Complexity: O(n)
- Auxiliary Space: O(1)
Last Updated :
22 Aug, 2022
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...