Largest with given number of digits and sum (original) (raw)

Last Updated : 14 Apr, 2026

Given two numbers **n and **s , find the **largest number that can be formed with n digits and whose sum of digits should be equals to s. Return -1 if it is not possible.

**Examples:

**Input: s = 9, d = 2
**Output: 90
**Explanation : 90 is the largest number with 2 digits and digit sum as 9

**Input: s = 20, d = 3
**Output: 992

Try It Yourselfredirect icon

Table of Content

**[Naive Approach] Brute Approach

#include #include using namespace std;

string findLargest(int n, int s) {

// Base case
if (n == 0)
    return (s == 0) ? "" : "-1";

// Try digits from 9 to 0
for (int digit = 9; digit >= 0; digit--) {
    
    // If remaining sum is valid
    if (s - digit >= 0) {
        string res = findLargest(n - 1, s - digit);
        
         // If valid result found return it
        if (res != "-1")
            return to_string(digit) + res;
    }
}
return "-1";

}

int main() {

int n=2, s=9;
string result = findLargest(n, s);
cout << result << endl;

return 0;

}

Java

class GfG {

static String findLargest(int n, int s) {

    // Base case: if digits are filled
    if (n == 0)
        return (s == 0) ? "" : "-1";

    // Try placing each digit from 9 to 0
    for (int digit = 9; digit >= 0; digit--) {

        // If remaining sum is valid
        if (s - digit >= 0) {
            String res = findLargest(n - 1, s - digit);

            // If valid result found return it
            if (!res.equals("-1"))
                return digit + res;
        }
    }
    return "-1";
}

public static void main(String[] args) {
    int n = 2, s = 9;
    System.out.println(findLargest(n, s));
}

}

Python

def findLargest(n, s):

# Base case: if digits are filled
if n == 0:
    return "" if s == 0 else "-1"

# Try placing each digit from 9 to 0
for digit in range(9, -1, -1):

    # If remaining sum is valid
    if s - digit >= 0:
        res = findLargest(n - 1, s - digit)

        # If valid result found return it
        if res != "-1":
            return str(digit) + res

return "-1"

n, s = 2, 9 print(findLargest(n, s))

C#

using System; class GfG {

static string findLargest(int n, int s) {

    // Base case: if digits are filled
    if (n == 0)
        return (s == 0) ? "" : "-1";

    // Try placing each digit from 9 to 0
    for (int digit = 9; digit >= 0; digit--) {

        // If remaining sum is valid
        if (s - digit >= 0) {
            string res = findLargest(n - 1, s - digit);

            // If valid result found return it
            if (res != "-1")
                return digit + res;
        }
    }
    return "-1";
}

static void Main() {
    int n = 2, s = 9;
    Console.WriteLine(findLargest(n, s));
}

}

JavaScript

function findLargest(n, s) {

// Base case: if digits are filled
if (n == 0)
    return (s == 0) ? "" : "-1";

// Try placing each digit from 9 to 0
for (let digit = 9; digit >= 0; digit--) {

    // If remaining sum is valid
    if (s - digit >= 0) {
        let res = findLargest(n - 1, s - digit);

        // If valid result found return it
        if (res != "-1")
            return digit + res;
    }
}
return "-1";

}

// driver code let n = 2, s = 9; console.log(findLargest(n, s));

`

**Time Complexity: O(10^m) - at each of the m positions, we try 10 digits recursively.
**Space Complexity: O(m) - recursion stack depth is at most m.

[Expected Approach] Greedy Approach - O(m) Time and O(m) Space

C++ `

#include #include using namespace std;

string findLargest(int n, int s) {

// If sum is 0, only possible if single digit
if (s == 0)
    return (n == 1) ? "0" : "-1";

// Sum greater than maximum possible sum
if (s > 9 * n)
    return "-1";

string result = "";

// Fill digits from left to right
for (int i = 0; i < n; i++) {

    // Place 9 if remaining sum is >= 9
    if (s >= 9) {
        result += '9';
        s -= 9;
    }
    // Place remaining sum and fill rest with 0s
    else {
        result += to_string(s);
        s = 0;

        // Fill remaining positions with 0
        if (i < n - 1) {
            result += string(n - i - 1, '0');
            break;
        }
    }
}
return result;

}

int main() {

int n = 2, s = 9;
cout << findLargest(n, s);
return 0;

}

Java

import java.util.*; class GfG {

static String findLargest(int n, int s) {

    // If sum is 0, only possible if single digit
    if (s == 0)
        return (n == 1) ? "0" : "-1";

    // Sum greater than maximum possible sum
    if (s > 9 * n)
        return "-1";

    String result = "";

    // Fill digits from left to right
    for (int i = 0; i < n; i++) {

        // Place 9 if remaining sum is >= 9
        if (s >= 9) {
            result += '9';
            s -= 9;
        }
        // Place remaining sum and fill rest with 0s
        else {
            result += s;
            s = 0;

            // Fill remaining positions with 0
            if (i < n - 1) {
                for (int j = i + 1; j < n; j++)
                    result += '0';
                break;
            }
        }
    }
    return result;
}

public static void main(String[] args) {
    int n = 2, s = 9;
    System.out.println(findLargest(n, s));
}

}

Python

def findLargest(n, s):

# If sum is 0, only possible if single digit
if s == 0:
    return "0" if n == 1 else "-1"

# Sum greater than maximum possible sum
if s > 9 * n:
    return "-1"

result = ""

# Fill digits from left to right
for i in range(n):

    # Place 9 if remaining sum is >= 9
    if s >= 9:
        result += '9'
        s -= 9
    # Place remaining sum and fill rest with 0s
    else:
        result += str(s)
        s = 0

        # Fill remaining positions with 0
        if i < n - 1:
            result += '0' * (n - i - 1)
            break

return result

n, s = 2, 9 print(findLargest(n, s))

C#

using System; class GfG {

static string findLargest(int n, int s) {

    // If sum is 0, only possible if single digit
    if (s == 0)
        return (n == 1) ? "0" : "-1";

    // Sum greater than maximum possible sum
    if (s > 9 * n)
        return "-1";

    string result = "";

    // Fill digits from left to right
    for (int i = 0; i < n; i++) {

        // Place 9 if remaining sum is >= 9
        if (s >= 9) {
            result += '9';
            s -= 9;
        }
        // Place remaining sum and fill rest with 0s
        else {
            result += s;
            s = 0;

            // Fill remaining positions with 0
            if (i < n - 1) {
                result += new string('0', n - i - 1);
                break;
            }
        }
    }
    return result;
}

static void Main() {
    int n = 2, s = 9;
    Console.WriteLine(findLargest(n, s));
}

}

JavaScript

function findLargest(n, s) {

// If sum is 0, only possible if single digit
if (s == 0)
    return n == 1 ? "0" : "-1";

// Sum greater than maximum possible sum
if (s > 9 * n)
    return "-1";

let result = "";

// Fill digits from left to right
for (let i = 0; i < n; i++) {

    // Place 9 if remaining sum is >= 9
    if (s >= 9) {
        result += '9';
        s -= 9;
    }
    // Place remaining sum and fill rest with 0s
    else {
        result += s;
        s = 0;

        // Fill remaining positions with 0
        if (i < n - 1) {
            result += '0'.repeat(n - i - 1);
            break;
        }
    }
}
return result;

}

// driver code let n = 2, s = 9; console.log(findLargest(n, s));

`