Array problem : find missing value between 1 to 100.
This is the most asked question in coding interviews. The interviewer will ask you to provide a solution to a problem in which you have an array ranged between 1 to 100 and you have to find a missing element in the given array.
The solution to this problem is very simple ..
you have to follow these two steps to get a solution to a given problem.
1.) find some elements between 1 to 100.
use formula : n*(n+1)/2; where n=100;
2.) find some of original array.
use : sum=sum+a[i] ;
program for given problem using c++ :
#include<iostream>
#include<array>
using namespace std;
int findMissingElement(array<int,10>a1 ,int n){
int ActualSum=0;
int ExpectedSum=n*(n-1)/2;
for(int i=0;i<n;i++){
ActualSum=ActualSum+a1.at(i);
}
return(ExpectedSum-ActualSum);
}
int main(){
array<int,10> ar={1,2,3,4,5,7,8,9};
int fx=findMissingElement(ar,10);
cout<<fx;
return 0;
}
Post a Comment: