You are given a positive integer n.
A positive integer is a base-10 component if it is the product of a single digit from 1 to 9 and a non-negative power of 10. For example, 500, 30, and 7 are base-10 components, while 537, 102, and 11 are not.
Express n as a sum of only base-10 components, using the fewest base-10 components possible.
Return an array containing these base-10 components in descending order.
Example 1:
Input: n = 537
Output: [500,30,7]
Explanation:
We can express 537 as 500 + 30 + 7. It is impossible to express 537 as a sum using fewer than 3 base-10 components.
Example 2:
Input: n = 102
Output: [100,2]
Explanation:
We can express 102 as 100 + 2. 102 is not a base-10 component, which means 2 base-10 components are needed.
Example 3:
Input: n = 6
Output: [6]
Explanation:
6 is a base-10 component.
Constraints:
1 <= n <= 109Problem Overview: Given an integer, compute its decimal representation as a sequence of digits. Instead of relying on built-in string conversion, you explicitly derive each digit using basic arithmetic operations.
Approach 1: Simulation with Division and Modulo (O(d) time, O(d) space)
The straightforward method simulates how humans extract digits from a number. Repeatedly apply num % 10 to get the least significant digit, append it to a result container, then update the number using num // 10. Continue until the value becomes zero. Because digits are collected from least significant to most significant, reverse the result at the end to restore the correct order.
This method directly models decimal decomposition. Each iteration isolates one digit, so the loop runs once per digit in the number. The number of iterations is proportional to the digit count d, giving O(d) time complexity. The output array stores all digits, so space complexity is also O(d). The approach relies only on arithmetic operations and a dynamic array, which makes it predictable and language‑agnostic.
This technique appears frequently in problems involving manual number processing, base conversions, and digit manipulation. Understanding it helps with tasks like reversing numbers, checking palindromes, or implementing custom numeric formats using math operations and array storage.
Recommended for interviews: The simulation approach is exactly what interviewers expect. It demonstrates that you understand how decimal numbers are constructed from digits using division and modulo. Even though many languages provide built‑in conversions, implementing the digit extraction manually shows control over fundamental math operations and array manipulation.
We can repeatedly perform modulo and division operations on n. Each modulo result multiplied by the current position value p represents a decimal component. If the modulo result is not 0, we add this component to our answer. Then we multiply p by 10 and continue processing the next position.
Finally, we reverse the answer to arrange it in descending order.
The time complexity is O(log n), where n is the input positive integer. The space complexity is O(log n) for storing the answer.
Python
Java
C++
Go
TypeScript
| Approach | Time | Space | When to Use |
|---|---|---|---|
| Simulation using division and modulo | O(d) | O(d) | General case when manually extracting digits of a number |
| String conversion then iterate | O(d) | O(d) | Quick implementation when language conversion utilities are allowed |
3697. Compute Decimal Representation (Leetcode Easy) • Programming Live with Larry • 421 views views
Watch 5 more video solutions →Practice Compute Decimal Representation with our built-in code editor and test cases.
Practice on FleetCode