Saturday, June 19, 2021
Home coding Write a program to find missing number in a given integer array...

Write a program to find missing number in a given integer array of 1 to 100?

Given a number we have to find the missing number in a given integer array which have value between 1 to 100.

Example:

input:1 2 3 4 5 6 8 9
output:7

Logic:

step 1- calculate the sum of array.
step 2-calculate the sum of first n natural number.
step 3-subtract both of them will give missing number.

Code-

#include<bits/stdc++.h>
using namespace std;
int main()
{
   int arr[]={1,2,3,5,6};
   int n=sizeof(arr)/sizeof(arr[0]);
   int m=n+1;
     int sum=(m*(m+1))/2;
     int sum1=0;
     for(int i=0;i<n;i++)
{
    sum1+=arr[i];
}
cout<<sum-sum1<<endl;
   
}

Output:

4

Time complexity:0(N)

Space Complexity:O(1)

LEAVE A REPLY

Please enter your comment!
Please enter your name here

- Advertisment -

Most Popular

Recent Comments