Compute Decimal Representation solution and explanation
8 more videos on this problem
Leetcode Weekly Contest 469 Q1. Compute Decimal RepresentationADevOpsEngineer · 3:42 · 555 views
3697. Compute Decimal Representation (Leetcode Easy)Programming Live with Larry · 3:38 · 427 views
Leetcode Weekly 469 | 3697 | Compute Decimal Representation | With detailed explanation and dry run.Samrat Bhardwaj · 9:58 · 269 views
Weekly Contest 469 🔥 LeetCode 3697 | Compute Decimal Representation | Optimal Solution + Dry Run Study Placement · 8:09 · 151 views
Compute Decimal Representation | Weekly Contest 469 | Java Code | Developer CoderDeveloper Coder · 6:59 · 131 views
3697 Compute Decimal Representation LeetCodeR Sai Siddhu · 3:43 · 127 views
LeetCode 3697 | Compute Decimal Representation [easy]LeetCoders United · 10:33 · 126 views
LeetCode 3697 | Compute Decimal Representation | Java Solution Explained | HindiCode Kage · 7:18 · 50 views
#leetcode Weekly Contest 469,, Q1. Compute Decimal Representation #coding #problemsolving #contestDsa Decoder · 4:09 · 21 views
The problem
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 <= 109
Approach overview
Problem 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.
Solution
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.
class Solution:
def decimalRepresentation(self, n: int) -> List[int]:
ans = []
p = 1
while n:
n, v = divmod(n, 10)
if v:
ans.append(p * v)
p *= 10
ans.reverse()
return ansclass Solution {
public int[] decimalRepresentation(int n) {
List<Integer> parts = new ArrayList<>();
int p = 1;
while (n > 0) {
int v = n % 10;
n /= 10;
if (v != 0) {
parts.add(p * v);
}
p *= 10;
}
Collections.reverse(parts);
int[] ans = new int[parts.size()];
for (int i = 0; i < parts.size(); ++i) {
ans[i] = parts.get(i);
}
return ans;
}
}class Solution {
public:
vector<int> decimalRepresentation(int n) {
vector<int> ans;
long long p = 1;
while (n > 0) {
int v = n % 10;
n /= 10;
if (v != 0) {
ans.push_back(p * v);
}
p *= 10;
}
reverse(ans.begin(), ans.end());
return ans;
}
};function decimalRepresentation(n: number): number[] {
const ans: number[] = [];
let p: number = 1;
while (n) {
const v = n % 10;
n = (n / 10) | 0;
if (v) {
ans.push(p * v);
}
p *= 10;
}
ans.reverse();
return ans;
}func decimalRepresentation(n int) []int {
ans := []int{}
p := 1
for n > 0 {
v := n % 10
n /= 10
if v != 0 {
ans = append(ans, p*v)
}
p *= 10
}
slices.Reverse(ans)
return ans
}Complexity analysis
| 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 |
Frequently asked questions
Is Compute Decimal Representation easy or hard?
Compute Decimal Representation is considered an Easy problem. The logic relies on basic arithmetic operations and simple array handling, making it a common warm‑up question for practicing number manipulation.
Compute Decimal Representation Python/Java solution
In Python or Java, loop while the number is greater than zero, append num % 10 to a list, then update num //= 10 (or num /= 10 in Java integer arithmetic). Reverse the collected digits to produce the final decimal representation.
How to solve Compute Decimal Representation in O(n)?
Treat n as the number of digits. Repeatedly apply modulo 10 to extract the last digit and integer division by 10 to remove it. Store each digit in a list and reverse the list at the end to obtain the correct decimal order, resulting in O(n) time.
What is the best approach for Compute Decimal Representation?
The standard approach uses simulation with division and modulo. Repeatedly compute num % 10 to extract the last digit, store it, then update the number with integer division by 10. This runs in O(d) time where d is the number of digits and uses O(d) space for the result array.
Is Compute Decimal Representation asked at Google/Amazon/Meta?
Digit extraction and number manipulation problems frequently appear in technical interviews at companies like Google, Amazon, and Meta. While this exact problem may vary in wording, the same idea is used in tasks involving number reversal, palindrome checks, and base conversion.
What data structure is used in Compute Decimal Representation?
An array or dynamic list is typically used to store the digits as they are extracted. Arithmetic operations like modulo and integer division handle the math part, while the array maintains the final ordered sequence of digits.
What is the time complexity of Compute Decimal Representation?
Time complexity is O(d), where d represents the number of digits in the input number. Each loop iteration extracts one digit using modulo and division operations. Space complexity is also O(d) because the algorithm stores all digits in an array or list.
