We are given a string consisting of digits, we may group these digits into sub-groups (but maintaining their original order). The task is to count number of groupings such that for every sub-group except the last one, sum of digits in a sub-group is less than or equal to sum of the digits in the sub-group immediately on its right.
For example, a valid grouping of digits of number 1119 is (1-11-9). Sum of digits in first subgroup is 1, next subgroup is 2, and last subgroup is 9. Sum of every subgroup is less than or equal to its immediate right.
Input : "1119" Output: 7 Sub-groups: [1-119], [1-1-19], [1-11-9], [1-1-1-9], [11-19] and [111-9]. Note : Here we have included  in the group and sum of digits is 12 and this group has no immediate right. Input : "1234" Output: 6 Sub-groups : , [1-234], [12-34], [1-2-3-4], [12-3-4] and [1-2-34]
Let “length” be the length of input number. A recursive solution is to consider every position from 0 length-1. For every position, recursively count all possible subgroups after it. Below is C++ implementation of naive recursive solution.
If we take a closer look at above recursive solution, we notice that there may be overlapping subproblems. For example, if input number is 12345, then for position = 3 and previous_sum = 3, we recur two times. Similarly for position 4 and previous_sum = 7, we recur two times. Therefore the above solution can be optimized using Dynamic Programming. Below is a Dynamic Programming based solution for this problem.
- The maximum sum of digits can be 9*length where ‘length’ is length of input num.
- Create a 2D array int dp[MAX][9*MAX] where MAX is maximum possible length of input numebr. A value dp[position][previous] is going to store result for ‘position’ and ‘previous_sum’.
- If current subproblem has been evaluated i.e; dp[position][previous_sum] != -1, then use this result, else recursively compute its value.
- If by including the current position digit in sum i.e; sum = sum + num[position]-‘0’, sum becomes greater than equal to previous sum, then increment the result and call the problem for next position in the num.
- If position == length, then we have been traversed current subgroup successfully and we return 1;
Below is implementation of above algorithm.
This article is contributed by Shashank Mishra ( Gullu ). This article is reviewed by team GeeksForGeeks.
Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.
- Number of permutation with K inversions
- Longest repeating and non-overlapping substring
- Find minimum sum such that one of every three consecutive elements is taken
- Count Distinct Subsequences
- Non-crossing lines to connect points in a circle
- Minimum cost to fill given weight in a bag
- A Space Optimized DP solution for 0-1 Knapsack Problem
- Find number of times a string occurs as a subsequence in given string
- Minimum Cost To Make Two Strings Identical
- Find Jobs involved in Weighted Job Scheduling
- Find minimum adjustment cost of an array
- Dynamic Programming | High-effort vs. Low-effort Tasks Problem
- Count all possible walks from a source to a destination with exactly k edges
- Count number of binary strings without consecutive 1's
- Maximum Product Cutting | DP-36