Question-
You have to find the missing number in the given array.
Explanation-
1) First input asks the user to enter the size of array.
2) Second input asks the user to enter the elements in sorted manner (if is not entered in sorted manner the you will have to use bubble sort for the sorting of elements)
3) Take out the sum of the elements present in array and store it in a variable.
4) Now take out the sum of (n+1) elements. So the formula for sum of first "n" natural number becomes (n+1)(n+2)/2.
5) Finally, subtract the both of the sum stored in different variable to get the final output.
Program-
Array size - 5
Elements- 1,2,3,4,6
Output-
Missing Number - 5
You have to find the missing number in the given array.
Explanation-
1) First input asks the user to enter the size of array.
2) Second input asks the user to enter the elements in sorted manner (if is not entered in sorted manner the you will have to use bubble sort for the sorting of elements)
3) Take out the sum of the elements present in array and store it in a variable.
4) Now take out the sum of (n+1) elements. So the formula for sum of first "n" natural number becomes (n+1)(n+2)/2.
5) Finally, subtract the both of the sum stored in different variable to get the final output.
Program-
#include<iostream> using namespace std; int main() { int n; cout<<"Enter size of array"<<endl; cin>>n; int a[n]; cout<<"Enter elements of array in sorted manner"<<endl; for(i=0;i<n;i++) cin>>a[i]; int sum=(n+1)*(n+2)/2; int arsum=0; for( int i=0;i<n;i++) arsum=arsum+a[i]; int misno=sum-arsum; cout<<"Element missing in the array is : "<<misno; }Input-
Array size - 5
Elements- 1,2,3,4,6
Output-
Missing Number - 5
Thankuh..it helped me alot...keep it up
ReplyDeleteNice 1
ReplyDelete