Find a missing number in an array

Here given code implementation process.

//C Program 
//Find a missing number in an array
#include <stdio.h>

//Find  missing element of given array
void missing_element(int arr[], int size)
{
	if (size <= 0)
	{
		return;
	}
	// Assume that
	// 1) Array are containing all unique element (no have repeat element)
	// 2) Array contains element between [ 1 to n ] or [-1 to -n] elements
	// 3) Only one element is missing in array
	// 4) Array is form of sorted or unsorted
	//Calculate sum of all elements in array
	int sum = ((size + 1) *(size + 2)) / 2;
	if (arr[0] < 0)
	{
		//When given array contains negative elements
		sum = -sum;
	}
	//loop which is iterating array elements 
	for (int i = 0; i < size; ++i)
	{
		//Total sum of array elements are minius by current array element [i].
		sum = sum - arr[i];
	}
	//Display result of missing element
	printf("\n Missing element : %d", sum);
}
int main()
{
	//Define collection of array elements
	int arr1[] = {
		1 , 3 , 4 , 5 , 6
	};
	//Get the size of array
	int size = sizeof(arr1) / sizeof(arr1[0]);
	missing_element(arr1, size);
	int arr2[] = {
		1 , 2 , 3 , 4 , 5 , 6 , 7 , 9
	};
	size = sizeof(arr2) / sizeof(arr2[0]);
	missing_element(arr2, size);
	//Given array element for (-1 to -n elements)
	int arr3[] = {
		-7 , -6 , -4 , -3 , -2 , -1
	};
	size = sizeof(arr3) / sizeof(arr3[0]);
	missing_element(arr3, size);
	//When given array sequence are unsort.
	int arr4[] = {
		1 , 6 , 5 , 4 , 2
	};
	size = sizeof(arr4) / sizeof(arr4[0]);
	missing_element(arr4, size);
	return 0;
}

Output

 Missing element : 2
 Missing element : 8
 Missing element : -5
 Missing element : 3
// Java Program
// Find a missing number in an array
class MyArray
{
	//Find  missing element of given array
	public void missing_element(int[] arr, int size)
	{
		if (size <= 0)
		{
			return;
		}
		// Assume that
		// 1) Array are containing all unique element (no have repeat element)
		// 2) Array contains element between [ 1 to n ] or [-1 to -n] elements
		// 3) Only one element is missing in array
		// 4) Array is form of sorted or unsorted
		//Calculate sum of all elements in array
		int sum = ((size + 1) *(size + 2)) / 2;
		if (arr[0] < 0)
		{
			//When given array contains negative elements
			sum = -sum;
		}
		//loop which is iterating array elements 
		for (int i = 0; i < size; ++i)
		{
			//Total sum of array elements are minius by current array element [i].
			sum = sum - arr[i];
		}
		System.out.print("\n Missing element : " + sum);
	}
	public static void main(String[] args)
	{
		MyArray obj = new MyArray();
		//Define collection of array elements
		int[] arr1 = {
			1 , 3 , 4 , 5 , 6
		};
		//Get the size of array
		int size = arr1.length;
		obj.missing_element(arr1, size);
		int[] arr2 = {
			1 , 2 , 3 , 4 , 5 , 6 , 7 , 9
		};
		size = arr2.length;
		obj.missing_element(arr2, size);
		//Given array element for (-1 to -n elements)
		int[] arr3 = {
			-7 , -6 , -4 , -3 , -2 , -1
		};
		size = arr3.length;
		obj.missing_element(arr3, size);
		//When given array sequence are unsort.
		int[] arr4 = {
			1 , 6 , 5 , 4 , 2
		};
		size = arr4.length;
		obj.missing_element(arr4, size);
	}
}

Output

 Missing element : 2
 Missing element : 8
 Missing element : -5
 Missing element : 3
//Include header file
#include <iostream>

