Open In App

Maximum number made up of distinct digits whose sum is equal to N

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:

Follow the steps below to solve the problem:

Below is the implementation of the above approach:




// C++ program for the above approach
 
#include <bits/stdc++.h>
using namespace std;
 
// Function to find the largest positive
// number made up of distinct digits
// having the sum of its digits as N
long largestNumber(int N)
{
    // If given number is greater
    // than 45, print -1
    if (N > 45)
        return -1;
 
    // Store the required number and
    // the digit to be considered
    int num = 0, digit = 9;
 
    // Loop until N > 0 and digit > 0
    while (N > 0 && digit > 0) {
 
        // If the current digit is
        // at most N then, add it
        // to number num
        if (digit <= N) {
 
            // Update the value of
            // num
            num *= 10;
            num += digit;
 
            // Decrement N by digit
            N -= digit;
        }
 
        // Consider the next lower
        // digit
        digit -= 1;
    }
 
    // Add 0 at the end and return
    // the number num
    return num * 10;
}
 
// Driver Code
int main()
{
    int N = 25;
    cout << largestNumber(N);
 
    return 0;
}




// Java program for the above approach
import java.io.*;
import java.util.*;
 
class GFG{
 
// Function to find the largest positive
// number made up of distinct digits
// having the sum of its digits as N
static long largestNumber(int N)
{
     
    // If given number is greater
    // than 45, print -1
    if (N > 45)
        return -1;
 
    // Store the required number and
    // the digit to be considered
    int num = 0, digit = 9;
 
    // Loop until N > 0 and digit > 0
    while (N > 0 && digit > 0)
    {
         
        // If the current digit is
        // at most N then, add it
        // to number num
        if (digit <= N)
        {
             
            // Update the value of
            // num
            num *= 10;
            num += digit;
 
            // Decrement N by digit
            N -= digit;
        }
 
        // Consider the next lower
        // digit
        digit -= 1;
    }
 
    // Add 0 at the end and return
    // the number num
    return num * 10;
}
 
// Driver Code
public static void main (String[] args)
{
    int N = 25;
     
    System.out.print(largestNumber(N));
}
}
 
// This code is contributed by sanjoy_62




# Python program for the above approach
 
# Function to find the largest positive
# number made up of distinct digits
# having the sum of its digits as N
def largestNumber(N):
     
    # If given number is greater
    # than 45, print -1
    if (N > 45):
        return -1
     
    # Store the required number and
    # the digit to be considered
    num = 0
    digit = 9
     
    # Loop until N > 0 and digit > 0
    while (N > 0 and digit > 0):
         
        # If the current digit is
        # at most N then, add it
        # to number num
        if (digit <= N):
             
            # Update the value of
            # num
            num *= 10
            num += digit
             
            # Decrement N by digit
            N -= digit
         
        # Consider the next lower
        # digit
        digit -= 1
     
    # Add 0 at the end and return
    # the number num
    return num * 10
 
# Driver Code
N = 25
print(largestNumber(N))
 
# This code is contributed by avanitrachhadiya2155




// C# program for the above approach
using System;
 
class GFG{
     
// Function to find the largest positive
// number made up of distinct digits
// having the sum of its digits as N
static long largestNumber(int N)
{
     
    // If given number is greater
    // than 45, print -1
    if (N > 45)
        return -1;
 
    // Store the required number and
    // the digit to be considered
    int num = 0, digit = 9;
 
    // Loop until N > 0 and digit > 0
    while (N > 0 && digit > 0)
    {
         
        // If the current digit is
        // at most N then, add it
        // to number num
        if (digit <= N)
        {
             
            // Update the value of
            // num
            num *= 10;
            num += digit;
 
            // Decrement N by digit
            N -= digit;
        }
 
        // Consider the next lower
        // digit
        digit -= 1;
    }
 
    // Add 0 at the end and return
    // the number num
    return num * 10;
}
 
// Driver Code
public static void Main()
{
    int N = 25;
     
    Console.Write(largestNumber(N));
}
}
 
// This code is contributed by ukasp




<script>
 
// JavaScript program for the above approach
 
// Function to find the largest positive
// number made up of distinct digits
// having the sum of its digits as N
function largestNumber(N)
{
     
    // If given number is greater
    // than 45, print -1
    if (N > 45)
        return -1;
 
    // Store the required number and
    // the digit to be considered
    let num = 0, digit = 9;
 
    // Loop until N > 0 and digit > 0
    while (N > 0 && digit > 0)
    {
         
        // If the current digit is
        // at most N then, add it
        // to number num
        if (digit <= N)
        {
             
            // Update the value of
            // num
            num *= 10;
            num += digit;
 
            // Decrement N by digit
            N -= digit;
        }
 
        // Consider the next lower
        // digit
        digit -= 1;
    }
 
    // Add 0 at the end and return
    // the number num
    return num * 10;
}
 
// Driver Code
     let N = 25;
     
    document.write(largestNumber(N));
 
</script>

Output: 
98710

 

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


Article Tags :