Just in case, someone struggles and needs some help, here is the idea:
we know that the closest negative to the Zero is the max value of the negative right? and the closest positive to the Zero is the minimum number among the positives.
According to that statement, calculate that and handle the case where the distance to Zero is equal ( e.g -5 , 5 ) the distance is 5 to the Zero, hence the answer is 5 according to the problem statement.
Hey, so I thought I was doing really well until my code didnât work because I donât have a size-1 array⊠Does anybody have any advice on how I could maybe fix that?
Hi i dont understand why my code isnât workin i tested on the terminal of node.js and it work but now in codingame didnât, any one can tell me why? thanks before hand.
const n = parseInt(readline()); // the number of temperatures to analyse
let numbers = readline().split(' ');
let pos = [];
let neg = [];
let num;
if(n > 0){
for(i in numbers)
{
const t = parseInt(numbers[i])
if(numbers[i]<0){
neg.push(numbers[i]);
}
else if(numbers[i]>0){
pos.push(numbers[i]);
}
pos.sort(function(a,b){return a-b});
neg.sort(function(a,b){return b-a});
}
if(pos[0]===Math.abs(neg[0])) {
console.log(pos[0])
}else if(pos[0]<Math.abs(neg[0])){
console.log(pos[0]);
}else if (pos[0]>Math.abs(neg[0]) ||pos[0]===undefined){
console.log(neg[0]);
}
}
else{
console.log(0);
}
Test âChoose the right temperatureâ test is broken in Python.
The inputs provide both -5 and 5 and my code selects -5 because it sees -5 first. The test then rejects -5 as a valid answer and yet both answers have the same distance to zero.
Write a program that prints the temperature closest to 0 among input data. If two numbers are equally close to zero, positive integer has to be considered closest to zero (for instance, if the temperatures are -5 and 5, then display 5).
Hello,
Quote of the rules: If two numbers are equally close to zero, positive integer has to be considered closest to zero (for instance, if the temperatures are -5 and 5, then display 5).
So, no, itâs not valid, sorry
Your script returns the mininimal temperature, and if the min is negative and the positive corresponding temperature exists, it returns the positive one.
Itâs not what is asked.
Your approach would work if you sort by absolute value instead of default sorting.
But if youâre going to iterate over the values anyway, you can just add a condition here, like âif the absolute value is <= than absolute value of the current min and âŠâ