leetcode

Compute Decimal Representation - Link

Question Description

You are given an integer n. Your task is to break it down into its decimal components and return them as an array.

For example:

Return the decimal representation as an array of integers, excluding zeros.


Constraints


Approach

Extract each non-zero digit and multiply it by its place value, starting from the units place.

Algorithm:

  1. Initialize an empty list to store decimal components
  2. Initialize place = 1 (units place)
  3. While n > 0:
    • Extract the last digit: digit = n % 10
    • If digit is not 0, compute digit * place and add to list
    • Remove the last digit: n = n / 10
    • Move to next place value: place = place * 10
  4. Reverse the list (since we processed from least significant to most significant)
  5. Convert list to array and return

This approach works because:


Dry Run

Example 1: n = 5030

Step-by-step execution:

List before reverse: [30, 5000]
List after reverse: [5000, 30]

Result: [5000, 30]

Example 2: n = 4321

Step-by-step execution:

List before reverse: [1, 20, 300, 4000]
List after reverse: [4000, 300, 20, 1]

Result: [4000, 300, 20, 1]

Example 3: n = 7

Step-by-step execution:

List before reverse: [7]
List after reverse: [7]

Result: [7]


Solution

class Solution {
    public int[] decimalRepresentation(int n) {
        List<Integer> base = new ArrayList<>();
        int place = 1;
        while (n > 0) {
            int digit = n % 10;
            if (digit != 0) {
                base.add(digit * place);
            }
            n /= 10;
            place *= 10;
        }
        Collections.reverse(base);
        int[] list = new int[base.size()];
        for (int i = 0; i < base.size(); i++) {
            list[i] = base.get(i);
        }
        return list;
    }
}

Time and Space Complexity


Alternative Approaches

String-Based Approach

class Solution {
    public int[] decimalRepresentation(int n) {
        String s = String.valueOf(n);
        List<Integer> result = new ArrayList<>();

        for (int i = s.length() - 1; i >= 0; i--) {
            int digit = s.charAt(i) - '0';
            if (digit != 0) {
                int placeValue = digit * (int) Math.pow(10, s.length() - 1 - i);
                result.add(placeValue);
            }
        }

        return result.stream().mapToInt(Integer::intValue).toArray();
    }
}

Explanation: Convert to string and calculate place values using powers of 10.

Time Complexity: O(log₁₀ n) - string conversion and processing Space Complexity: O(log₁₀ n) - for string and result list

Mathematical Approach with Place Tracking

class Solution {
    public int[] decimalRepresentation(int n) {
        List<Integer> result = new ArrayList<>();
        int multiplier = 1;

        while (n > 0) {
            int remainder = n % 10;
            if (remainder != 0) {
                result.add(remainder * multiplier);
            }
            n = n / 10;
            multiplier = multiplier * 10;
        }

        // Reverse the list
        Collections.reverse(result);

        return result.stream().mapToInt(Integer::intValue).toArray();
    }
}

Explanation: Same logic but with different variable names for clarity.

Time Complexity: O(log₁₀ n) - digit processing Space Complexity: O(log₁₀ n) - result storage

Recursive Approach

class Solution {
    private List<Integer> result = new ArrayList<>();

    public int[] decimalRepresentation(int n) {
        helper(n, 1);
        Collections.reverse(result);
        return result.stream().mapToInt(Integer::intValue).toArray();
    }

    private void helper(int n, int place) {
        if (n == 0) return;

        int digit = n % 10;
        if (digit != 0) {
            result.add(digit * place);
        }

        helper(n / 10, place * 10);
    }
}

Explanation: Use recursion to process digits from least significant to most significant.

Time Complexity: O(log₁₀ n) - recursive calls for each digit Space Complexity: O(log₁₀ n) - recursion stack + result list


Key Insights

Place Value System:

Why Skip Zeros:

Edge Cases:

Applications: