Largest Number With Digit Sum
MediumAsked in:Amazon•Stage:Online Assessment
Problem Statement
Given two integers n and s, construct the largest possible n‑digit integer such that the sum of its digits equals s. If no such integer exists, output -1. The integer must not contain leading zeros unless n equals 1.
Input Format
Two space‑separated integers n and s, where n is the number of digits and s is the required digit sum.
Output Format
Print the largest n‑digit number whose digits sum to s, or -1 if it cannot be formed.
Constraints
- 1 <= n <= 10^5
- 0 <= s <= 9*n
- The most significant digit cannot be zero unless n=1