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
Table of Content
**[Naive Approach] Brute Approach
- Try digits from 9 to 0 at each position recursively
- Place a digit only if remaining sum is non-negative
- Reduce n and s and call recursion
- If n becomes 0 and s is 0 return empty string, otherwise return -1
- On getting a valid result append digit in front and return immediately
- If no valid number is found return -1 C++ `
#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
- If sum is 0, return 0 only if n is 1, else return -1.
- If sum exceeds 9*n, return "-1" since no such number is possible.
- Fill digits from left to right, place 9 if remaining sum is greater than or equal to 9 and subtract 9 from sum.
- If remaining sum is less than 9, place the remaining sum at current position and fill all remaining positions with 0.
- Return the resulting string as the largest number.

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 resultn, 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));
`