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; ` `} ` |

*chevron_right*

*filter_none*

## 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 ` |

*chevron_right*

*filter_none*

## 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 ` |

*chevron_right*

*filter_none*

## 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 ` |

*chevron_right*

*filter_none*

**Output:**

6667

**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.

## Recommended Posts:

- 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
- Forming smallest array with given constraints
- Numbers of Length N having digits A and B and whose sum of digits contain only digits A and B
- Find the Largest number with given number of digits and sum of digits
- Find the final X and Y when they are Altering under given condition
- Find the kth smallest number with sum of digits as m
- Find the smallest number whose sum of digits is N
- Smallest odd number with even sum of digits from the given number N
- Find the smallest number whose digits multiply to a given number n
- Find smallest possible Number from a given large Number with same count of digits
- Find smallest number formed by inverting digits of given number N
- 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)
- Smallest number with sum of digits as N and divisible by 10^N
- Count numbers in given range such that sum of even digits is greater than sum of odd digits

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. 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.