How to find the largest number with given digit sum s and number of digits d?
Input : s = 9, d = 2 Output : 90 Input : s = 20, d = 3 Output : 992
A Simple Solution is to consider all m digit numbers and keep track of maximum number with digit sum as s. A close upper bound on time complexity of this solution is O(10m).
There is a Greedy approach to solve the problem. The idea is to one by one fill all digits from leftmost to rightmost (or from most significant digit to least significant).
We compare the remaining sum with 9 if the remaining sum is more than 9, we put 9 at the current position, else we put the remaining sum. Since we fill digits from left to right, we put the highest digits on the left side, hence get the largest number.
Below image is an illustration of the above approach:
Below is the implementation of the above approach:
Largest number is 90
Time Complexity of this solution is O(m).
This article is contributed by Vaibhav Agarwal. If you like GeeksforGeeks and would like to contribute, you can also write an article and 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.
- Find smallest number with given number of digits and sum of digits under given constraints
- Numbers of Length N having digits A and B and whose sum of digits contain only digits A and B
- Find smallest number with given number of digits and sum of digits
- Smallest number with given sum of digits and sum of square of digits
- Count numbers in given range such that sum of even digits is greater than sum of odd digits
- Find largest sum of digits in all divisors of n
- Find the average of k digits from the beginning and l digits from the end of the given number
- Largest number with the given set of N digits that is divisible by 2, 3 and 5
- Number formed by deleting digits such that sum of the digits becomes even and the number odd
- Sum of the digits of square of the given number which has only 1's as its digits
- Find the largest multiple of 3 from array of digits | Set 2 (In O(n) time and O(1) space)
- Maximize the given number by replacing a segment of digits with the alternate digits given
- Find the Largest Cube formed by Deleting minimum Digits from a number
- Find largest number smaller than N with same set of digits
- Find the largest number that can be formed by changing at most K digits
- Numbers with sum of digits equal to the sum of digits of its all prime factor
- Count of numbers between range having only non-zero digits whose sum of digits is N and number is divisible by M
- Find a number x such that sum of x and its digits is equal to given n.
- Count of groups having largest size while grouping according to sum of its digits
- Find the largest contiguous pair sum in given Array
Improved By : jit_t