Temperatures puzzle discussion

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.

Hope this helps! :slight_smile:

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? :slight_smile:

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);
}

image
Your code doesn’t return anything for the 3rd test.

Bonjour, je crois qu’il y a un bug en JS. En effet, lorsque que la tempĂ©rature n’est pas dans la fourchette (ex: 5527) le programme de rentre meme pas dans le for (for qui est prĂ©sent Ă  l’initialisation)

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.

https://gyazo.com/e2995ff62785d0955e6a98f95a888d7c

Edit: Please ignore, I failed to RTFM. Thank you @Astrobytes

1 Like

From the statement:

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).

1 Like

Can, anyone help me with the condition of test case when there were no input and answer was zero in C++.

If you use std::min_element, it returns last if range is empty, so it should’nt be hard to manage.

You could also use a for loop and update min.

Hello! Just a little error I would like to point out to not confuse future programmers:

Standard Output Stream:

-2

Failure

Found:

-2

Expected:

2

I have not used the absolute values of temperatures, thus I have a different result, but it is still valid (I hope :-).
Have a good day.

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 :wink:

1 Like

Oh, I am very sorry
 it was late and I have not seen that rule
 sorry for disturbing :slight_smile:.

Have a nice day!

/*

 *author:DJ

 *CODE GIVEN ABOVE ;)

 *

 *

 */

// #pragma GCC optimize("O3") 

#include <iostream>

#include <algorithm>

#include <cstdlib>

#include <vector>

#include <numeric>

using namespace std ; 

using ll = long long ;

int main(){

    int n ;

    vector<int> v,v1 ;

    cin >> n; 

    if(n == 0){

        cout << "0" << "\n" ;

    }

    else{

        for(int i = 0;i < n ; ++i){

            int t ; 

            cin >> t ; 

            v.push_back(abs(t-0)) ;

            v1.push_back(t);

        }

        sort(v.begin(),v.end()) ;

        int cnt = 0 ,imax = 0,sum = 0 ;

        for(auto it = v1.begin();it != v1.end(); ++it){

            if(*it == v[0] || *it == (-1)*v[0]){

                cnt++ ;

                imax = *it ;

            }

        }

        if(cnt == 2){

            cout << v[0] << "\n" ;

        }

        else if(cnt == 1){

            cout << imax << "\n" ;

        }

        else if(cnt == 2 && accumulate(v1.begin(),v1.end(),sum) < 0){

            cout << imax << "\n" ;

        }

    }

    return 0 ;

}

I have submitted this code, its failing one test case how the both negative ones, how??

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 
”

1 Like

Thanks, I am trying my best to solve the question.

I always pass everty single test, excpet the validator 7. No idea on why this is happening.

Solutions?

If you can’t figure out the problem by looking at your code, how could we without seeing it ?

The name of validator 7 is very informative:

Two negative temperatures that are equal: {-10 -10}

1 Like

Bonjour, le dernier test bloque: Si le rĂ©sultat est 0, il me dit qu’il attend Rien, si le rĂ©sultat est Rien il me dit qu’il attend 0


Salut !
PrĂ©fĂšre l’anglais si tu peux.
Le rĂ©sultat attendu est bien 0 pour ce test. Est-tu sĂ»re que c’est bien ce que tu renvois ? Et si oui est-tu sĂ»re de ne rien afficher d’autre (espace, plusieurs sauts de lignes, 
) ?

1 Like