Find smallest number with given number of digits and sum of digits under given constraints Last Updated : 21 Sep, 2022 Comments Improve Suggest changes Like Article Like Report 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.Examples: Input: S = 25, D = 4 Output: 6667 The difference between maximum digit 7 and minimum digit 6 is 1. Input: S = 27, D = 3 Output: 999 Approach: 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: C++ // C++ implementation of the approach #include <bits/stdc++.h> using namespace std; // Function to find the number having // sum of digits as s and d number of // digits such that the difference between // the maximum and the minimum digit // the minimum possible string findNumber(int s, int d) { // To store the final number string num = ""; // To store the value that is evenly // distributed among all the digits int val = s / d; // To store the remaining sum that still // remains to be distributed among d digits int rem = s % d; int i; // rem stores the value that still remains // to be distributed // To keep the difference of digits minimum // last rem digits are incremented by 1 for (i = 1; i <= d - rem; i++) { num = num + to_string(val); } // In the last rem digits one is added to // the value obtained by equal distribution if (rem) { val++; for (i = d - rem + 1; i <= d; i++) { num = num + to_string(val); } } return num; } // Driver function int main() { int s = 25, d = 4; cout << findNumber(s, d); return 0; } Java // Java implementation of the approach import java.util.*; class GFG { // Function to find the number having // sum of digits as s and d number of // digits such that the difference between // the maximum and the minimum digit // the minimum possible static String findNumber(int s, int d) { // To store the final number String num = ""; // To store the value that is evenly // distributed among all the digits int val = s / d; // To store the remaining sum that still // remains to be distributed among d digits int rem = s % d; int i; // rem stores the value that still remains // to be distributed // To keep the difference of digits minimum // last rem digits are incremented by 1 for (i = 1; i <= d - rem; i++) { num = num + String.valueOf(val); } // In the last rem digits one is added to // the value obtained by equal distribution if (rem > 0) { val++; for (i = d - rem + 1; i <= d; i++) { num = num + String.valueOf(val); } } return num; } // Driver function public static void main(String[] args) { int s = 25, d = 4; System.out.print(findNumber(s, d)); } } // This code is contributed by 29AjayKumar Python3 # Python3 implementation of the approach # Function to find the number having # sum of digits as s and d number of # digits such that the difference between # the maximum and the minimum digit # the minimum possible def findNumber(s, d) : # To store the final number num = "" # To store the value that is evenly # distributed among all the digits val = s // d # To store the remaining sum that still # remains to be distributed among d digits rem = s % d # rem stores the value that still remains # to be distributed # To keep the difference of digits minimum # last rem digits are incremented by 1 for i in range(1, d - rem + 1) : num = num + str(val) # In the last rem digits one is added to # the value obtained by equal distribution if (rem) : val += 1 for i in range(d - rem + 1, d + 1) : num = num + str(val) return num # Driver function if __name__ == "__main__" : s = 25 d = 4 print(findNumber(s, d)) # This code is contributed by AnkitRai01 C# // C# implementation of the approach using System; class GFG { // Function to find the number having // sum of digits as s and d number of // digits such that the difference between // the maximum and the minimum digit // the minimum possible static String findNumber(int s, int d) { // To store the readonly number String num = ""; // To store the value that is evenly // distributed among all the digits int val = s / d; // To store the remaining sum that still // remains to be distributed among d digits int rem = s % d; int i; // rem stores the value that still remains // to be distributed // To keep the difference of digits minimum // last rem digits are incremented by 1 for (i = 1; i <= d - rem; i++) { num = num + String.Join("", val); } // In the last rem digits one is added to // the value obtained by equal distribution if (rem > 0) { val++; for (i = d - rem + 1; i <= d; i++) { num = num + String.Join("", val); } } return num; } // Driver function public static void Main(String[] args) { int s = 25, d = 4; Console.Write(findNumber(s, d)); } } // This code is contributed by 29AjayKumar JavaScript <script> // Javascript implementation of the approach // Function to find the number having // sum of digits as s and d number of // digits such that the difference between // the maximum and the minimum digit // the minimum possible function findNumber(s, d) { // To store the final number var num = [] ; // To store the value that is evenly // distributed among all the digits var val = parseInt(s / d); // To store the remaining sum that still // remains to be distributed among d digits var rem = s % d; // rem stores the value that still remains // to be distributed // To keep the difference of digits minimum // last rem digits are incremented by 1 for (var i = 1; i <= d - rem; i++) { // num = num.concat(toString(val)); num.push(val.toString()); } // In the last rem digits one is added to // the value obtained by equal distribution if (rem != 0) { val++; for (var i = d - rem + 1; i <= d; i++) { // num = num + toString(val); num.push(val.toString()); } } return num; } var s = 25, d = 4; var n=findNumber(s, d); for(var i = 0; i < n.length; i++) { document.write(n[i]); } // This code is contributed by SoumikMondal </script> Output: 6667 Time Complexity: O(d) Auxiliary Space: O(d) Comment More infoAdvertise with us Next Article Find smallest number with given number of digits and sum of digits under given constraints nik1996 Follow Improve Article Tags : Algorithms Mathematical DSA number-digits Practice Tags : AlgorithmsMathematical Similar Reads Find the Largest number with given number of digits and sum of digits Given an integer s and d, The task is to find the largest number with given digit sum s and the number of digits d. Examples: Input: s = 9, d = 2Output: 90 Input: s = 20, d = 3Output: 992 Recommended PracticeLargest number possibleTry It! Naive Approach: Consider all m digit numbers and keep a max v 13 min read Find smallest number with given digits and sum of digits Given two positive integers P and Q, find the minimum integer containing only digits P and Q such that the sum of the digits of the integer is N. Example: Input: N = 11, P = 4, Q = 7 Output: 47Explanation: There are two possible integers that can be formed from 4 and 7 such that their sum is 11 i.e. 9 min read Smallest number with given sum of digits and sum of square of digits Given the sum of digits a and sum of the square of digits b . Find the smallest number with the given sum of digits and the sum of the square of digits. The number should not contain more than 100 digits. Print -1 if no such number exists or if the number of digits is more than 100.Examples: Input : 15+ min read Find second smallest number from sum of digits and number of digits Given the sum of digits as S and the number of digits as D, the task is to find the second smallest number Examples: Input: S = 9, D = 2Output: 27Explanation: 18 is the smallest number possible with sum = 9 and total digits = 2, Whereas the second smallest is 27. Input: S = 16, D = 3Output: 178Expla 8 min read Smallest number with sum of digits as N and divisible by 10^N Find the smallest number such that the sum of its digits is N and it is divisible by 10^N . Examples : Input : N = 5 Output : 500000 500000 is the smallest number divisible by 10^5 and sum of digits as 5. Input : N = 20 Output : 29900000000000000000000Recommended PracticeSmallest number with sum of 6 min read Count number of integers in given range with adjacent digits different and sum of digits equal to M Given integers T, A, and B, the task for this problem is to find numbers in the range [A, B] such that the adjacent digits of the number are different and the sum of digits is equal to T. ( A ? B ? 1018) Examples: Input: T = 5, A = 1, B = 100Output: 6Explanation: 5, 14, 23, 32, 41, and 50 are valid 15+ min read Finding sum of digits of a number until sum becomes single digit Given an integer n, we need to repeatedly find the sum of its digits until the result becomes a single-digit number.Examples:Input: n = 1234 Output: 1 Explanation:Step 1: 1 + 2 + 3 + 4 = 10 Step 2: 1 + 0 = 1Input: n = 5674Output: 4Explanation: Step 1: 5 + 6 + 7 + 4 = 22 Step 2: 2 + 2 = 4Table of Con 5 min read Counting numbers with given digits and digit sum Given a number N, count the numbers X of length exactly N such that the number X and the sum of digits of the number X have digits A and B only in their decimal representation. The length of a number is defined as the number of digits in its decimal representation without leading zeroes. Note: As th 11 min read Count numbers formed by given two digit with sum having given digits Given a, b and N(1 to 106). Task is to count the numbers formed by digits a and b exactly of a length N such that the sum of the digits of the number thus formed also contains digits a and b only. Since the count can be very large, print the count % 1000000007. Examples : Input : a = 1 b = 3 n = 3 O 15 min read Smallest number with given digit count and sum Given two integers s and d, find the smallest possible number that has exactly d digits and a sum of digits equal to s.Return the number as a string. If no such number exists, return "-1".Examples :Input: s = 9, d = 2Output: 18 Explanation: 18 is the smallest number possible with the sum of digits = 10 min read Like