Find smallest number with given number of digits and sum of digits under given constraints
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++
#include <bits/stdc++.h>
using namespace std;
string findNumber( int s, int d)
{
string num = "" ;
int val = s / d;
int rem = s % d;
int i;
for (i = 1; i <= d - rem; i++) {
num = num + to_string(val);
}
if (rem) {
val++;
for (i = d - rem + 1; i <= d; i++) {
num = num + to_string(val);
}
}
return num;
}
int main()
{
int s = 25, d = 4;
cout << findNumber(s, d);
return 0;
}
|
Java
import java.util.*;
class GFG
{
static String findNumber( int s, int d)
{
String num = "" ;
int val = s / d;
int rem = s % d;
int i;
for (i = 1 ; i <= d - rem; i++)
{
num = num + String.valueOf(val);
}
if (rem > 0 )
{
val++;
for (i = d - rem + 1 ; i <= d; i++)
{
num = num + String.valueOf(val);
}
}
return num;
}
public static void main(String[] args)
{
int s = 25 , d = 4 ;
System.out.print(findNumber(s, d));
}
}
|
Python3
def findNumber(s, d) :
num = ""
val = s / / d
rem = s % d
for i in range ( 1 , d - rem + 1 ) :
num = num + str (val)
if (rem) :
val + = 1
for i in range (d - rem + 1 , d + 1 ) :
num = num + str (val)
return num
if __name__ = = "__main__" :
s = 25
d = 4
print (findNumber(s, d))
|
C#
using System;
class GFG
{
static String findNumber( int s, int d)
{
String num = "" ;
int val = s / d;
int rem = s % d;
int i;
for (i = 1; i <= d - rem; i++)
{
num = num + String.Join( "" , val);
}
if (rem > 0)
{
val++;
for (i = d - rem + 1; i <= d; i++)
{
num = num + String.Join( "" , val);
}
}
return num;
}
public static void Main(String[] args)
{
int s = 25, d = 4;
Console.Write(findNumber(s, d));
}
}
|
Javascript
<script>
function findNumber(s, d)
{
var num = [] ;
var val = parseInt(s / d);
var rem = s % d;
for ( var i = 1; i <= d - rem; i++)
{
num.push(val.toString());
}
if (rem != 0)
{
val++;
for ( var i = d - rem + 1; i <= d; i++)
{
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]);
}
</script>
|
Time Complexity: O(d)
Auxiliary Space: O(d)
Last Updated :
21 Sep, 2022
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...