Given two integers S and D, the task is to find the number having D number of digits and the sum of its digits as S such that the difference between the maximum and the minimum digit in the number is as minimum as possible. If multiple such numbers are possible, print the smallest number.
Input: S = 25, D = 4
The difference between maximum digit 7 and minimum digit 6 is 1.
Input: S = 27, D = 3
- Finding smallest number for given number of digits and sum is already discussed in this article.
- In this article, the idea is to minimize the difference between the maximum and minimum digit in the required number. Therefore, the sum s should be evenly distributed among d digits.
- If the sum is evenly distributed then the difference can be at most 1. The difference is zero when sum s is divisible by d. In that case, each of the digits has the same value equal to s/d.
- The difference is one when sum s is not divisible by d. In that case, after each digit is assigned value s/d, s%d sum value is still left to be distributed.
- As the smallest number is required, this remaining value is evenly distributed among last s%d digits of the number, i.e., last s%d digits in the number are incremented by one.
Below is the implementation of the above approach:
Time Complexity: O(d)
Auxiliary Space: O(1)
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.
- Check if it is possible to form string B from A under the given constraints
- Forming smallest array with given constraints
- 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
- Divide array into two parts with equal sum according to the given constraints
- Find the final X and Y when they are Altering under given condition
- Numbers of Length N having digits A and B and whose sum of digits contain only digits A and B
- Count digit groupings of a number with given constraints
- Maximum number of ones in a N*N matrix with given constraints
- Find Square Root under Modulo p | Set 1 (When p is in form of 4*i + 3)
- Find Square Root under Modulo p | Set 2 (Shanks Tonelli algorithm)
- Find power of power under mod of a prime
- Find Square Root under Modulo p | (When p is product of two primes in the form 4*i + 3)
- Construct a binary string following the given constraints
- Check if the Matrix follows the given constraints or not
- Count array elements having modular inverse under given prime number P equal to itself
- Nearest smaller number to N having multiplicative inverse under modulo N equal to that number
- Find the Largest number with given number of digits and sum of digits
- Find the kth smallest number with sum of digits as m
- Find the smallest number whose sum of digits is N
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 Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.