Skip to content
Related Articles

Related Articles

Improve Article

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

  • Last Updated : 28 Jun, 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++




// 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




// 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

Python3




# 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#




// 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

Javascript




<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, prlet -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)

Attention reader! Don’t stop learning now. Get hold of all the important mathematical concepts for competitive programming with the Essential Maths for CP Course at a student-friendly price. To complete your preparation from learning a language to DS Algo and many more,  please refer Complete Interview Preparation Course.




My Personal Notes arrow_drop_up
Recommended Articles
Page :