Biggest integer having maximum digit sum
Basic Accuracy: 10.44% Submissions: 647 Points: 1

Given a number N, the task is to find a number in range from 1 to N such that its sum is maximum. If there are several such numbers, print the biggest of them.

Input:
The first line of input contains an integer T denoting the number of test cases. Then T test cases follow. Each test case contains a number N as input.

Output:
For each test case, print the biggest number having maximum sum in new line.

Constraints:
1 <= T <= 100
1 <= N <= 1018

Example:
Input:
2

90
48

Output:
89
48

Explanation:

Input: n = 48
Output: 48
There are two numbers with maximum
digit sum. The numbers are 48 and 39
Since 48 > 39, it is our answer.

to report an issue on this page.

Editorial

We strongly recommend solving this problem on your own before viewing its editorial. Do you still want to view the editorial?

Yes

All Submissions

My Submissions:

Login to access your submissions.