Skip to content

Instantly share code, notes, and snippets.

@sbilello
Created June 20, 2014 09:08
Show Gist options
  • Save sbilello/fe36c51afd06717c47ae to your computer and use it in GitHub Desktop.
Save sbilello/fe36c51afd06717c47ae to your computer and use it in GitHub Desktop.
Arithmetic progression
import java.io.*;
import java.lang.Math;
public class Solution {
public static void main(String args[] ) throws Exception {
String line = null;
try {
BufferedReader is = new BufferedReader(new InputStreamReader(System.in));
line = is.readLine();
line = is.readLine();
String[] splitted = line.split(" ");
int[] numbers = new int[splitted.length];
int k=0;
for (String s : splitted){
numbers[k] = Integer.valueOf(s);
k++;
}
int first = numbers[1]-numbers[0];
int last = numbers[numbers.length-1]-numbers[numbers.length-2];
int offset = Math.min(first,last);
int found = 0;
for (int i=0; i<numbers.length-1;i++){
if (numbers[i]+offset==numbers[i+1])
continue;
else{
found=numbers[i]+offset;
break;
}
}
System.out.println(found);
} catch (NumberFormatException ex) {
System.err.println("Not a valid number: " + line);
} catch (IOException e) {
System.err.println("Unexpected IO ERROR: " + e);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment