Skip to content

Instantly share code, notes, and snippets.

@luisafvaca
Last active February 19, 2021 04:01
Show Gist options
  • Save luisafvaca/952ef02e92954744aa3b8ea16be92fb9 to your computer and use it in GitHub Desktop.
Save luisafvaca/952ef02e92954744aa3b8ea16be92fb9 to your computer and use it in GitHub Desktop.
Temperatures - Solved
/** You can copy and paste this code in this patform
* https://www.codingame.com/ide/puzzle/temperatures
*to test cases and run correctly this code
**/
/**
* Auto-generated code below aims at helping you parse
* the standard input according to the problem statement.
**/
/**
* Auto-generated code below aims at helping you parse
* the standard input according to the problem statement.
**/
const n = parseInt(readline()); // the number of temperatures to analyse
var inputs = readline().split(' ').filter(temp => parseInt(temp));
let moreThan0 = [];
let lessThan0 = [];
if(inputs.length <= 0){
console.log(0)
} else {
for (let i = 0; i < n; i++) {
const t = inputs[i];// a temperature expressed as an integer ranging from -273 to 5526
if(t>0){
moreThan0.push(t)
}
if(t<0){
lessThan0.push(t)
}
}
}
function calcNearToZero(arr1,arr2){
if(arr1.length > 0 && arr2.length > 0){
const a = arr1[0];
const b = arr2[0];
if(Math.abs(a) === Math.abs(b)){
console.log(a)
} else {
console.log(a)
}
}
if(arr1.length === 0 && arr2.length>0){
const a = arr2[0];
console.log(a)
} else if(arr2.length === 0 && arr1.length>0){
const a = arr1[0];
console.log(a)
}
}
calcNearToZero(moreThan0.sort((a,b)=>a-b), lessThan0.sort((a,b)=>a-b).reverse());
// Write an answer using console.log()
// To debug: console.error('Debug messages...');
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment