Given a number n, find a number in range from 1 to n such that its sum is maximum. If there are several such integers, determine the biggest of them.
Input: n = 100 Output: 99 99 is the largest number in range from 1 to 100 with maximum sum of digits. Input: n = 48 Output: 48 Explanation: There are two numbers with maximum digit sum. The numbers are 48 and 39 Since 48 > 39, it is our answer.
A naive approach is to iterate for all numbers from 1 to n and find out which number has maximum sum of digits. Time complexity of this solution is O(n).
An efficient approach is to iterate from n to 1. Do the following for each digit of current number, if the digit is not zero, reduce it by one and change all other digits to nine to the right of it. If the sum of digits in the resulting integer is strictly greater than the sum of the digits of the current answer, then update the answer with the resulting integer. If the sum of the resulting integer is same as the current answer, then if the resulting integer is more then current answer, update the current answer with the resulting integer.
How to reduce a digit and change all other digits on its right to 9?
Let x be our current number. We can find next number for current digit using below formula. In below formula, b is a power of 10 to represent position of current digit. After every iteration we reduce x to x/10 and change b to b * 10.
We use (x – 1) * b + (b – 1);
This line can further be explained as, if the number is x = 521 and b = 1, then
- (521 – 1) * 1 + (1-1) which gives you 520, which is the thing we need to do, reduce the position number by 1 and replace all other numbers to the right by 9.
- After x /= 10 gives you x as 52 and b*=10 gives you b as 10, which is again executed as (52-1)*(10) + 9 which gives you 519, which is what we have to do, reduce the current index by 1 and increase all other rights by 9.
Time complexity : O(m) where m is the number of digits in n.
This article is contributed by Striver. If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to email@example.com. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.
- Radius of the biggest possible circle inscribed in rhombus which in turn is inscribed in a rectangle
- Biggest Reuleaux Triangle within a Square which is inscribed within a Circle
- Biggest Reuleaux Triangle within a Square which is inscribed within a Right angle Triangle
- Biggest Reuleaux Triangle inscribed within a square which is inscribed within an ellipse
- Biggest Reuleaux Triangle inscribed within a square which is inscribed within a hexagon
- Smallest integer which has n factors or more
- Count numbers in a range with digit sum divisible by K having first and last digit different
- Count of N-digit numbers having digit XOR as single digit
- Count of pairs (A, B) in range 1 to N such that last digit of A is equal to the first digit of B
- Count of Numbers in Range where first digit is equal to last digit of the number
- Largest number less than N with digit sum greater than the digit sum of N
- Count pairs (A, B) such that A has X and B has Y number of set bits and A+B = C
- Check if it is possible to create a matrix such that every row has A 1s and every column has B 1s
- Count of numbers from the range [L, R] which contains at least one digit that divides K
- Count of numbers in range which are divisible by M and have digit D at odd places
- Querying maximum number of divisors that a number in a given range has
- Digital Root (repeated digital sum) of square of an integer using Digital root of the given integer
- Smallest integer with digit sum M and multiple of N
- Check if the array has an element which is equal to sum of all the remaining elements
- Sum of the digits of square of the given number which has only 1's as its digits
Improved By : jit_t