Posted on by Kalkicode
Code Array

# Smallest positive missing number in an array

The problem at hand is to find the smallest positive missing number in an array of integers. Given an array, the task is to determine the smallest positive integer that is not present in the array.

## Problem Statement

Given an array of integers, the task is to find the smallest positive missing number.

## Explanation with Examples

• Test A
• The array is `[1, 2, -4, 5, -4, 3, -5]`.
• The positive integers in the array are `[1, 2, 5, 3]`.
• The smallest positive missing integer is `4`.
• Test B
• The array is `[1, -3, 4, 0, 6, 2]`.
• The positive integers in the array are `[1, 4, 6, 2]`.
• The smallest positive missing integer is `3`.
• Test C
• The array is `[3, 4, 1, 2]`.
• The positive integers in the array are `[3, 4, 1, 2]`.
• The smallest positive missing integer is `5`.

## Idea to Solve the Problem

To solve this problem, we can use an auxiliary array to track the presence of positive integers. We initialize an auxiliary array `check` with zeros. For each positive integer `x` encountered in the original array, we set `check[x - 1]` to `1` to indicate that the number `x` is present in the array. Then, we iterate through the `check` array to find the first index where the value is `0`, and the index plus `1` represents the smallest missing positive number.

## Pseudocode

``````Function printArray(arr, size):
Loop i from 0 to size - 1:
Print arr[i] with spaces

Function firstPositiveSmallest(arr, n):
If n is less than or equal to 0:
Return

Create an array check of size n, initialized with zeros

Loop i from 0 to n - 1:
If arr[i] is positive and less than or equal to n:
Set check[arr[i] - 1] to 1

Initialize result as 0

While result is less than n and check[result] is 1:
Increment result

Print "Array :"
Call printArray(arr, n)
Print "Result :", result + 1

Main:
Define array elements
n = size of array
Call firstPositiveSmallest(arr, n)``````

## Algorithm Explanation

1. The `firstPositiveSmallest` function initializes an array `check` with zeros to keep track of the presence of positive integers.
2. It then iterates through the array and marks the presence of positive integers in the `check` array.
3. After marking the presence of positive integers, the function iterates through the `check` array to find the first index with a value of `0`, and the index plus `1` represents the smallest missing positive number.
``````/*
C program for
Smallest positive missing number in an array
*/
#include <stdio.h>

// Display array elements
void printArray(int arr[], int n)
{
for (int i = 0; i < n; ++i)
{
printf("  %d", arr[i]);
}
}
void firstPositiveSmallest(int arr[], int n)
{
if (n <= 0)
{
return;
}
int check[n];
// Set initial value of auxiliary array
for (int i = 0; i < n; ++i)
{
// Zero are indicate number is missing
check[i] = 0;
}
for (int i = 0; i < n; ++i)
{
if (arr[i] > 0 && arr[i] <= n)
{
// When number exist between 1 to n
check[arr[i] - 1] = 1;
}
}
int result = 0;
// This loop are executed until not find smallest missing number
while (result < n && check[result] == 1)
{
result++;
}
printf("\n Array : ");
printArray(arr, n);
// Display calculated result
printf("\n  Result :  %d \n", result + 1);
}
int main(int argc, char
const *argv[])
{
int arr1[] = {
1 , 2 , -4 , 5 , -4 , 3 , -5
};
int arr2[] = {
1 , -3 , 4 , 0 , 6 , 2
};
int arr3[] = {
3 , 4 , 1 , 2
};
// Get the size of arrays
int l = sizeof(arr1) / sizeof(arr1[0]);
int m = sizeof(arr2) / sizeof(arr2[0]);
int n = sizeof(arr3) / sizeof(arr3[0]);
// Test A
/*
arr =  [1, 2, -4, 5, -4, 3, -5]

[1,2,5,3]  Positive number
---------------------
Smallest missing : 4
*/
firstPositiveSmallest(arr1, l);
// Test B
/*
arr =  [1, -3, 4, 0, 6, 2]

[1,4,6,2]  Positive number
---------------------
Smallest missing : 3
*/
firstPositiveSmallest(arr2, m);
// Test C
/*
arr =  [3,4, 1, 2]

[3,4, 1, 2]  Positive number
---------------------
Smallest missing : 5
*/
firstPositiveSmallest(arr3, n);
return 0;
}``````

