Find the Missing Number in Array

Get an array of Integers arr[] with size n and find the first missing natural number in the array arr[].The array does not contains duplicates.

Example

Input 1:

5

4 5 3 1 6

Output 1:

2

Input 1:

6

4 5 3 2 6 7

Output 1:

1

Try your Solution

Strongly recommended to Solve it on your own, Don't directly go to the solution given below.

public class Hello { public static void main(String args[]) { //Write your code here } }

Program or Solution

				
				
					

import java.util.*;

class Missing_Number

{

public static void main(String args[])

    {


//Variable and  Object Declarations

        Scanner input = new Scanner(System.in);

int n;

//Getting size of Array & Declare Array arr[]

n = input.nextInt();

int arr[] = new int[n];


//Get n values to array arr[]

for(int i = 0; i<n; i++)

{

arr[i] = input.nextInt();

}


Arrays.sort(arr);

for(int i = 0; i<n; i++)

{

if(i+1!=arr[i])

{

System.out.print(i+1);

break;

}

}

      }

}

Program Explanation

Comments