🍢 Beginner-Friendly Guide 'Partitioning Into Minimum Number Of Deci-Binary Numbers' - Problem 1689 (C++, Python, JavaScript)

Published: (February 28, 2026 at 09:52 PM EST)
3 min read
Source: Dev.to

Source: Dev.to

Cover image for 🍢 Beginner-Friendly Guide 'Partitioning Into Minimum Number Of Deci-Binary Numbers' - Problem 1689

Problem Summary

You’re given:
A string n representing a very large positive integer.

Your goal:
Find the minimum number of deci-binary numbers (numbers consisting only of digits 0 and 1) that sum up to n.

Intuition

Addition works digit by digit. A deci-binary number can contribute only 0 or 1 to any decimal place.

  • If a digit 7 appears in the units place, you need at least 7 separate deci-binary numbers to provide enough 1s to reach 7.
  • Other digits (e.g., 2 or 3) can be satisfied by using 0s in those positions for some of the numbers.

Thus, the limiting factor is simply the largest digit in the string.
If the string contains a 9, you need 9 deci-binary numbers; if the largest digit is 3, you need 3.

Walkthrough: Understanding the Examples

Example 1: n = 32

  • Digits: 3 (tens) and 2 (units).
  • Need at least three 1s in the tens place → 10 + 10 + 10.
  • Need two 1s in the units place → 11 + 11 + 10 = 32.
  • Total numbers used: 3 (the maximum digit).

Example 2: n = 82734

  • Digits: 8, 2, 7, 3, 4.
  • Largest digit is 8.
  • Must have 8 separate numbers; all other positions can be formed using ≤ 8 1s.
  • Result: 8.

Code Blocks

C++

class Solution {
public:
    int minPartitions(string n) {
        int maxi = 0;
        for (char c : n) {
            // Convert character to integer and track the maximum
            if (maxi  int:
        # The result is simply the maximum digit in the string
        # We convert the characters to integers to find the max
        return int(max(n))

JavaScript

/**
 * @param {string} n
 * @return {number}
 */
var minPartitions = function(n) {
    let maxi = 0;
    for (let i = 0; i  maxi) {
            maxi = digit;
        }
        // Optimization: if we find a 9, we can stop early
        if (maxi === 9) return 9;
    }
    return maxi;
};

Key Takeaways

  • Greedy Logic: The maximum value in a dataset often dictates the requirement for the whole set.
  • String Manipulation: For massive numbers exceeding standard integer limits (10^{100000}), treat the number as a string.
  • Complexity: The solution runs in O(n) time (where n is the string length) and uses O(1) extra space.

Final Thoughts

This problem delivers an “Aha!” moment in technical interviews. It may appear to be a dynamic‑programming or partition problem, but it’s really a test of observation. In real‑world software engineering, this mirrors resource allocation: the total execution time of parallel tasks is often limited by the single longest task, just as our sum is limited by the largest digit.

0 views
Back to Blog

Related posts

Read more »

Reverse array in groups

Problem Statement Reverse an array in groups of a given size k. The array is divided into consecutive chunks windows of length k, and each chunk is reversed in...