#### Output

`````` Array :   1  2  -4  5  -4  3  -5
Result :  4

Array :   1  -3  4  0  6  2
Result :  3

Array :   3  4  1  2
Result :  5``````
``````/*
Java program for
Smallest positive missing number in an array
*/
public class Missing
{
// Display array elements
public void printArray(int[] arr, int n)
{
for (int i = 0; i < n; ++i)
{
System.out.print(" " + arr[i]);
}
}
public void firstPositiveSmallest(int[] arr, int n)
{
if (n <= 0)
{
return;
}
boolean[] check = new boolean[n];
// Set initial value of auxiliary array
for (int i = 0; i < n; ++i)
{
// False are indicate number is missing
check[i] = false;
}
for (int i = 0; i < n; ++i)
{
if (arr[i] > 0 && arr[i] <= n)
{
// When number exist between 1 to n
check[arr[i] - 1] = true;
}
}
int result = 0;
// This loop are executed until not find smallest missing number
while (result < n && check[result] == true)
{
result++;
}
System.out.print("\n Array : ");
printArray(arr, n);
// Display calculated result
System.out.print("\n Result : " + (result + 1) + " \n");
}
public static void main(String[] args)
{
int[] arr1 = {
1 , 2 , -4 , 5 , -4 , 3 , -5
};
int[] arr2 = {
1 , -3 , 4 , 0 , 6 , 2
};
int[] arr3 = {
3 , 4 , 1 , 2
};
// Get the size of arrays
int l = arr1.length;
int m = arr2.length;
int n = arr3.length;
// Test A
/*
arr =  [1, 2, -4, 5, -4, 3, -5]

[1,2,5,3]  Positive number
---------------------
Smallest missing : 4
*/
// Test B
/*
arr =  [1, -3, 4, 0, 6, 2]

[1,4,6,2]  Positive number
---------------------
Smallest missing : 3
*/
// Test C
/*
arr =  [3,4, 1, 2]

[3,4, 1, 2]  Positive number
---------------------
Smallest missing : 5
*/
}
}``````

#### Output

`````` Array :  1 2 -4 5 -4 3 -5
Result : 4

Array :  1 -3 4 0 6 2
Result : 3

Array :  3 4 1 2
Result : 5``````
``````// Include header file
#include <iostream>

using namespace std;
/*
C++ program for
Smallest positive missing number in an array
*/
class Missing
{
public:
// Display array elements
void printArray(int arr[], int n)
{
for (int i = 0; i < n; ++i)
{
cout << " " << arr[i];
}
}
void firstPositiveSmallest(int arr[], int n)
{
if (n <= 0)
{
return;
}
bool check[n];
// Set initial value of auxiliary array
for (int i = 0; i < n; ++i)
{
// False are indicate number is missing
check[i] = false;
}
for (int i = 0; i < n; ++i)
{
if (arr[i] > 0 && arr[i] <= n)
{
// When number exist between 1 to n
check[arr[i] - 1] = true;
}
}
int result = 0;
// This loop are executed until not find smallest missing number
while (result < n && check[result] == true)
{
result++;
}
cout << "\n Array : ";
this->printArray(arr, n);
// Display calculated result
cout << "\n Result : " << (result + 1) << " \n";
}
};
int main()
{
int arr1[] = {
1 , 2 , -4 , 5 , -4 , 3 , -5
};
int arr2[] = {
1 , -3 , 4 , 0 , 6 , 2
};
int arr3[] = {
3 , 4 , 1 , 2
};
// Get the size of arrays
int l = sizeof(arr1) / sizeof(arr1[0]);
int m = sizeof(arr2) / sizeof(arr2[0]);
int n = sizeof(arr3) / sizeof(arr3[0]);
// Test A
/*
arr =  [1, 2, -4, 5, -4, 3, -5]
[1,2,5,3]  Positive number
---------------------
Smallest missing : 4
*/
// Test B
/*
arr =  [1, -3, 4, 0, 6, 2]
[1,4,6,2]  Positive number
---------------------
Smallest missing : 3
*/
// Test C
/*
arr =  [3,4, 1, 2]
[3,4, 1, 2]  Positive number
---------------------
Smallest missing : 5
*/
return 0;
}``````