using namespace std;
// C++ Program
// Find a missing number in an array
class MyArray
{
	public:
		//Find  missing element of given array
		void missing_element(int arr[], int size)
		{
			if (size <= 0)
			{
				return;
			}
			// Assume that
			// 1) Array are containing all unique element (no have repeat element)
			// 2) Array contains element between [ 1 to n ] or [-1 to -n] elements
			// 3) Only one element is missing in array
			// 4) Array is form of sorted or unsorted
			//Calculate sum of all elements in array
			int sum = ((size + 1) * (size + 2)) / 2;
			if (arr[0] < 0)
			{
				//When given array contains negative elements
				sum = -sum;
			}
			//loop which is iterating array elements 
			for (int i = 0; i < size; ++i)
			{
				//Total sum of array elements are minius by current array element [i].
				sum = sum - arr[i];
			}
			cout << "\n Missing element : " << sum;
		}
};
int main()
{
	MyArray obj = MyArray();
	int arr1[] = {
		1 , 3 , 4 , 5 , 6
	};
	//Get the size of array
	int size = sizeof(arr1) / sizeof(arr1[0]);
	obj.missing_element(arr1, size);
	int arr2[] = {
		1 , 2 , 3 , 4 , 5 , 6 , 7 , 9
	};
	size = sizeof(arr2) / sizeof(arr2[0]);
	obj.missing_element(arr2, size);
	int arr3[] = {
		-7 , -6 , -4 , -3 , -2 , -1
	};
	size = sizeof(arr3) / sizeof(arr3[0]);
	obj.missing_element(arr3, size);
	int arr4[] = {
		1 , 6 , 5 , 4 , 2
	};
	size = sizeof(arr4) / sizeof(arr4[0]);
	obj.missing_element(arr4, size);
	return 0;
}

Output

 Missing element : 2
 Missing element : 8
 Missing element : -5
 Missing element : 3
//Include namespace system
using System;
// C# Program
// Find a missing number in an array
class MyArray
{
	//Find  missing element of given array
	public void missing_element(int[] arr, int size)
	{
		if (size <= 0)
		{
			return;
		}
		// Assume that
		// 1) Array are containing all unique element (no have repeat element)
		// 2) Array contains element between [ 1 to n ] or [-1 to -n] elements
		// 3) Only one element is missing in array
		// 4) Array is form of sorted or unsorted
		//Calculate sum of all elements in array
		int sum = ((size + 1) * (size + 2)) / 2;
		if (arr[0] < 0)
		{
			//When given array contains negative elements
			sum = -sum;
		}
		//loop which is iterating array elements 
		for (int i = 0; i < size; ++i)
		{
			//Total sum of array elements are minius by current array element [i].
			sum = sum - arr[i];
		}
		Console.Write("\n Missing element : " + sum);
	}
	public static void Main(String[] args)
	{
		MyArray obj = new MyArray();
		int[] arr1 = {
			1 , 3 , 4 , 5 , 6
		};
		//Get the size of array
		int size = arr1.Length;
		obj.missing_element(arr1, size);
		int[] arr2 = {
			1 , 2 , 3 , 4 , 5 , 6 , 7 , 9
		};
		size = arr2.Length;
		obj.missing_element(arr2, size);
		int[] arr3 = {
			-7 , -6 , -4 , -3 , -2 , -1
		};
		size = arr3.Length;
		obj.missing_element(arr3, size);
		int[] arr4 = {
			1 , 6 , 5 , 4 , 2
		};
		size = arr4.Length;
		obj.missing_element(arr4, size);
	}
}

Output

 Missing element : 2
 Missing element : 8
 Missing element : -5
 Missing element : 3
<?php
// Php Program
// Find a missing number in an array
class MyArray
{
	//Find  missing element of given array
	public	function missing_element( & $arr, $size)
	{
		if ($size <= 0)
		{
			return;
		}
		// Assume that
		// 1) Array are containing all unique element (no have repeat element)
		// 2) Array contains element between [ 1 to n ] or [-1 to -n] elements
		// 3) Only one element is missing in array
		// 4) Array is form of sorted or unsorted
		//Calculate sum of all elements in array
		$sum = intval((($size + 1) * ($size + 2)) / 2);
		if ($arr[0] < 0)
		{
			//When given array contains negative elements
			$sum = -$sum;
		}
		//loop which is iterating array elements 
		for ($i = 0; $i < $size; ++$i)
		{
			//Total sum of array elements are minius by current array element [i].
			$sum = $sum - $arr[$i];
		}
		echo "\n Missing element : ". $sum;
	}
}

function main()
{
	$obj = new MyArray();
	//Define collection of array elements
	$arr1 = array(1, 3, 4, 5, 6);
	//Get the size of array
	$size = count($arr1);
	$obj->missing_element($arr1, $size);
	$arr2 = array(1, 2, 3, 4, 5, 6, 7, 9);
	$size = count($arr2);
	$obj->missing_element($arr2, $size);
	//Given array element for (-1 to -n elements)
	$arr3 = array(-7, -6, -4, -3, -2, -1);
	$size = count($arr3);
	$obj->missing_element($arr3, $size);
	//When given array sequence are unsort.
	$arr4 = array(1, 6, 5, 4, 2);
	$size = count($arr4);
	$obj->missing_element($arr4, $size);
}
main();

