Form Largest Integer With Digits That Add up to Target - Problem

Given an array of integers cost and an integer target, return the maximum integer you can paint under the following rules:

  • The cost of painting a digit (i + 1) is given by cost[i] (0-indexed).
  • The total cost used must be equal to target.
  • The integer does not have 0 digits.

Since the answer may be very large, return it as a string. If there is no way to paint any integer given the condition, return "0".

Input & Output

Example 1 — Basic Case
$ Input: cost = [4,3,2,5,6,7,2,5,5], target = 7
Output: "772"
💡 Note: We can paint digit 7 twice (cost 2 each) and digit 2 once (cost 3), giving total cost 2+2+3=7. The digits 7,7,2 arranged in descending order give us 772.
Example 2 — No Solution
$ Input: cost = [7,6,5,5,5,6,8,7,8], target = 5
Output: "5"
💡 Note: The minimum cost is 5 (for digits 3, 4, or 5). With target=5, we can afford exactly one digit. The largest digit that costs 5 is digit 5, so the result is "5".
Example 3 — Multiple Digits
$ Input: cost = [2,4,6,2,4,6,4,4,4], target = 5
Output: "0"
💡 Note: Digits 1 and 4 cost 2 each. We could paint two of them for cost 4, leaving 1 remaining cost, but no digit costs 1. We could paint one digit costing 2, leaving 3 remaining, but no digit costs exactly 3. No combination of digits sums to exactly 5, so no solution exists.

Constraints

  • cost.length == 9
  • 1 ≤ cost[i] ≤ 5000
  • 1 ≤ target ≤ 5000

Visualization

Tap to expand
Form Largest Integer With Digits That Add up to Target INPUT cost[] array (digit costs) i=0 4 i=1 3 i=2 2 i=3 5 i=4 6 i=5 7 i=6 2 i=7 5 i=8 5 Digit = index + 1 cost[6]=2 paints digit 7 cost[2]=2 paints digit 3 target = 7 Total cost budget Min cost digits: Digit 3: cost=2 Digit 7: cost=2 (largest!) GREEDY ALGORITHM 1 Find Min Cost Min cost = 2 (digits 3,7) 2 Max Digits Count 7 / 2 = 3 digits max 3 Greedy Selection Pick largest digit first 4 Build Result Form largest number Greedy Process: Remaining: 7, Pick 7 (cost 2) 7 Remaining: 5, Pick 7 (cost 2) 7 Remaining: 3, Pick 3 (cost 3) 3 Remaining: 0. Done! But 77 better: 7+7=14, wait... FINAL RESULT Optimal selection: Cost Calculation Digit 7: cost = 2 Digit 7: cost = 2 ------------------- Total: 4 Why not "773"? 7+7+3 = cost 2+2+3 = 7 773 < 77? No! 773 > 77 Correction: More digits = larger! 773 uses full budget 7 OUTPUT "77" Key Insight: Greedy approach: To maximize the integer, we need (1) maximum number of digits, then (2) largest digits first. First, find the minimum cost to maximize digit count. Then greedily pick the largest digit possible at each position while ensuring remaining budget can fill remaining positions. Here: min cost=2, so max 3 digits. Pick 7,7,3 = "773". TutorialsPoint - Form Largest Integer With Digits That Add up to Target | Greedy Approach
Asked in
Google 15 Amazon 12 Microsoft 8
28.4K Views
Medium Frequency
~35 min Avg. Time
892 Likes
Ln 1, Col 1
Smart Actions
💡 Explanation
AI Ready
💡 Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen