Created
June 20, 2014 09:08
-
-
Save sbilello/fe36c51afd06717c47ae to your computer and use it in GitHub Desktop.
Arithmetic progression
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
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