Tap here to switch tabs
Problem
Submissions

Problem: Maximize Sum of Squares of Digits

Maximize Sum of Squares of Digits asks you to build a fixed-length positive integer whose digits have a given sum, maximizing squared digits and breaking ties with the largest number.

Statement

You are given two integers num and sum.

A positive integer is defined as good if:

  • It contains exactly num digits without leading zeros.

  • The sum of its digits equals sum.

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

Return the good integer that achieves the maximum score as a string. If there are multiple good integers that achieve the maximum score, return the largest one among them. If no good integer exists, return an empty string "".

Constraints:

  • 1≤1 \leq num ≤2×105\leq 2 \times 10^5

  • 1≤1 \leq sum ≤2×106\leq 2 \times 10^6

⋮
Tap here to switch tabs
Problem
Submissions

Problem: Maximize Sum of Squares of Digits

Maximize Sum of Squares of Digits asks you to build a fixed-length positive integer whose digits have a given sum, maximizing squared digits and breaking ties with the largest number.

Statement

You are given two integers num and sum.

A positive integer is defined as good if:

  • It contains exactly num digits without leading zeros.

  • The sum of its digits equals sum.

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

Return the good integer that achieves the maximum score as a string. If there are multiple good integers that achieve the maximum score, return the largest one among them. If no good integer exists, return an empty string "".

Constraints:

  • 1≤1 \leq num ≤2×105\leq 2 \times 10^5

  • 1≤1 \leq sum ≤2×106\leq 2 \times 10^6