Find smallest number with given digits and sum of digits | C ++ Program of the above approach ; Function to print the minimum integer having only digits P and Q and the sum of digits as N ; If Q is greater that P then swap the values of P and 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 ; Loop to find the maximum value of count_P that also satisfy P * count_P + Q * count_Q = N ; If N is 0 , their is a valid integer possible that satisfies all the requires conditions ; Print Answer ; Driver Code ; Function Call
#include <bits/stdc++.h> NEW_LINE 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 ; }