Smallest positive missing number in an array

Here given code implementation process.

/*
    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)
	{
		Missing task = new Missing();
		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
		*/
		task.firstPositiveSmallest(arr1, l);
		// Test B
		/*
		   arr =  [1, -3, 4, 0, 6, 2]

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

		   [3,4, 1, 2]  Positive number
		   ---------------------
		   Smallest missing : 5
		*/
		task.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
// 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()
{
	Missing *task = new Missing();
	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
	*/
	task->firstPositiveSmallest(arr1, l);
	// Test B
	/*
	   arr =  [1, -3, 4, 0, 6, 2]
	   [1,4,6,2]  Positive number
	   ---------------------
	   Smallest missing : 3
	*/
	task->firstPositiveSmallest(arr2, m);
	// Test C
	/*
	   arr =  [3,4, 1, 2]
	   [3,4, 1, 2]  Positive number
	   ---------------------
	   Smallest missing : 5
	*/
	task->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
// 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)
	{
		Missing task = new Missing();
		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
		*/
		task.firstPositiveSmallest(arr1, l);
		// Test B
		/*
		   arr =  [1, -3, 4, 0, 6, 2]
		   [1,4,6,2]  Positive number
		   ---------------------
		   Smallest missing : 3
		*/
		task.firstPositiveSmallest(arr2, m);
		// Test C
		/*
		   arr =  [3,4, 1, 2]
		   [3,4, 1, 2]  Positive number
		   ---------------------
		   Smallest missing : 5
		*/
		task.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
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()
{
	$task = new Missing();
	$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
	*/
	$task->firstPositiveSmallest($arr1, $l);
	// Test B
	/*
	   arr =  [1, -3, 4, 0, 6, 2]
	   [1,4,6,2]  Positive number
	   ---------------------
	   Smallest missing : 3
	*/
	$task->firstPositiveSmallest($arr2, $m);
	// Test C
	/*
	   arr =  [3,4, 1, 2]
	   [3,4, 1, 2]  Positive number
	   ---------------------
	   Smallest missing : 5
	*/
	$task->firstPositiveSmallest($arr3, $n);
}
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 task = new Missing();
	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
	*/
	task.firstPositiveSmallest(arr1, l);
	// Test B
	/*
	   arr =  [1, -3, 4, 0, 6, 2]
	   [1,4,6,2]  Positive number
	   ---------------------
	   Smallest missing : 3
	*/
	task.firstPositiveSmallest(arr2, m);
	// Test C
	/*
	   arr =  [3,4, 1, 2]
	   [3,4, 1, 2]  Positive number
	   ---------------------
	   Smallest missing : 5
	*/
	task.firstPositiveSmallest(arr3, n);
}
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() :
	task = Missing()
	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
	task.firstPositiveSmallest(arr1, l)
	#  Test B
	#   arr =  [1, -3, 4, 0, 6, 2]
	#   [1,4,6,2]  Positive number
	#   ---------------------
	#   Smallest missing : 3
	task.firstPositiveSmallest(arr2, m)
	#  Test C
	#   arr =  [3,4, 1, 2]
	#   [3,4, 1, 2]  Positive number
	#   ---------------------
	#   Smallest missing : 5
	task.firstPositiveSmallest(arr3, n)

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() 
	task = Missing.new()
	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
	task.firstPositiveSmallest(arr1, l)
	#  Test B
	#   arr =  [1, -3, 4, 0, 6, 2]
	#   [1,4,6,2]  Positive number
	#   ---------------------
	#   Smallest missing : 3
	task.firstPositiveSmallest(arr2, m)
	#  Test C
	#   arr =  [3,4, 1, 2]
	#   [3,4, 1, 2]  Positive number
	#   ---------------------
	#   Smallest missing : 5
	task.firstPositiveSmallest(arr3, n)
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
		*/
		task.firstPositiveSmallest(arr1, l);
		// Test B
		/*
		   arr =  [1, -3, 4, 0, 6, 2]
		   [1,4,6,2]  Positive number
		   ---------------------
		   Smallest missing : 3
		*/
		task.firstPositiveSmallest(arr2, m);
		// Test C
		/*
		   arr =  [3,4, 1, 2]
		   [3,4, 1, 2]  Positive number
		   ---------------------
		   Smallest missing : 5
		*/
		task.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
/*
    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 task: Missing = Missing();
	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
	*/
	task.firstPositiveSmallest(arr1, l);
	// Test B
	/*
	   arr =  [1, -3, 4, 0, 6, 2]
	   [1,4,6,2]  Positive number
	   ---------------------
	   Smallest missing : 3
	*/
	task.firstPositiveSmallest(arr2, m);
	// Test C
	/*
	   arr =  [3,4, 1, 2]
	   [3,4, 1, 2]  Positive number
	   ---------------------
	   Smallest missing : 5
	*/
	task.firstPositiveSmallest(arr3, n);
}
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 task: Missing = Missing();
	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
	*/
	task.firstPositiveSmallest(arr1, l);
	// Test B
	/*
	   arr =  [1, -3, 4, 0, 6, 2]
	   [1,4,6,2]  Positive number
	   ---------------------
	   Smallest missing : 3
	*/
	task.firstPositiveSmallest(arr2, m);
	// Test C
	/*
	   arr =  [3,4, 1, 2]
	   [3,4, 1, 2]  Positive number
	   ---------------------
	   Smallest missing : 5
	*/
	task.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


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.

New Comment







© 2021, kalkicode.com, All rights reserved