Positive Con Sequences
find two consecutive known numbers and calculate their difference and quotient. trying both, check whether the other known number is as it should be, and if so, return the missing number
import java.util.*;
public class a {
Scanner in=new Scanner(System.in);
public static void main(String[] args) {
new a().go();
}
private void go() {
OUT:
while(true){
int[] d=new int[4];
for(int i=0;i<4;i++)d[i]=in.nextInt();
if(d[0]==-1&&d[1]==-1)break;
int index=0;
for(int i=0;i<4;i++)if(d[i]==-1)index=i;
int first=0;
if(index<2)first=2;
int diff=d[first+1]-d[first];
int ans=-1;
boolean works=true;
for(int i=0;i<4;i++){
int val=d[first]+diff*(i-first);
if(d[i]==-1)ans=val;
else if(val!=d[i])works=false;
}
if(works&&ans>0&&ans<1000001){
System.out.println(ans);
continue OUT;
}
diff=d[first+1]/d[first];
if(d[first]*diff!=d[first+1]||(index==0&&d[1]%diff!=0)){
System.out.println(-1);
continue OUT;
}
ans=-1;
works=true;
for(int i=0;i<4;i++){
int val=(int) (d[first]*Math.pow(diff, i-first));
if(d[i]==-1)ans=val;
else if(val!=d[i])works=false;
}
if(works&&ans<1000001)System.out.println(ans);
else System.out.println(-1);
}
}
}