Ballpark Estimate
InterviewTime limit2sMemory limit512 MB
Given n up to 1e18, print the closest number that has a single non-zero digit, breaking ties toward the larger value.
- Level
Medium5 of 10
- Topics
- Math, Implementation, Greedy, Brute force
- Solved
- No attempts yet
Problem
Giving the right level of detail is an important skill for efficient communication. Sometimes, only the high-level message matters.
For example, whenever a person asks for a number, often they just want an estimate. If the value is in the millions, they do not need to know the precise number of hundreds and tens. Likewise, if the value is in the billions, they do not necessarily care about little things like millions.

Figure B.1: Illustration of ballpark figures versus actual figures, as a log chart.
Given a (possibly very large) number n, print its numerically closest representation that has only one digit other than trailing zeroes.
The closeness of the representation r of a number n is defined by abs(r − n).
Input
The input consists of:
- one line with the positive integer n (1 ≤ n ≤ 1018).
Output
Output the closest number to n with exactly one significant (non-zero) figure. If there are two equally-close answers, print the larger one.