#### Output

`````` Array :  1 2 -4 5 -4 3 -5
Result : 4

Array :  1 -3 4 0 6 2
Result : 3

Array :  3 4 1 2
Result : 5``````
``````// Include namespace system
using System;
/*
Csharp program for
Smallest positive missing number in an array
*/
public class Missing
{
// Display array elements
public void printArray(int[] arr, int n)
{
for (int i = 0; i < n; ++i)
{
Console.Write(" " + arr[i]);
}
}
public void firstPositiveSmallest(int[] arr, int n)
{
if (n <= 0)
{
return;
}
Boolean[] check = new Boolean[n];
// Set initial value of auxiliary array
for (int i = 0; i < n; ++i)
{
// False are indicate number is missing
check[i] = false;
}
for (int i = 0; i < n; ++i)
{
if (arr[i] > 0 && arr[i] <= n)
{
// When number exist between 1 to n
check[arr[i] - 1] = true;
}
}
int result = 0;
// This loop are executed until not find smallest missing number
while (result < n && check[result] == true)
{
result++;
}
Console.Write("\n Array : ");
this.printArray(arr, n);
// Display calculated result
Console.Write("\n Result : " + (result + 1) + " \n");
}
public static void Main(String[] args)
{
int[] arr1 = {
1 , 2 , -4 , 5 , -4 , 3 , -5
};
int[] arr2 = {
1 , -3 , 4 , 0 , 6 , 2
};
int[] arr3 = {
3 , 4 , 1 , 2
};
// Get the size of arrays
int l = arr1.Length;
int m = arr2.Length;
int n = arr3.Length;
// Test A
/*
arr =  [1, 2, -4, 5, -4, 3, -5]
[1,2,5,3]  Positive number
---------------------
Smallest missing : 4
*/
// Test B
/*
arr =  [1, -3, 4, 0, 6, 2]
[1,4,6,2]  Positive number
---------------------
Smallest missing : 3
*/
// Test C
/*
arr =  [3,4, 1, 2]
[3,4, 1, 2]  Positive number
---------------------
Smallest missing : 5
*/
}
}``````

#### Output

`````` Array :  1 2 -4 5 -4 3 -5
Result : 4

Array :  1 -3 4 0 6 2
Result : 3

