Find the Missing Number
2. You're given all numbers between 1, 2, 3, 4 … n except one. Your task is to find the missing number.
Input:
First input line contains an integer n. The second line contains (n−1) numbers. Every number is distinct and between 1 and n (inclusive).
Output:
Print the missing number.
Sample Input | Sample Output |
---|---|
5 2 4 3 1 |
5 |
10 2 10 7 9 3 6 4 8 1 |
5 |
Sample Output
10 2 10 7 9 3 6 4 8 1 Missing Number is: 5 Process returned 0 (0x0) execution time : 20.268 s Press any key to continue.
Source Code
#include<stdio.h>
int main()
{
int i, j, sum, arrSum = 0, n;
scanf("%d", &n);
int numbers[n];
for(i = 0; i < n-1; i++)
{
scanf("%d", &numbers[i]);
}
sum = (n * (n+1))/2;
for(i = 0; i < n-1; i++)
{
arrSum = arrSum + numbers[i];
}
printf("Missing Number is: %d\n\n", sum - arrSum);
return 0;
}
Sample Output
5 2 4 3 1 Missing Number is: 5 Process returned 0 (0x0) execution time : 16.864 s Press any key to continue.
No comments:
Post a Comment