Maximum number made up of distinct digits whose sum is equal to N
Last Updated :
23 Nov, 2021
Given a positive integer N, the task is to find the largest positive number made up of distinct digits having the sum of its digits equal to N. If no such number exists, print “-1”.
Examples:
Input: N = 25
Output: 98710
Explanation:
The number 98710 is the largest number that contains only unique digits and the sum of its digits (9 + 8 + 7 + 1 + 0) is N(= 25).
Input: N = 50
Output: -1
Approach: The given problem can be solved based on the following observations:
- If the value of N is at least 45: The required result is -1 as the largest number that can be made using non-repeating digits is 9876543210, whose sum of digits is 45.
- For all other values of N: To get the largest number, start from the largest digit i.e., 9, and keep decrementing it and adding it to the required number. The required number must contain a 0 at the end of it as it increases the value without changing the sum of digits.
Follow the steps below to solve the problem:
- If the given number N is greater than 45, then print “-1”.
- Otherwise, perform the following steps:
- Initialize a variable, say num as 0 to store the required result, and a variable, say digit, as 9.
- Iterate a loop until the values of N and digit are positive and perform the following steps:
- If the value of a digit is at most N, then multiply num by 10 and then add the value of digit to num and decrement the value of N by digit.
- Also, decrement the value of the digit by 1.
- Multiply the variable num by 10 and append digit 0 at the end.
- After completing the above steps, print the value of num as the resultant number.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
long largestNumber( int N)
{
if (N > 45)
return -1;
int num = 0, digit = 9;
while (N > 0 && digit > 0) {
if (digit <= N) {
num *= 10;
num += digit;
N -= digit;
}
digit -= 1;
}
return num * 10;
}
int main()
{
int N = 25;
cout << largestNumber(N);
return 0;
}
|
Java
import java.io.*;
import java.util.*;
class GFG{
static long largestNumber( int N)
{
if (N > 45 )
return - 1 ;
int num = 0 , digit = 9 ;
while (N > 0 && digit > 0 )
{
if (digit <= N)
{
num *= 10 ;
num += digit;
N -= digit;
}
digit -= 1 ;
}
return num * 10 ;
}
public static void main (String[] args)
{
int N = 25 ;
System.out.print(largestNumber(N));
}
}
|
Python3
def largestNumber(N):
if (N > 45 ):
return - 1
num = 0
digit = 9
while (N > 0 and digit > 0 ):
if (digit < = N):
num * = 10
num + = digit
N - = digit
digit - = 1
return num * 10
N = 25
print (largestNumber(N))
|
C#
using System;
class GFG{
static long largestNumber( int N)
{
if (N > 45)
return -1;
int num = 0, digit = 9;
while (N > 0 && digit > 0)
{
if (digit <= N)
{
num *= 10;
num += digit;
N -= digit;
}
digit -= 1;
}
return num * 10;
}
public static void Main()
{
int N = 25;
Console.Write(largestNumber(N));
}
}
|
Javascript
<script>
function largestNumber(N)
{
if (N > 45)
return -1;
let num = 0, digit = 9;
while (N > 0 && digit > 0)
{
if (digit <= N)
{
num *= 10;
num += digit;
N -= digit;
}
digit -= 1;
}
return num * 10;
}
let N = 25;
document.write(largestNumber(N));
</script>
|
Time Complexity: O(1)
Auxiliary Space: O(1)
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...