Last active
January 3, 2016 00:09
-
-
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
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#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