Array :  3 4 1 2
Result : 5``````
``````package main
import "fmt"
/*
Go program for
Smallest positive missing number in an array
*/
// Display array elements
func printArray(arr[] int, n int) {
for i := 0 ; i < n ; i++ {
fmt.Print(" ", arr[i])
}
}
func firstPositiveSmallest(arr[] int, n int) {
if n <= 0 {
return
}
// Set initial value of auxiliary array
// False are indicate number is missing
var check = make([] bool, n)
for i := 0 ; i < n ; i++ {
if arr[i] > 0 && arr[i] <= n {
// When number exist between 1 to n
check[arr[i] - 1] = true
}
}
var result int = 0
// This loop are executed until not find smallest missing number
for (result < n && check[result] == true) {
result++
}
fmt.Print("\n Array : ")
printArray(arr, n)
// Display calculated result
fmt.Print("\n Result : ", (result + 1), " \n")
}
func main() {

var arr1 = [] int { 1, 2, -4, 5, -4, 3, -5 }
var arr2 = [] int { 1, -3, 4, 0, 6, 2 }
var arr3 = [] int { 3,4, 1, 2 }
// Get the size of arrays
var l int = len(arr1)
var m int = len(arr2)
var n int = len(arr3)
// Test A
/*
arr =  [1, 2, -4, 5, -4, 3, -5]
[1,2,5,3]  Positive number
---------------------
Smallest missing : 4
*/
firstPositiveSmallest(arr1, l)
// Test B
/*
arr =  [1, -3, 4, 0, 6, 2]
[1,4,6,2]  Positive number
---------------------
Smallest missing : 3
*/
firstPositiveSmallest(arr2, m)
// Test C
/*
arr =  [3,4, 1, 2]
[3,4, 1, 2]  Positive number
---------------------
Smallest missing : 5
*/
firstPositiveSmallest(arr3, n)
}``````

#### Output

`````` Array :  1 2 -4 5 -4 3 -5
Result : 4

Array :  1 -3 4 0 6 2
Result : 3

Array :  3 4 1 2
Result : 5``````
``````<?php
/*
Php program for
Smallest positive missing number in an array
*/
class Missing
{
// Display array elements
public	function printArray(\$arr, \$n)
{
for (\$i = 0; \$i < \$n; ++\$i)
{
echo(" ".\$arr[\$i]);
}
}
public	function firstPositiveSmallest(\$arr, \$n)
{
if (\$n <= 0)
{
return;
}
// Set initial value of auxiliary array
// False are indicate number is missing
\$check = array_fill(0, \$n, false);
for (\$i = 0; \$i < \$n; ++\$i)
{
if (\$arr[\$i] > 0 && \$arr[\$i] <= \$n)
{
// When number exist between 1 to n
\$check[\$arr[\$i] - 1] = true;
}
}
\$result = 0;
// This loop are executed until not find smallest missing number
while (\$result < \$n && \$check[\$result] == true)
{
\$result++;
}
echo("\n Array : ");
\$this->printArray(\$arr, \$n);
// Display calculated result
echo("\n Result : ".(\$result + 1).
" \n");
}
}

function main()
{
\$arr1 = array(1, 2, -4, 5, -4, 3, -5);
\$arr2 = array(1, -3, 4, 0, 6, 2);
\$arr3 = array(3, 4, 1, 2);
// Get the size of arrays
\$l = count(\$arr1);
\$m = count(\$arr2);
\$n = count(\$arr3);
// Test A
/*
arr =  [1, 2, -4, 5, -4, 3, -5]
[1,2,5,3]  Positive number
---------------------
Smallest missing : 4
*/
// Test B
/*
arr =  [1, -3, 4, 0, 6, 2]
[1,4,6,2]  Positive number
---------------------
Smallest missing : 3
*/
// Test C
/*
arr =  [3,4, 1, 2]
[3,4, 1, 2]  Positive number
---------------------
Smallest missing : 5
*/
}
main();``````

#### Output

`````` Array :  1 2 -4 5 -4 3 -5
Result : 4

Array :  1 -3 4 0 6 2
Result : 3

Array :  3 4 1 2
Result : 5``````
``````/*
Node JS program for
Smallest positive missing number in an array
*/
class Missing
{
// Display array elements
printArray(arr, n)
{
for (var i = 0; i < n; ++i)
{
process.stdout.write(" " + arr[i]);
}
}
firstPositiveSmallest(arr, n)
{
if (n <= 0)
{
return;
}
// Set initial value of auxiliary array
// False are indicate number is missing
var check = Array(n).fill(false);
for (var i = 0; i < n; ++i)
{
if (arr[i] > 0 && arr[i] <= n)
{
// When number exist between 1 to n
check[arr[i] - 1] = true;
}
}
var result = 0;
// This loop are executed until not find smallest missing number
while (result < n && check[result] == true)
{
result++;
}
process.stdout.write("\n Array : ");
this.printArray(arr, n);
// Display calculated result
process.stdout.write("\n Result : " + (result + 1) + " \n");
}
}