Output

 Missing element : 2
 Missing element : 8
 Missing element : -5
 Missing element : 3
// Node Js Program
// Find a missing number in an array
class MyArray
{
	//Find  missing element of given array
	missing_element(arr, size)
	{
		if (size <= 0)
		{
			return;
		}
		// Assume that
		// 1) Array are containing all unique element (no have repeat element)
		// 2) Array contains element between [ 1 to n ] or [-1 to -n] elements
		// 3) Only one element is missing in array
		// 4) Array is form of sorted or unsorted
		//Calculate sum of all elements in array
		var sum = parseInt(((size + 1) * (size + 2)) / 2);
		if (arr[0] < 0)
		{
			//When given array contains negative elements
			sum = -sum;
		}
		//loop which is iterating array elements 
		for (var i = 0; i < size; ++i)
		{
			//Total sum of array elements are minius by current array element [i].
			sum = sum - arr[i];
		}
		process.stdout.write("\n Missing element : " + sum);
	}
}

function main()
{
	var obj = new MyArray();
	//Define collection of array elements
	var arr1 = [1, 3, 4, 5, 6];
	//Get the size of array
	var size = arr1.length;
	obj.missing_element(arr1, size);
	var arr2 = [1, 2, 3, 4, 5, 6, 7, 9];
	size = arr2.length;
	obj.missing_element(arr2, size);
	//Given array element for (-1 to -n elements)
	var arr3 = [-7, -6, -4, -3, -2, -1];
	size = arr3.length;
	obj.missing_element(arr3, size);
	//When given array sequence are unsort.
	var arr4 = [1, 6, 5, 4, 2];
	size = arr4.length;
	obj.missing_element(arr4, size);
}
main();

Output

 Missing element : 2
 Missing element : 8
 Missing element : -5
 Missing element : 3
#  Python 3 Program
#  Find a missing number in an array
class MyArray :
	# Find  missing element of given array
	def missing_element(self, arr, size) :
		if (size <= 0) :
			return
		
		#  Assume that
		#  1) Array are containing all unique element (no have repeat element)
		#  2) Array contains element between [ 1 to n ] or [-1 to -n] elements
		#  3) Only one element is missing in array
		#  4) Array is form of sorted or unsorted
		# Calculate sum of all elements in array
		sum = int(((size + 1) * (size + 2)) / 2)
		if (arr[0] < 0) :
			# When given array contains negative elements
			sum = -sum
		
		i = 0
		# loop which is iterating array elements 
		while (i < size) :
			# Total sum of array elements are minius by current array element [i].
			sum = sum - arr[i]
			i += 1
		
		print("\n Missing element : ", sum, end = "")
	

def main() :
	obj = MyArray()
	# Define collection of array elements
	arr1 = [1, 3, 4, 5, 6]
	# Get the size of array
	size = len(arr1)
	obj.missing_element(arr1, size)
	arr2 = [1, 2, 3, 4, 5, 6, 7, 9]
	size = len(arr2)
	obj.missing_element(arr2, size)
	# Given array element for (-1 to -n elements)
	arr3 = [-7, -6, -4, -3, -2, -1]
	size = len(arr3)
	obj.missing_element(arr3, size)
	# When given array sequence are unsort.
	arr4 = [1, 6, 5, 4, 2]
	size = len(arr4)
	obj.missing_element(arr4, size)

if __name__ == "__main__": main()

Output

 Missing element :  2
 Missing element :  8
 Missing element :  -5
 Missing element :  3
#  Ruby Program
#  Find a missing number in an array
class MyArray

	# Find  missing element of given array
	def missing_element(arr, size)
	
		if (size <= 0)
		
			return
		end
		#  Assume that
		#  1) Array are containing all unique element (no have repeat element)
		#  2) Array contains element between [ 1 to n ] or [-1 to -n] elements
		#  3) Only one element is missing in array
		#  4) Array is form of sorted or unsorted
		# Calculate sum of all elements in array
		sum = ((size + 1) * (size + 2)) / 2
		if (arr[0] < 0)
		
			# When given array contains negative elements
			sum = -sum
		end
		i = 0
		# loop which is iterating array elements 
		while (i < size)
		
			# Total sum of array elements are minius by current array element [i].
			sum = sum - arr[i]
			i += 1
		end
		print("\n Missing element : ", sum)
	end
