Skip to content
Related Articles

Related Articles

Improve Article
Save Article
Like Article

Find smallest number with given digits and sum of digits

  • Difficulty Level : Easy
  • Last Updated : 03 Sep, 2021

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:

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.  To complete your preparation from learning a language to DS Algo and many more,  please refer Complete Interview Preparation Course.

In case you wish to attend live classes with experts, please refer DSA Live Classes for Working Professionals and Competitive Programming Live for Students.

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 :

  1. Initialize count_P and count_Q as 0.
  2. If N is divisible by P, count_P = N/P and N=0.
  3. If N is not divisible by P, subtract Q from N and increment count_Q by 1.
  4. Repeat steps number 2 and 3 until N is greater than 0.
  5. 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, their 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, their 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, their 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, their 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, their 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>
Output
47777

Time Complexity: O(N)
Space Complexity: O(1)




My Personal Notes arrow_drop_up
Recommended Articles
Page :

Start Your Coding Journey Now!