Array problem : find missing value between 1 to 100.
This is the mostly asked question in coding interviews. Interviewer will ask you to provide solution of a problem in which you have an array ranged between 1 to 100 and you have to find missing element in given array.
Solution of this problem is very simple ..
you have to follow these two steps to get solution of given problem..
1.) find some of elements between 1 to 100..
2.) find some of original array.
use : sum=sum+a[i] ;
program for given problem using c++ :
Solution of this problem is very simple ..
you have to follow these two steps to get solution of given problem..
1.) find some of 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;
#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;
}
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: