Given a numeric string S representing a positive decimal integer, the task is to find the minimum number of positive Deci-Binary numbers required to obtain the sum S.
Deci-Binary Numbers: Decimal numbers consisting of only 0s and 1s as its digits.
Examples:
Input: S = “31”
Output: 3
Explanation: S can be represented as the sum of minimum of 3 Deci-Binary numbers {10, 10, 11}.Input: S = “82734”
Output: 8
Explanation: S can be represented as sum minimum of 8 Deci-Binary numbers {11111, 11111, 10111, 10101, 10100, 10100, 10100, 10000}.
Approach: The given problem can be solved based on the following observations:
Suppose X Deci-Binary numbers are needed to obtain the sum S. To make the sum of X Deci-Binary numbers at i-th place equal to a digit d in S, there must be exactly d Deci-Binary numbers among X numbers having 1 at the ith position.
Therefore, the minimum number of Deci-Binary numbers required to obtain a sum S is equal to the maximum value of any of the digits of S.
Therefore, to solve the problem, iterate over the characters of the string S and find the maximum digit present in it.
Below is the implementation of the above approach:
C++
// C++ Program to implemeent // the above approach #include <bits/stdc++.h> using namespace std; // Function to find the count of minimum // Deci-Binary numbers required to obtain S int minimum_deci_binary_number(string s) { // Stores the minimum count int m = INT_MIN; // Iterate over the string s for (int i = 0; i < s.size(); i++) { // Convert the char to its // equivalent integer int temp = s[i] - '0'; // If current character is // the maximum so far if (temp > m) { // Update the maximum digit m = temp; } } // Print the required result return m; } // Driver Code int main() { string S = "31"; cout << minimum_deci_binary_number(S); return 0; } |
3
Time Complexity: O(N)
Auxiliary Space: O(N)
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.
Recommended Posts:
- Minimum count of digits required to obtain given Sum
- Minimum number of steps required to obtain the given Array by the given operations
- Minimum number of operations required to obtain a given Binary String
- Minimum count of elements required to obtain the given Array by repeated mirror operations
- Minimum number of Appends of X or Y characters from the end to the front required to obtain given string
- Minimum increments by index value required to obtain at least two equal Array elements
- Minimum circular rotations to obtain a given numeric string by avoiding a set of given strings
- Minimize operations required to obtain N
- Minimize Steps required to obtain Sorted Order of an Array
- Count of decrement operations required to obtain K in N steps
- Number of ways to obtain each numbers in range [1, b+c] by adding any two numbers in range [a, b] and [b, c]
- Count of Ways to obtain given Sum from the given Array elements
- Minimum Steps to obtain N from 1 by the given operations
- Sum of indices of Characters removed to obtain an Empty String based on given conditions
- Count ways to obtain given sum by repeated throws of a dice
- Count of minimum reductions required to get the required sum K
- Find K that requires minimum increments or decrements of array elements to obtain a sequence of increasing powers of K
- Minimum count of numbers required ending with 7 to sum as a given number
- Minimize insertions in an array to obtain all sums upto N
- Maximize cost of deletions to obtain string having no pair of similar adjacent characters
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.

