-
-
Save luisafvaca/952ef02e92954744aa3b8ea16be92fb9 to your computer and use it in GitHub Desktop.
Temperatures - Solved
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
/** 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