Skip to content

Instantly share code, notes, and snippets.

@pveyes
Last active January 3, 2016 00:09
Show Gist options
  • Save pveyes/8380521 to your computer and use it in GitHub Desktop.
Save pveyes/8380521 to your computer and use it in GitHub Desktop.
My solution to Facebook Programming Challenge sample problem: Missing number in arithmetic sequence. Source: http://retina.pvey.es/solving-facebook-programming-challenge-sample-problem
#include <stdio.h>
int main() {
int input, val;
int init, delta = 0, newdelta;
int i;
scanf("%d\n", &input);
scanf("%d ", &init);
for (i = 1; i < input; i++) {
scanf("%d ", &val);
newdelta = val - init;
if (newdelta == 2 * delta) {
// skip on current loop
printf("%d\n", val - delta);
break;
}
else if (delta == 2 * newdelta) {
// skip on previous loop
printf("%d\n", init - newdelta);
break;
}
delta = newdelta;
init = val;
}
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment