- 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 the Largest number with given number of digits and sum of digits in C++
In this problem, we are given two integer values, N denoting the count of digits of a number and sum denoting the sum of digits of the number. Our task is to find the largest number with given number of digits and sum of digits.
Let's take an example to understand the problem,
Input : N = 3, sum = 15 Output : 960
Solution Approach
A simple approach to solve the problem is by traversing all N digit numbers from the largest to smallest. The find the digit sum numbers, if it's equal to sum return the number.
Example
Program to illustrate the working of our solution
#include <iostream> using namespace std; int digitSum(int n){ int sum = 0; while(n){ sum += n%10; n = n/10; } return sum; } int findLargestNumWithSum(int N, int sum){ if (sum == 0){ if(N == 1) return -1; else return -1; } if (sum > 9*N){ return -1; } int num = 1; for(int i = 0; i < N; i++) num *= 10; while(1){ if(digitSum(num) == sum){ return num; } num -- ; if(num == 0) return -1; } } int main(){ int sum = 25, N = 3; cout<<"The largest "<<N<<" digit number with sum "<<sum<<" is "<< findLargestNumWithSum(N, sum); return 0; }
Output
The largest 3 digit number with sum 25 is 997
Another approach to solve the problem is by using Greedy Approach. We will do this by starting from the MSB, placing the highest possible number from sum and subtracting it from the sum.
We will perform this step for N times, we will get the required number. So, if the sum is greater than 9, place 9 to the current digit, if it is less than 9, place sum to the current digit. Do this process, for all digits from MSB to LSB placing digits.
Example
Program to illustrate the working of our solution
#include <iostream> using namespace std; int findLargestNumWithSum(int N, int sum){ if (sum == 0){ if(N == 1) return -1; else return -1; } if (sum > 9*N){ return -1; } int num = 0; for (int i = 0; i < N; i++){ if (sum >= 9){ num += 9; sum -= 9; if(i < (N - 1)){ num *= 10; } } else{ num += sum; sum = 0; if( i < (N - 1)) num *= 10; } } return num; } int main(){ int sum = 25, N = 3; cout<<"The largest "<<N<<" digit number with sum "<<sum<<" is "<<findLargestNumWithSum(N, sum); return 0; }
Output
The largest 3 digit number with sum 25 is 997
- Related Articles
- Find smallest 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.
- Largest number with prime digits in C++
- Find the Number With Even Sum of Digits using C++
- Find the largest number that can be formed with the given digits in C++
- Number of digits in the nth number made of given four digits in C++
- C++ Program to Sum the digits of a given number
- Find largest number smaller than N with same set of digits in C++
- Minimum number with digits as and 7 only and given sum in C++
- Program to find the sum of all digits of given number in Python
- 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.
- Maximize the given number by replacing a segment of digits with the alternate digits given in C++
- Find sum of digits in factorial of a number in C++
- C Program to sum the digits of a given number in single statement
