Find smallest number with given digits and sum of digits
Last Updated :
24 Mar, 2023
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: 47
Explanation: There are two possible integers that can be formed from 4 and 7 such that their sum is 11 i.e. 47 and 74. Since we need to find the minimum possible value, 47 is the required answer.
Input: N = 11, P = 9, Q = 7
Output: Not Possible
Explanation: It is not possible to create an integer using digits 7 and 9 such that their sum is 11.
Efficient Approach: Let's consider P is greater than or equal to Q, count_P denotes the number of occurrences of P and count_Q denoted the number of occurrences of Q in the resulting integer. So, the question can be represented in the form of an equation (P * count_P) + (Q * count_Q) = N, and in order to minimize the count of digits in the resulting integer count_P + count_Q should be as minimum as possible. It can be observed that since P >= Q, the maximum possible value of count_P that satisfies (P * count_P) + (Q * count_Q) = N will be the most optimal choice. Below are the steps for the above approach :
- Initialize count_P and count_Q as 0.
- If N is divisible by P, count_P = N/P and N=0.
- If N is not divisible by P, subtract Q from N and increment count_Q by 1.
- Repeat steps number 2 and 3 until N is greater than 0.
- If N != 0, it is not possible to generate the integer that satisfies the required conditions. Else the resulting integer will be count_Q times Q followed by count_P times P.
Below is the implementation of the above approach:
C++
// C++ Program of the above approach
#include <bits/stdc++.h>
using namespace std;
// Function to print the minimum
// integer having only digits P and
// Q and the sum of digits as N
void printMinInteger(int P, int Q, int N)
{
// If Q is greater that P then
// swap the values of P and Q
if (Q > P) {
swap(P, Q);
}
// If P and Q are both zero or
// if Q is zero and N is not
// divisible by P then there
// is no possible integer which
// satisfies the given conditions
if (Q == 0 && (P == 0 || N % P != 0)) {
cout << "Not Possible";
return;
}
int count_P = 0, count_Q = 0;
// Loop to find the maximum value
// of count_P that also satisfy
// P*count_P + Q*count_Q = N
while (N > 0) {
if (N % P == 0) {
count_P += N / P;
N = 0;
}
else {
N = N - Q;
count_Q++;
}
}
// If N is 0, there is a valid
// integer possible that satisfies
// all the requires conditions
if (N == 0) {
// Print Answer
for (int i = 0; i < count_Q; i++)
cout << Q;
for (int i = 0; i < count_P; i++)
cout << P;
}
else {
cout << "Not Possible";
}
}
// Driver Code
int main()
{
int N = 32;
int P = 7;
int Q = 4;
// Function Call
printMinInteger(P, Q, N);
return 0;
}
Java
// Java program for the above approach
import java.io.*;
class GFG {
// Function to print the minimum
// integer having only digits P and
// Q and the sum of digits as N
static void printMinInteger(int P, int Q, int N)
{
// If Q is greater that P then
// swap the values of P and Q
if (Q > P) {
int temp;
temp = P;
P = Q;
Q = temp;
}
// If P and Q are both zero or
// if Q is zero and N is not
// divisible by P then there
// is no possible integer which
// satisfies the given conditions
if (Q == 0 && (P == 0 || N % P != 0)) {
System.out.println("Not Possible");
return;
}
int count_P = 0, count_Q = 0;
// Loop to find the maximum value
// of count_P that also satisfy
// P*count_P + Q*count_Q = N
while (N > 0) {
if (N % P == 0) {
count_P += N / P;
N = 0;
}
else {
N = N - Q;
count_Q++;
}
}
// If N is 0, there is a valid
// integer possible that satisfies
// all the requires conditions
if (N == 0) {
// Print Answer
for (int i = 0; i < count_Q; i++)
System.out.print(Q);
for (int i = 0; i < count_P; i++)
System.out.print(P);
}
else {
System.out.println("Not Possible");
}
}
// Driver code
public static void main(String[] args)
{
int N = 32;
int P = 7;
int Q = 4;
// Function Call
printMinInteger(P, Q, N);
}
}
// This code is contributed by code_hunt.
Python3
# Python3 program for the above approach
# Function to print minimum
# integer having only digits P and
# Q and the sum of digits as N
def printMinInteger(P, Q, N):
# If Q is greater that P then
# swap the values of P and Q
if (Q > P):
t = P
P = Q
Q = t
# If P and Q are both zero or
# if Q is zero and N is not
# divisible by P then there
# is no possible integer which
# satisfies the given conditions
if (Q == 0 and (P == 0 or N % P != 0)):
print("Not Possible")
return
count_P = 0
count_Q = 0
# Loop to find the maximum value
# of count_P that also satisfy
# P*count_P + Q*count_Q = N
while (N > 0):
if (N % P == 0):
count_P += N / P
N = 0
else:
N = N - Q
count_Q += 1
# If N is 0, there is a valid
# integer possible that satisfies
# all the requires conditions
if (N == 0):
# Print Answer
for i in range(count_Q):
print(Q, end = "")
for i in range(int(count_P)):
print(P, end = "")
else:
print("Not Possible")
# Driver Code
N = 32
P = 7
Q = 4
# Function Call
printMinInteger(P, Q, N)
# This code is contributed by code_hunt
C#
// C# program for the above approach
using System;
public class GFG {
// Function to print the minimum
// integer having only digits P and
// Q and the sum of digits as N
static void printMinint(int P, int Q, int N)
{
// If Q is greater that P then
// swap the values of P and Q
if (Q > P) {
int temp;
temp = P;
P = Q;
Q = temp;
}
// If P and Q are both zero or
// if Q is zero and N is not
// divisible by P then there
// is no possible integer which
// satisfies the given conditions
if (Q == 0 && (P == 0 || N % P != 0)) {
Console.WriteLine("Not Possible");
return;
}
int count_P = 0, count_Q = 0;
// Loop to find the maximum value
// of count_P that also satisfy
// P*count_P + Q*count_Q = N
while (N > 0) {
if (N % P == 0) {
count_P += N / P;
N = 0;
}
else {
N = N - Q;
count_Q++;
}
}
// If N is 0, there is a valid
// integer possible that satisfies
// all the requires conditions
if (N == 0) {
// Print Answer
for (int i = 0; i < count_Q; i++)
Console.Write(Q);
for (int i = 0; i < count_P; i++)
Console.Write(P);
}
else {
Console.WriteLine("Not Possible");
}
}
// Driver code
public static void Main(String[] args)
{
int N = 32;
int P = 7;
int Q = 4;
// Function Call
printMinint(P, Q, N);
}
}
// This code contributed by shikhasingrajput
JavaScript
<script>
// Javascript Program of the above approach
// Function to print the minimum
// integer having only digits P and
// Q and the sum of digits as N
function printMinInteger(P, Q, N) {
// If Q is greater that P then
// swap the values of P and Q
if (Q > P) {
swap(P, Q);
}
// If P and Q are both zero or
// if Q is zero and N is not
// divisible by P then there
// is no possible integer which
// satisfies the given conditions
if (Q == 0 && (P == 0 || N % P != 0)) {
document.write("Not Possible");
return;
}
let count_P = 0,
count_Q = 0;
// Loop to find the maximum value
// of count_P that also satisfy
// P*count_P + Q*count_Q = N
while (N > 0) {
if (N % P == 0) {
count_P += Math.floor(N / P);
N = 0;
} else {
N = N - Q;
count_Q++;
}
}
// If N is 0, there is a valid
// integer possible that satisfies
// all the requires conditions
if (N == 0) {
// Print Answer
for (let i = 0; i < count_Q; i++) document.write(Q);
for (let i = 0; i < count_P; i++) document.write(P);
} else {
document.write("Not Possible");
}
}
// Driver Code
let N = 32;
let P = 7;
let Q = 4;
// Function Call
printMinInteger(P, Q, N);
// This code is contributed by gfgking.
</script>
Time Complexity: O(N)
Space Complexity: O(1)
Similar Reads
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
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
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
Find the kth smallest number with sum of digits as m Given two integers M and K, the task is to find the Kth smallest number with digit sum as M.Examples: Input: M = 5, K = 3 Output: 23 Sequence of numbers starting from 1 with digit sum as 5 is as follows: 5 14 23 32 41 So 3rd smallest number is 23Input: M = 4, K = 1 Output: 4 Approach: We need to fin
6 min read
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: I
7 min read
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 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
Find the smallest number whose sum of digits is N Given a positive integers N, the task is to find the smallest number whose sum of digits is N.Example: Input: N = 10Output: 19Explanation: 1 + 9 = 10 = N Input: N = 18Output: 99Explanation: 9 + 9 = 18 = N Naive Approach: A Naive approach is to run a loop of i starting from 0 and find Sum of digits o
6 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
Smallest integer with digit sum M and multiple of N Given two positive integers N and M, the task is to find the smallest positive integer which is divisible by N and whose digit sum is M. Print -1 if no such integer exists within the range of int. Examples: Input: N = 13, M = 32 Output: 8879 8879 is divisible by 13 and its Sum of digits of 8879 is 8
5 min read