Find missing numbers in an array

Problem statement:- Program to Find missing numbers in an array.

Example:-

              Input : Given

                                 size=5

                                 arr[]=[1,2,4,5,6] 

             Output: Maximum difference between two Element is 12 //(13-1=12)            

Data requirement:-


   Input Data:- size, arr

  Output Data:-miss

  Additional Data:- i,sum,size2

Program in C

Here is the source code of the C Program to Find missing numbers in an array.

Code:

#include<stdio.h>
#include<math.h>
main()
{
    printf("Enter the size of the array:");
    int size;
    scanf("%d",&size);
    int arr[size];
    int i;
    printf("Enter the Element of the array:\n");
    for(i=0;i<size;i++)
    {
        scanf("%d",&arr[i]);
    }
    int sum=0;
    for(i=0;i<size;i++)
    {
        sum+=arr[i];
    }
    int size2=size+1;
    int miss=(size2*(size2+1))/2 - sum;
    printf("Missing Number is: %d",abs(miss));
}

Input/Output:
Enter the size of the array:5
Enter the Element of the array:
1 2 4 5 6
Missing Number is: 3

Program in C++

Here is the source code of the C++ Program to Find missing numbers in an array.

Code:

#include<iostream>
#include<cmath>
using namespace std;
int main()
{
    cout<<"Enter the size of the array:";
    int size;
    cin>>size;
    int arr[size];
    int i,j;
    cout<<"Enter the Element of the array:\n";
    for(i=0;i<size;i++)
    {
        cin>>arr[i];
    }
    int sum=0;
    for(i=0;i<size;i++)
    {
        sum+=arr[i];
    }
    int size2=size+1;
    int miss=(size2*(size2+1))/2 - sum;
    cout<<"Missing Number is: "<<abs(miss);
}

Input/Output:
Enter the size of the array:4
Enter the Element of the array:
3 4 6 7
Missing Number is: 5

Program in Java

Here is the source code of the Java Program to Find missing numbers in an array.

Code:

import java.util.Scanner;
public class MissingNumber {

public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
  System.out.println("Enter the size of the array:");
     int size,i;
     size=sc.nextInt();
     int arr[ ]=new int[size];
     System.out.println("Enter the Element of the array:");
      for(i=0;i<size;i++)
     {
         arr[i]=sc.nextInt();
     }
     
      int sum=0;
      for(i=0;i<size;i++)
      {
          sum+=arr[i];
      }
      int size2=size+1;
      int miss=(size2*(size2+1))/2;
      System.out.println("Missing Number is: "+Math.abs(miss-sum));
   sc.close();
}
}

Input/Output:
Enter the size of the array:
5
Enter the Element of the array:
1 5 4 3 6
Missing Number is: 2

Program in Python

Here is the source code of the Python Program to Find missing numbers in an array.

Code:

arr=[]
size = int(input("Enter the size of the array: "))
print("Enter the Element of the array:")
for i in range(0,size):
    num = int(input())
    arr.append(num)
sum=0
for i in range(0,size):
    sum += arr[i]
size2=size+1
miss=int((size2*(size2+1))/2)

print("Missing Number is: ",abs(miss-sum))

Input/Output:
Enter the size of the array: 4
Enter the Element of the array:
1
2
4
5
Missing Number is:  3

Post a Comment

0 Comments