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++
#include <bits/stdc++.h>
using namespace std;
void printMinInteger( int P, int Q, int N)
{
if (Q > P) {
swap(P, Q);
}
if (Q == 0 && (P == 0 || N % P != 0)) {
cout << "Not Possible" ;
return ;
}
int count_P = 0, count_Q = 0;
while (N > 0) {
if (N % P == 0) {
count_P += N / P;
N = 0;
}
else {
N = N - Q;
count_Q++;
}
}
if (N == 0) {
for ( int i = 0; i < count_Q; i++)
cout << Q;
for ( int i = 0; i < count_P; i++)
cout << P;
}
else {
cout << "Not Possible" ;
}
}
int main()
{
int N = 32;
int P = 7;
int Q = 4;
printMinInteger(P, Q, N);
return 0;
}
|
Java
import java.io.*;
class GFG {
static void printMinInteger( int P, int Q, int N)
{
if (Q > P) {
int temp;
temp = P;
P = Q;
Q = temp;
}
if (Q == 0 && (P == 0 || N % P != 0 )) {
System.out.println( "Not Possible" );
return ;
}
int count_P = 0 , count_Q = 0 ;
while (N > 0 ) {
if (N % P == 0 ) {
count_P += N / P;
N = 0 ;
}
else {
N = N - Q;
count_Q++;
}
}
if (N == 0 ) {
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" );
}
}
public static void main(String[] args)
{
int N = 32 ;
int P = 7 ;
int Q = 4 ;
printMinInteger(P, Q, N);
}
}
|
Python3
def printMinInteger(P, Q, N):
if (Q > P):
t = P
P = Q
Q = t
if (Q = = 0 and (P = = 0 or N % P ! = 0 )):
print ( "Not Possible" )
return
count_P = 0
count_Q = 0
while (N > 0 ):
if (N % P = = 0 ):
count_P + = N / P
N = 0
else :
N = N - Q
count_Q + = 1
if (N = = 0 ):
for i in range (count_Q):
print (Q, end = "")
for i in range ( int (count_P)):
print (P, end = "")
else :
print ( "Not Possible" )
N = 32
P = 7
Q = 4
printMinInteger(P, Q, N)
|
C#
using System;
public class GFG {
static void printMinint( int P, int Q, int N)
{
if (Q > P) {
int temp;
temp = P;
P = Q;
Q = temp;
}
if (Q == 0 && (P == 0 || N % P != 0)) {
Console.WriteLine( "Not Possible" );
return ;
}
int count_P = 0, count_Q = 0;
while (N > 0) {
if (N % P == 0) {
count_P += N / P;
N = 0;
}
else {
N = N - Q;
count_Q++;
}
}
if (N == 0) {
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" );
}
}
public static void Main(String[] args)
{
int N = 32;
int P = 7;
int Q = 4;
printMinint(P, Q, N);
}
}
|
Javascript
<script>
function printMinInteger(P, Q, N) {
if (Q > P) {
swap(P, Q);
}
if (Q == 0 && (P == 0 || N % P != 0)) {
document.write( "Not Possible" );
return ;
}
let count_P = 0,
count_Q = 0;
while (N > 0) {
if (N % P == 0) {
count_P += Math.floor(N / P);
N = 0;
} else {
N = N - Q;
count_Q++;
}
}
if (N == 0) {
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" );
}
}
let N = 32;
let P = 7;
let Q = 4;
printMinInteger(P, Q, N);
</script>
|
Time Complexity: O(N)
Space Complexity: O(1)
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...