end
def main()

	obj = MyArray.new()
	# Define collection of array elements
	arr1 = [1, 3, 4, 5, 6]
	# Get the size of array
	size = arr1.length
	obj.missing_element(arr1, size)
	arr2 = [1, 2, 3, 4, 5, 6, 7, 9]
	size = arr2.length
	obj.missing_element(arr2, size)
	# Given array element for (-1 to -n elements)
	arr3 = [-7, -6, -4, -3, -2, -1]
	size = arr3.length
	obj.missing_element(arr3, size)
	# When given array sequence are unsort.
	arr4 = [1, 6, 5, 4, 2]
	size = arr4.length
	obj.missing_element(arr4, size)
end
main()

Output

 Missing element : 2
 Missing element : 8
 Missing element : -5
 Missing element : 3
// Scala Program
// Find a missing number in an array
class MyArray
{
	//Find  missing element of given array
	def missing_element(arr: Array[Int], size: Int): Unit = {
		if (size <= 0)
		{
			return;
		}
		// Assume that
		// 1) Array are containing all unique element (no have repeat element)
		// 2) Array contains element between [ 1 to n ] or [-1 to -n] elements
		// 3) Only one element is missing in array
		// 4) Array is form of sorted or unsorted
		//Calculate sum of all elements in array
		var sum: Int = (((size + 1) * (size + 2)) / 2).toInt;
		if (arr(0) < 0)
		{
			//When given array contains negative elements
			sum = -sum;
		}
		var i: Int = 0;
		//loop which is iterating array elements 
		while (i < size)
		{
			//Total sum of array elements are minius by current array element [i].
			sum = sum - arr(i);
			i += 1;
		}
		print("\n Missing element : " + sum);
	}
}
object Main
{
	def main(args: Array[String]): Unit = {
		var obj: MyArray = new MyArray();
		//Define collection of array elements
		var arr1: Array[Int] = Array(1, 3, 4, 5, 6);
		//Get the size of array
		var size: Int = arr1.length;
		obj.missing_element(arr1, size);
		var arr2: Array[Int] = Array(1, 2, 3, 4, 5, 6, 7, 9);
		size = arr2.length;
		obj.missing_element(arr2, size);
		//Given array element for (-1 to -n elements)
		var arr3: Array[Int] = Array(-7, -6, -4, -3, -2, -1);
		size = arr3.length;
		obj.missing_element(arr3, size);
		//When given array sequence are unsort.
		var arr4: Array[Int] = Array(1, 6, 5, 4, 2);
		size = arr4.length;
		obj.missing_element(arr4, size);
	}
}

Output

 Missing element : 2
 Missing element : 8
 Missing element : -5
 Missing element : 3
// Swift Program
// Find a missing number in an array
class MyArray
{
	//Find  missing element of given array
	func missing_element(_ arr: [Int], _ size: Int)
	{
		if (size <= 0)
		{
			return;
		}
		// Assume that
		// 1) Array are containing all unique element (no have repeat element)
		// 2) Array contains element between [ 1 to n ] or [-1 to -n] elements
		// 3) Only one element is missing in array
		// 4) Array is form of sorted or unsorted
		//Calculate sum of all elements in array
		var sum: Int = ((size + 1) * (size + 2)) / 2;
		if (arr[0] < 0)
		{
			//When given array contains negative elements
			sum = -sum;
		}
		var i: Int = 0;
		//loop which is iterating array elements 
		while (i < size)
		{
			//Total sum of array elements are minius by current array element [i].
			sum = sum - arr[i];
			i += 1;
		}
		print("\n Missing element : ", sum, terminator: "");
	}
}
func main()
{
	let obj: MyArray = MyArray();
	//Define collection of array elements
	let arr1: [Int] = [1, 3, 4, 5, 6];
	//Get the size of array
	var size: Int = arr1.count;
	obj.missing_element(arr1, size);
	let arr2: [Int] = [1, 2, 3, 4, 5, 6, 7, 9];
	size = arr2.count;
	obj.missing_element(arr2, size);
	//Given array element for (-1 to -n elements)
	let arr3: [Int] = [-7, -6, -4, -3, -2, -1];
	size = arr3.count;
	obj.missing_element(arr3, size);
	//When given array sequence are unsort.
	let arr4: [Int] = [1, 6, 5, 4, 2];
	size = arr4.count;
	obj.missing_element(arr4, size);
}
main();

Output

 Missing element :  2
 Missing element :  8
 Missing element :  -5
 Missing element :  3


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