Coding Solution

Temperatures – CodinGame solution in C

Easylinear searchabsolute valuetie-breakingedge casesUpdated
Short answer

Read every temperature once and keep the one with the smallest absolute value; when two readings are equally close to zero (like -5 and 5), keep the positive one. If the list is empty (N = 0), print 0. This single linear pass runs in O(n) time and O(1) memory.

The problem

You get a count N followed by a line of N integer temperatures, each between -273 and 5526. Your program must print the reading that lies closest to zero.

Two details decide whether you pass every test: when a negative and a positive value are at the same distance from zero, the positive one wins, and when N is 0 the line of readings is empty and the expected output is 0.

Approach, step by step

  1. Handle the empty case firstIf N is 0 there is nothing to compare, so print 0 and stop. Doing this up front avoids touching an empty line or returning a sentinel value such as INT_MAX by accident.
  2. Scan the list onceWalk through the readings in order and keep a single "best so far" variable. There is no need to store the whole list or to sort it: each value is compared once and then forgotten.
  3. Compare by absolute valueA reading T replaces the current best B when |T| < |B|. Absolute value turns "closest to zero" into a plain minimum search, which is the whole trick of the puzzle.
  4. Break ties toward the positive valueWhen |T| == |B|, the two values are either equal or opposite. Replace B only if T is positive, i.e. T == -B and T > 0. Equivalently, keep max(T, B) on a tie.
  5. Print the resultAfter the loop the variable holds the answer. Print it as a plain integer with no extra spaces.

Complexity

TimeO(n)
SpaceO(1)

Each reading is examined exactly once with a constant-time comparison, and only the current best value is kept in memory. Sorting would work too but costs O(n log n) for nothing.

Solution code

Bug fixed in this version: The C version reads the readings line into a 10,000-character buffer. With up to 9,999 values of up to 5 characters plus separators, the line can exceed that size; fgets then silently truncates it and later values are ignored. The official tests are small enough that it usually passes, but the replacement below reads each value with scanf and has no size limit.

Temperatures – C

#include <stdio.h>
#include <stdlib.h>

int main()
{
    int n;
    scanf("%d", &n);
    int best = 0; /* stays 0 when there is no reading */
    for (int i = 0; i < n; i++) {
        int t;
        scanf("%d", &t);
        if (i == 0 || abs(t) < abs(best) || (abs(t) == abs(best) && t > best))
            best = t;
    }
    printf("%d\n", best);
    return 0;
}

Common mistakes

Going further

The tie rule can be folded into a single ordering: sort key (|T|, -T). The smallest pair under that key is the answer, so in Python min(temps, key=lambda t: (abs(t), -t)) solves the puzzle in one line, and in C or Java you can compare those two numbers lexicographically. Thinking of it this way also shows why the order of the input never matters: you are computing a minimum under a total order, which is independent of the order you read the elements. A good self-test is the input "-273 5526 -1 1 -1": a correct solution prints 1 whatever the order of these five numbers.

FAQ

What should Temperatures print when there are no readings?

Print 0. When N is 0 the second line is empty, and the expected output is simply 0.

Which value wins between -5 and 5?

The positive one, 5. The puzzle says that when two readings are equally close to zero you must output the positive reading.

Do I need to sort the temperatures?

No. A single pass that keeps the best value is enough and is faster: O(n) instead of O(n log n) for sorting.

Similar puzzles

★ 5.0 · 16 reviews · 928 users Add to Chrome