Skip to content

Instantly share code, notes, and snippets.

@philipnilsson
Last active January 2, 2016 23:19
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save philipnilsson/8375266 to your computer and use it in GitHub Desktop.
Save philipnilsson/8375266 to your computer and use it in GitHub Desktop.
Missing integer in arithmetic sequence
#include <stdio.h>
int main(void) {
int n, e, i = 0;
scanf("%d\n", &n);
scanf("%d", &e);
int sum = e, first = e, last = e, len = 1;
int delta;
while (++i < n) {
scanf(" %d", &e);
sum += e;
len++;
int tmp = e - last;
last = e;
if (i > 1 && tmp != delta)
break;
delta = tmp;
}
int seqSum = (first + last) * (len + 1) / 2;
printf("%d\n", seqSum - sum);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment