Program to Find the smallest missing number

Description

This program is for finding smallest number that is missing from the array.
Given a sorted array of n distinct integers where each integer is in the range
from 0 to m and m > n.

C/C++

/* C program to Find the smallest missing number */
//Save it as SmallestMissingNumber.c

#include<stdio.h>
int main(){

    int i, j, n, m;

    printf("Enter the size of array : ");
    scanf("%d",&n);

    int arr[n];

    printf("Enter the elements of sorted array : ");
    for(i=0;i<n;i++) {
        scanf("%d",&arr[i]);
    }

    printf("Enter range of array : ");
    scanf("%d",&m);

    printf("Entered elements are : ");
    for(i=0;i<n;i++) {
        printf("%d ",arr[i]);
    }

    printf("\nThe Range of array : %d",m);

    int min = 0;
    int flag = 0, count;

    for(i=0;i<=m;i++) {
        count=0;
        for(j=0;j<n;j++) {
            if(i == arr[j]) {
                break;
            }
            count++;
        }
        if(count == n) {
            min = i;
            break;
        }
    }
    printf("\nSmallest missing number : %d",min);

    return 0;
}
Input:
Enter the size of array : 
9
Enter the elements of array : 
0
1
2
3
4
5
6
7
10
Enter range of array : 
11

Output:
Entered elements are : 
0 1 2 3 4 5 6 7 10 
The Range of array : 11
Smallest missing number : 8

Java

/* Java program to Find the smallest missing number */
//Save it as SmallestMissingNumber.java

import java.io.*;
import java.util.Scanner;

public class SmallestMissingNumber {

    public static void main(String[] args) {

        Scanner scanner = new Scanner(System.in);
        
        System.out.println("Enter the size of array : ");
        int n = scanner.nextInt();
        
        int arr[] = new int[n];
        
        System.out.println("Enter the elements of sorted array : ");
        for(int i=0;i<n;i++) {
            arr[i] = scanner.nextInt();
        }
        
        System.out.println("Enter range of array : ");
        int m = scanner.nextInt();
        
        System.out.println("Entered elements are : ");
        for(int i=0;i<n;i++) {
            System.out.print(arr[i]+" ");
        }
        
        System.out.println("\nThe Range of array : "+m);
        
        int min = 0;
        int flag = 0, count;
        
        for(int i=0;i<=m;i++) {
            count=0;
            for(int j=0;j<n;j++) {
                if(i == arr[j]) {
                    break;
                }
                count++;
            }
            if(count == n) {
                min = i;
                break;
            }
        }
        System.out.println("Smallest missing number : "+min);
    }
}
Input:
Enter the size of array : 
9
Enter the elements of array : 
0
1
2
3
4
5
6
7
10
Enter range of array : 
11

Output:
Entered elements are : 
0 1 2 3 4 5 6 7 10 
The Range of array : 11
Smallest missing number : 8

Related Programs

1) Find Smallest element and it’s position of an Array
2) Program for finding Second Largest Element Of Array
3) Program to merge two sorted arrays
4) Program to Find missing odd number in first n odd number
5) Program to Find K largest elements from array
6) Program to Print How Many Numbers Smaller than Current Number
7) Remove Duplicate Elements From Array
8) Program to Check Array is Perfect or Not
9) Program to Find Mode of a Array
10) Program to Rearrange an array such that arr[i]=i
Share Me

Leave a Reply