Mid/SeniorGreedyMath

Maximize Sum of Squares of Digits

You are given two positive integers num and sum.

A positive integer n is good if it satisfies both of the following:

  • The number of digits in n is exactly num.
  • The sum of digits in n is exactly sum.

The score of a good integer n is the sum of the squares of digits in n.

Return a string denoting the good integer n that achieves the maximum score. If there are multiple possible integers, return the maximum one. If no such integer exists, return an empty string.

Example 1
Inputnum = 2, sum = 3
Output"30"
Among the good integers 12, 21, and 30, 30 has the maximum score of 9.
Example 2
Inputnum = 2, sum = 17
Output"98"
Both 89 and 98 have the maximum score of 145, and 98 is the maximum such good integer.

Constraints

  • 1 <= num <= 2 * 10^5
  • 1 <= sum <= 2 * 10^6

Asked at 1 companies

</>

Your Solution

(Ctrl/Cmd + Enter)

Switching Language

Loading template...

Loading...

Sign in to save your progress

AI code evaluation

Get a correctness verdict, missed edge cases, and complexity analysis of your solution.

Sign in to evaluate