function main()
{
var arr1 = [1, 2, -4, 5, -4, 3, -5];
var arr2 = [1, -3, 4, 0, 6, 2];
var arr3 = [3, 4, 1, 2];
// Get the size of arrays
var l = arr1.length;
var m = arr2.length;
var n = arr3.length;
// Test A
/*
arr =  [1, 2, -4, 5, -4, 3, -5]
[1,2,5,3]  Positive number
---------------------
Smallest missing : 4
*/
// Test B
/*
arr =  [1, -3, 4, 0, 6, 2]
[1,4,6,2]  Positive number
---------------------
Smallest missing : 3
*/
// Test C
/*
arr =  [3,4, 1, 2]
[3,4, 1, 2]  Positive number
---------------------
Smallest missing : 5
*/
}
main();``````

#### Output

`````` Array :  1 2 -4 5 -4 3 -5
Result : 4

Array :  1 -3 4 0 6 2
Result : 3

Array :  3 4 1 2
Result : 5``````
``````#    Python 3 program for
#    Smallest positive missing number in an array
class Missing :
#  Display list elements
def printArray(self, arr, n) :
i = 0
while (i < n) :
print(" ", arr[i], end = "")
i += 1

def firstPositiveSmallest(self, arr, n) :
if (n <= 0) :
return

#  Set initial value of auxiliary list
#  False are indicate number is missing
check = [False] * (n)
i = 0
while (i < n) :
if (arr[i] > 0 and arr[i] <= n) :
#  When number exist between 1 to n
check[arr[i] - 1] = True

i += 1

result = 0
#  This loop are executed until not find smallest missing number
while (result < n and check[result] == True) :
result += 1

print("\n Array : ", end = "")
self.printArray(arr, n)
#  Display calculated result
print("\n Result : ", (result + 1) ," ")

def main() :
arr1 = [1, 2, -4, 5, -4, 3, -5]
arr2 = [1, -3, 4, 0, 6, 2]
arr3 = [3, 4, 1, 2]
#  Get the size of lists
l = len(arr1)
m = len(arr2)
n = len(arr3)
#  Test A
#   arr =  [1, 2, -4, 5, -4, 3, -5]
#   [1,2,5,3]  Positive number
#   ---------------------
#   Smallest missing : 4
#  Test B
#   arr =  [1, -3, 4, 0, 6, 2]
#   [1,4,6,2]  Positive number
#   ---------------------
#   Smallest missing : 3
#  Test C
#   arr =  [3,4, 1, 2]
#   [3,4, 1, 2]  Positive number
#   ---------------------
#   Smallest missing : 5

if __name__ == "__main__": main()``````

#### Output

`````` Array :   1  2  -4  5  -4  3  -5
Result :  4

Array :   1  -3  4  0  6  2
Result :  3

Array :   3  4  1  2
Result :  5``````
``````#    Ruby program for
#    Smallest positive missing number in an array
class Missing
#  Display array elements
def printArray(arr, n)
i = 0
while (i < n)
print(" ", arr[i])
i += 1
end

end

def firstPositiveSmallest(arr, n)
if (n <= 0)
return
end

#  Set initial value of auxiliary array
#  False are indicate number is missing
check = Array.new(n) {false}
i = 0
while (i < n)
if (arr[i] > 0 && arr[i] <= n)
#  When number exist between 1 to n
check[arr[i] - 1] = true
end

i += 1
end

result = 0
#  This loop are executed until not find smallest missing number
while (result < n && check[result] == true)
result += 1
end

print("\n Array : ")
self.printArray(arr, n)
#  Display calculated result
print("\n Result : ", (result + 1) ," \n")
end

end

def main()
arr1 = [1, 2, -4, 5, -4, 3, -5]
arr2 = [1, -3, 4, 0, 6, 2]
arr3 = [3, 4, 1, 2]
#  Get the size of arrays
l = arr1.length
m = arr2.length
n = arr3.length
#  Test A
#   arr =  [1, 2, -4, 5, -4, 3, -5]
#   [1,2,5,3]  Positive number
#   ---------------------
#   Smallest missing : 4
#  Test B
#   arr =  [1, -3, 4, 0, 6, 2]
#   [1,4,6,2]  Positive number
#   ---------------------
#   Smallest missing : 3
#  Test C
#   arr =  [3,4, 1, 2]
#   [3,4, 1, 2]  Positive number
#   ---------------------
#   Smallest missing : 5
end

main()``````

#### Output

`````` Array :  1 2 -4 5 -4 3 -5
Result : 4

Array :  1 -3 4 0 6 2
Result : 3

Array :  3 4 1 2
Result : 5
``````
``````/*
Scala program for
Smallest positive missing number in an array
*/
class Missing()
{
// Display array elements
def printArray(arr: Array[Int], n: Int): Unit = {
var i: Int = 0;
while (i < n)
{
print(" " + arr(i));
i += 1;
}
}
def firstPositiveSmallest(arr: Array[Int], n: Int): Unit = {
if (n <= 0)
{
return;
}
// Set initial value of auxiliary array
// False are indicate number is missing
var check: Array[Boolean] = Array.fill[Boolean](n)(false);
var i: Int = 0;
while (i < n)
{
if (arr(i) > 0 && arr(i) <= n)
{
// When number exist between 1 to n
check(arr(i) - 1) = true;
}
i += 1;
}
var result: Int = 0;
// This loop are executed until not find smallest missing number
while (result < n && check(result) == true)
{
result += 1;
}
print("\n Array : ");
printArray(arr, n);
// Display calculated result
print("\n Result : " + (result + 1) + " \n");
}
}
object Main
{
def main(args: Array[String]): Unit = {
var task: Missing = new Missing();
var arr1: Array[Int] = Array(1, 2, -4, 5, -4, 3, -5);
var arr2: Array[Int] = Array(1, -3, 4, 0, 6, 2);
var arr3: Array[Int] = Array(3, 4, 1, 2);
// Get the size of arrays
var l: Int = arr1.length;
var m: Int = arr2.length;
var n: Int = arr3.length;
// Test A
/*
arr =  [1, 2, -4, 5, -4, 3, -5]
[1,2,5,3]  Positive number
---------------------
Smallest missing : 4
*/
// Test B
/*
arr =  [1, -3, 4, 0, 6, 2]
[1,4,6,2]  Positive number
---------------------
Smallest missing : 3
*/
// Test C
/*
arr =  [3,4, 1, 2]
[3,4, 1, 2]  Positive number
---------------------
Smallest missing : 5
*/
}
}``````

#### Output

`````` Array :  1 2 -4 5 -4 3 -5
Result : 4

Array :  1 -3 4 0 6 2
Result : 3

