-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathmissing_integer.c
More file actions
24 lines (21 loc) · 790 Bytes
/
Copy pathmissing_integer.c
File metadata and controls
24 lines (21 loc) · 790 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
// You are given a sequence of n-1 distinct positive integers, all of which are less than or equal to a integer ‘n’. You have to find the integer that is missing from the range [1,2, . . . n]. Solve the question without using arrays.
// Input Format:
// One line containing the integer ‘n’ where 2<=n<=10,000
// First line is followed by a sequence of ‘n-1’ distinct positive integers. Note that the sequence may not be in any particular order.
// Output Format:
// One line containing the missing number
#include <stdio.h>
int main()
{
int n;
scanf("%d", &n);
int actual_sum = n * (n + 1) / 2, given_sum = 0;
while (n != 1)
{
int k;
scanf("%d", &k);
given_sum += k;
n--;
}
printf("%d", actual_sum - given_sum);
}