Problem Statement
Given an integer n, find a number in the range from 1 to n whose digit sum is maximum.
If multiple numbers have the same maximum digit sum, return the largest number among them.
Example 1
Input: n = 48
Output: 48
The digit sums of the relevant numbers are:
Digit sum of 48 = 4 + 8 = 12
Digit sum of 39 = 3 + 9 = 12
Both numbers have the same maximum digit sum, but:
48 > 39
Therefore, the answer is:
48
Example 2
Input: n = 90
Output: 89
Because:
Digit sum of 90 = 9 + 0 = 9
Digit sum of 89 = 8 + 9 = 17
Therefore, the answer is:
89
Approach
A simple solution would be to check every number from 1 to n, calculate its digit sum, and keep track of the best answer.
However, this becomes inefficient when n is large.
For example, if:
n = 1,000,000,000
checking every number would require a very large number of operations.
Instead, we can use an important observation about decimal numbers.
Key Observation
For any candidate smaller than n, the best way to maximize its digit sum is to:
Choose a non-zero digit of
n.Decrease that digit by
1.Replace every digit to its right with
9.Keep the digits to its left unchanged.
Why does this work?
The digit 9 provides the largest possible contribution to a decimal digit sum. Therefore, after making a number smaller at one position, setting every following digit to 9 gives the maximum possible digit sum for that prefix.
For example:
n = 1234
Possible candidates generated from n include:
1234
1229
1199
999
Their digit sums are:
1234 → 1 + 2 + 3 + 4 = 10
1229 → 1 + 2 + 2 + 9 = 14
1199 → 1 + 1 + 9 + 9 = 20
999 → 9 + 9 + 9 = 27
So the best candidate is:
999
Example: n = 48
Start with:
n = 48
The digit sum is:
4 + 8 = 12
Now consider the tens digit.
Decrease:
4 → 3
and replace everything to its right with 9:
48
↓
39
The digit sum of 39 is:
3 + 9 = 12
We now have:
48 → 12
39 → 12
Both have the same digit sum, so we select the larger number:
48
Example: n = 90
Start with:
n = 90
Its digit sum is:
9 + 0 = 9
The last digit is 0, so it cannot be decreased.
Now consider the first digit:
9 → 8
Replace the digit to its right with 9:
90
↓
89
The digit sum becomes:
8 + 9 = 17
Therefore:
89
is better than 90.
Java Implementation
class Solution {
public int findMax(int n) {
int best = n;
int maxSum = digitSum(n);
int temp = n;
int place = 1;
while (temp > 0) {
int digit = temp % 10;
if (digit > 0) {
int candidate =
(n / (place * 10)) * (place * 10)
+ (digit - 1) * place
+ (place - 1);
int sum = digitSum(candidate);
if (sum > maxSum ||
(sum == maxSum && candidate > best)) {
maxSum = sum;
best = candidate;
}
}
temp /= 10;
place *= 10;
}
return best;
}
private int digitSum(int num) {
int sum = 0;
while (num > 0) {
sum += num % 10;
num /= 10;
}
return sum;
}
}
Code Explanation
1. Initialize the Answer
The first step is to consider n itself as a candidate.
int best = n;
int maxSum = digitSum(n);
For:
n = 48
we get:
best = 48
maxSum = 12
This is important because n itself can be the answer, as demonstrated by the n = 48 example.
2. Process Each Digit
The following variables are used to process the digits:
int temp = n;
int place = 1;
Then:
while (temp > 0) {
int digit = temp % 10;
The expression:
temp % 10
extracts the rightmost digit.
For example, if:
n = 1234
the digits are processed in this order:
4
3
2
1
The place variable represents the position of the current digit:
1 → units
10 → tens
100 → hundreds
1000 → thousands
3. Ignore Zero Digits
The code checks:
if (digit > 0) {
A digit must be greater than zero because the algorithm decreases it by one.
For example:
5 → 4
is valid.
But:
0 → -1
is not valid.
Therefore, zero digits are skipped.
4. Construct the Candidate
The most important part of the algorithm is:

Join the conversation! Your thoughts help the community grow.