- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Find smallest number with given number of digits and sum of digits in C++
In this problem, we are given two values that are the sum (denoting the sum of digits) and digit (denoting the number of digits). Our task is to find the smallest number with a given number of digits and sum of digits.
Let’s take an example to understand the problem,
Input
sum = 15, dgiti = 2
Output
69
Explanation
All 2 digits numbers with sum 15 are : 69, 78, 87, 96.
Solution Approach
A simple solution to the problem is by considering all the numbers with digit count as digit and find the smallest number whose sum of digit is equal to the sum.
An efficient solution is using the greedy approach. We will create the number by filling the elements from the last digit i.e. the LSB of the number. We will consider the largest possible element for LSB and then go for the next position.
We will try to keep the LSB as large as possible and MSB as small as possible.
Program to illustrate the working of our solution,
Example
#include <iostream> using namespace std; void findSmallestNumWithSum(int digit, int sum) { if (sum == 0) { if(digit == 1) cout<<"Smallest number is 0"; else cout<<"Smallest number with sum cannot be found"; return ; } if (sum > 9*digit) { cout<<"Smallest number with sum cannot be found"; return ; } int number[digit]; sum -= 1; for (int i = digit-1; i>0; i--) { if (sum > 9) { number[i] = 9; sum -= 9; } else { number[i] = sum; sum = 0; } } number[0] = sum + 1; cout<<"Smallest number is "; for (int i=0; i<digit; i++) cout<<number[i]; } int main() { int sum = 15, digit = 3; findSmallestNumWithSum(digit, sum); return 0; }
Output
Smallest number is 159
- Related Articles
- Find the Largest number with given number of digits and sum of digits in C++
- Find the difference between the smallest number of 7 digits and the largest number of digits.
- Find the difference between the smallest number of 7 digits and the largest number of 4 digits.
- Find the Number With Even Sum of Digits using C++
- Number of digits in the nth number made of given four digits in C++
- Minimum number with digits as and 7 only and given sum in C++
- C++ Program to Sum the digits of a given number
- Find sum of digits in factorial of a number in C++
- Program to find the sum of all digits of given number in Python
- Maximize the given number by replacing a segment of digits with the alternate digits given in C++
- The sum of digits of a two-digit number is 15. The number obtained by reversing the order of digits of the given number exceeds the given number by 9. Find the given number.
- Find the number of whole numbers between the smallest and the greatest number of 2 digits.
- Python | Sum of number digits in List
- C Program to sum the digits of a given number in single statement
- C program to find sum of digits of a five digit number
