Skip to content

Instantly share code, notes, and snippets.

@vchatela
Created February 2, 2016 21:40
Show Gist options
  • Save vchatela/7dddb6e8b0ffa0ceb04b to your computer and use it in GitHub Desktop.
Save vchatela/7dddb6e8b0ffa0ceb04b to your computer and use it in GitHub Desktop.
Codingame : Temperatures challenge Solution
import java.util.*;
import java.io.*;
import java.math.*;
/**
* Auto-generated code below aims at helping you parse
* the standard input according to the problem statement.
**/
class Solution {
public static void main(String args[]) {
Scanner in = new Scanner(System.in);
int n = in.nextInt(); // the number of temperatures to analyse
in.nextLine();
String temps = in.nextLine(); // the n temperatures expressed as integers ranging from -273 to 5526
int closest = 5527;
System.err.println(temps);
String[] array = temps.split(" ");
int nbr;
for(int i = 0; i<n;i++){
nbr = Integer.parseInt(array[i]);
if(Math.abs(nbr) <= Math.abs(closest)){
if((closest < 0 && nbr > 0) || Math.abs(nbr) < Math.abs(closest)){
closest = nbr;
}
}
}
if(closest == 5527)
closest = 0;
System.out.println(closest);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment