-
Notifications
You must be signed in to change notification settings - Fork 0
/
missingNumAP.c
47 lines (44 loc) · 944 Bytes
/
missingNumAP.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
#include <cs50.h>
#include <stddef.h>
#include <stdio.h>
int find_missing(const int *nums, size_t n);
int main(void)
{
size_t n = get_int("Size: ");
int array[n];
printf("Arithmetic Progression: ");
for (int i = 0; i < n; i++)
{
scanf("%d", &array[i]);
}
printf("Missing term in AP: %d\n", find_missing(array, n));
return 0;
}
int find_missing(const int *nums, size_t n)
{
if (nums[0] == nums[1])
{
return nums[0];
}
int d = nums[1] - nums[0], check = nums[0];
if (d > 0)
{
d = d < (nums[2] - nums[1]) ? d : (nums[2] - nums[1]);
}
else
{
d = d > (nums[2] - nums[1]) ? d : (nums[2] - nums[1]);
}
for (size_t i = 0; i < n; i++)
{
if (nums[i] != check)
{
return check;
}
else
{
check += d;
}
}
return 0;
}