Array :  3 4 1 2
Result : 5``````
``````/*
Swift 4 program for
Smallest positive missing number in an array
*/
class Missing
{
// Display array elements
func printArray(_ arr: [Int], _ n: Int)
{
var i: Int = 0;
while (i < n)
{
print(" ", arr[i], terminator: "");
i += 1;
}
}
func firstPositiveSmallest(_ arr: [Int], _ n: Int)
{
if (n <= 0)
{
return;
}
// Set initial value of auxiliary array
// False are indicate number is missing
var check: [Bool] = Array(repeating: false, count: n);
var i: Int = 0;
while (i < n)
{
if (arr[i] > 0 && arr[i] <= n)
{
// When number exist between 1 to n
check[arr[i] - 1] = true;
}
i += 1;
}
var result: Int = 0;
// This loop are executed until not find smallest missing number
while (result < n && check[result] == true)
{
result += 1;
}
print("\n Array : ", terminator: "");
self.printArray(arr, n);
// Display calculated result
print("\n Result : ", (result + 1) ," ");
}
}
func main()
{
let arr1: [Int] = [1, 2, -4, 5, -4, 3, -5];
let arr2: [Int] = [1, -3, 4, 0, 6, 2];
let arr3: [Int] = [3, 4, 1, 2];
// Get the size of arrays
let l: Int = arr1.count;
let m: Int = arr2.count;
let n: Int = arr3.count;
// Test A
/*
arr =  [1, 2, -4, 5, -4, 3, -5]
[1,2,5,3]  Positive number
---------------------
Smallest missing : 4
*/
// Test B
/*
arr =  [1, -3, 4, 0, 6, 2]
[1,4,6,2]  Positive number
---------------------
Smallest missing : 3
*/
// Test C
/*
arr =  [3,4, 1, 2]
[3,4, 1, 2]  Positive number
---------------------
Smallest missing : 5
*/
}
main();``````

#### Output

`````` Array :   1  2  -4  5  -4  3  -5
Result :  4

Array :   1  -3  4  0  6  2
Result :  3

Array :   3  4  1  2
Result :  5``````
``````/*
Kotlin program for
Smallest positive missing number in an array
*/
class Missing
{
// Display array elements
fun printArray(arr: Array < Int > , n: Int): Unit
{
var i: Int = 0;
while (i < n)
{
print(" " + arr[i]);
i += 1;
}
}
fun firstPositiveSmallest(arr: Array < Int > , n: Int): Unit
{
if (n <= 0)
{
return;
}
// Set initial value of auxiliary array
// False are indicate number is missing
val check: Array < Boolean > = Array(n)
{
false
};
var i: Int = 0;
while (i < n)
{
if (arr[i] > 0 && arr[i] <= n)
{
// When number exist between 1 to n
check[arr[i] - 1] = true;
}
i += 1;
}
var result: Int = 0;
// This loop are executed until not find smallest missing number
while (result < n && check[result] == true)
{
result += 1;
}
print("\n Array : ");
this.printArray(arr, n);
// Display calculated result
print("\n Result : " + (result + 1) + " \n");
}
}
fun main(args: Array < String > ): Unit
{
val arr1: Array < Int > = arrayOf(1, 2, -4, 5, -4, 3, -5);
val arr2: Array < Int > = arrayOf(1, -3, 4, 0, 6, 2);
val arr3: Array < Int > = arrayOf(3, 4, 1, 2);
// Get the size of arrays
val l: Int = arr1.count();
val m: Int = arr2.count();
val n: Int = arr3.count();
// Test A
/*
arr =  [1, 2, -4, 5, -4, 3, -5]
[1,2,5,3]  Positive number
---------------------
Smallest missing : 4
*/
// Test B
/*
arr =  [1, -3, 4, 0, 6, 2]
[1,4,6,2]  Positive number
---------------------
Smallest missing : 3
*/
// Test C
/*
arr =  [3,4, 1, 2]
[3,4, 1, 2]  Positive number
---------------------
Smallest missing : 5
*/
}``````

#### Output

`````` Array :  1 2 -4 5 -4 3 -5
Result : 4

Array :  1 -3 4 0 6 2
Result : 3

Array :  3 4 1 2
Result : 5``````

## Time Complexity Analysis

The time complexity of this algorithm is O(n), where n is the number of elements in the array. This is because we iterate through the array twice: once to mark the presence of positive integers and once to find the smallest missing positive number.

## Comment

Please share your knowledge to improve code and content standard. Also submit your doubts, and test case. We improve by your feedback. We will try to resolve your query as soon as possible.

Categories
Relative Post