Logo
IconChallenge
Icon
IconProblem
IconSolutions
IconSubmissions

Maximize Sum of Squares of Digits

XPChallenge Points: 20
levelLevel: Medium

You are given two positive integers num and sum. A positive integer n is good if:

  • It has exactly num digits (no leading zero).

  • The sum of its digits is exactly sum.

The score of n is the sum of the squares of its digits.
Return the string of the good integer that maximizes the score. If multiple solutions achieve the same score, return the lexicographically largest one. If no such integer exists, return an empty string "".

Example 1:

Input: num = 3, sum = 20

Output: "992"

Explanation:

Maximize squares by packing 9s: 9+9+2=20 ⇒ "992".

Example 2:

Input: num = 2, sum = 17

Output: 98

Explanation:

Good integers: 89, 98. Both have score 8^2+9^2=145. Lexicographically larger is "98".

to Continue
like
dislike

Accepted:

Submission:

IconReport an issue
Icon
IconCode
IconYou need toto run or submitYou need toto run or submit
IconTest Case
IconTest Result