LeetCode Problem

How to Solve Sum of Numbers With Units Digit K

This problem requires calculating the minimum number of integers with a specific units digit that sum to num. We can leverage state transition dynamic programming to explore combinations efficiently, checking sums recursively or iteratively while minimizing set size. If no valid combination exists, we return -1, handling zero as a special empty set case.

GhostInterview Help

Need help with Sum of Numbers With Units Digit K without spending extra time grinding it?

GhostInterview can read Sum of Numbers With Units Digit K from a screenshot, generate the answer path, explain the complexity, and support solver-first interview workflows when you need direct help fast.

Screenshot Input

Capture the prompt fast instead of rewriting the problem by hand.

Answer + Complexity

Get the solution path, trade-offs, and complexity summary in one pass.

Stealth Workflow

Stay outside captured layers on supported screen-share workflows.

Problem #2310State transition dynamic programmingReviewed 2026-03-08
Difficulty
Medium
Primary pattern
State transition dynamic programming
Answer-first problem summary
Step-by-step approach and complexity
GhostInterview solver workflow

This problem requires calculating the minimum number of integers with a specific units digit that sum to num. We can leverage state transition dynamic programming to explore combinations efficiently, checking sums recursively or iteratively while minimizing set size. If no valid combination exists, we return -1, handling zero as a special empty set case.

Problem Statement

Given two integers num and k, find the smallest number of positive integers such that each integer ends with the digit k and their sum equals num. If no such set exists, return -1. This problem emphasizes a state transition dynamic programming approach where each partial sum depends on previous smaller sums.

For example, num = 58 and k = 9 allows sets like [9,49] or [19,39] with sum 58. Constraints: 0 <= num <= 3000, 0 <= k <= 9. Consider zero as a valid sum for an empty set. The solution involves systematically checking integer combinations while minimizing set size.

Examples

Example 1

Input: num = 58, k = 9

Output: 2

One valid set is [9,49], as the sum is 58 and each integer has a units digit of 9. Another valid set is [19,39]. It can be shown that 2 is the minimum possible size of a valid set.

Example 2

Input: num = 37, k = 2

Output: -1

It is not possible to obtain a sum of 37 using only integers that have a units digit of 2.

Example 3

Input: num = 0, k = 7

Output: 0

The sum of an empty set is considered 0.

Constraints

  • 0 <= num <= 3000
  • 0 <= k <= 9

Solution Approach

Dynamic Programming Table

Construct a DP table where dp[s] stores the minimum count of integers with units digit k summing to s. Update dp iteratively for each integer candidate ending with k, ensuring the smallest set is tracked.

Greedy and Enumeration Shortcut

For small values of num and k, we can try adding multiples of k while checking the remainder modulo 10. This can quickly identify minimal sets without full DP, but may fail for edge cases where larger combinations are needed.

Recursive State Transition

Define a recursive function taking remaining sum and current count. At each step, subtract a number ending with k and recurse. Memoization prevents recomputation. Base cases handle exact sum, overreach, or zero sum.

Complexity Analysis

MetricValue
TimeDepends on the final approach
SpaceDepends on the final approach

Time complexity ranges from O(num/k) in greedy checks to O(num) in DP. Space complexity depends on memoization or DP table size, which is O(num). Recursive approaches may use stack space up to O(num/k).

What Interviewers Usually Probe

  • Candidate quickly identifies units digit pattern for recursion or DP.
  • Candidate evaluates minimal set size instead of arbitrary sums.
  • Candidate handles edge cases: zero sum and impossible sums correctly.

Common Pitfalls or Variants

Common pitfalls

  • Ignoring sums where multiple combinations yield smaller counts.
  • Failing to handle num = 0 as an empty set.
  • Misapplying greedy without checking all remainder possibilities, leading to wrong -1 results.

Follow-up variants

  • Find minimum set with digits ending in k summing to a target multiple of 10.
  • Allow negative integers with units digit k and sum to num.
  • Count all possible sets meeting the units digit condition instead of minimum size.

How GhostInterview Helps

  • GhostInterview identifies edge cases where greedy fails and DP is required.
  • Provides recursive state transition templates to minimize set size efficiently.
  • Highlights failure modes such as impossible sums or zero sum handling and suggests correct returns.

Topic Pages

FAQ

What is the main pattern used in Sum of Numbers With Units Digit K?

The main pattern is state transition dynamic programming, where each sum depends on previous smaller sums ending with digit k.

Can we solve this problem using only greedy?

Greedy works for simple cases but may fail for combinations requiring multiple integers; DP or recursion ensures correctness.

How do we handle num = 0 in this problem?

An empty set is considered valid, so the minimum set size is 0 when num = 0.

What is the time complexity of the DP solution?

Time complexity is O(num) for DP table updates; recursive memoized solutions are similar but include stack overhead.

How does GhostInterview help optimize the solution?

GhostInterview guides identifying minimal sets, avoiding recomputation with memoization, and handling edge cases like impossible sums or empty sets.

GhostInterview Solver

Need direct help with Sum of Numbers With Units Digit K instead of spending more time grinding it?

Download GhostInterview when you want a LeetCode solver, not another long practice loop. Capture Sum of Numbers With Units Digit K from a screenshot, get the answer path and complexity, and use supported stealth workflows that stay outside captured layers.

Screenshot Input

Capture the prompt fast instead of rewriting the problem by hand.

Answer + Complexity

Get the solution path, trade-offs, and complexity summary in one pass.

Stealth Workflow

Stay outside captured layers on supported screen-share workflows.