Find the Missing Number in Array 3

Get an array of Integers arr[] with size n and find the first missing number in the array arr[]. Assume the array starts with smallest number and ends with greatest number. So find the missing number between smallest to greatest. Return 0 if no missing number found. Array may contains duplicates numbers.

Example

Input 1:

5

12 10 9 7 11

Output 1:

8

Input 1:

6

4 5 7 8 6 9

Output 1:

0

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_Number2

{

public static void main(String args[])

    {


//Variable and  Object Declarations

        Scanner input = new Scanner(System.in);

int n,bool=0;

//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 = 1; i<n; i++)

{

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

{

System.out.print(arr[i-1]+1);

bool = 1;

break;

}

}

if(bool==0)

{

System.out.print(0);

}

      }

}

Program Explanation

Comments