Given infinte number of elements of form
and
( n >= 0 ). The task is to find the minimum count of elements chosen such that there sum is equal to K.
Examples:
Input : K = 48
Output : 6
elements chosen are: (1 + 1 + 1 + 10 + 10 + 25)Input : 69
Output : 9
elements chosen are: (1 + 1 + 1 + 1 + 10 + 10 + 10 + 10 + 25)
Approach:
There are infinite number of the following elements :
1, 10, 25, 100, 1000, 2500, 10000, 100000, 250000 … and so on.
Greedy Approach won’t work here. For K = 66, by Greedy Approach minimum count will be 9 and chosen elements are 25 + 25 + 10 + 1 + 1 + 1 + 1 + 1 + 1 = 66. But its optimum answer is 6 when these elements are chosen: 25 + 10 + 10 + 10 + 10 + 1 = 66. So, Dynamic Programming will work here. But simple DP cannot be applied because K can go upto 10^9 .
Dynamic Programming approach:
- Precompute the minimum no. of elements chosen that constitutes sum upto 99 and store it in memo array.
- Also, sums upto 99 can only be formed by the combinations of 1, 10 and 25.
- Starting from end of K, iterate over each last 2 digits to find minimum no. of elements chosen that will sum to last two digits.
- Sum them all to find the minimum count.
Illustration of the above approach:
Let’s take K = 250166Let min_count = 0, last 2 digits = 66
add minimum number of elements to min_count that sums to 66 (it is calculated from memo array
that we have precomputed).
min_count = min_count + 6,
Now, min_count = 6, last 2 digits = 01add minimum number of elements to min_count sums to 1.
min_count = min_count + 1,
Now, min_count = 7, last 2 digits = 25add minimum number of elements to min_count sums to 25.
min_count = min_count + 1,
Now, min_count = 8.So, minimum number of elements chosen that sums to 250166 is 8.
Optimal chosen Elements are (250000, 100, 25, 10, 10, 10, 10, 1)
Below is the implementation of the above approach.
C++
// C++ implementation of the above approach #include <bits/stdc++.h> using namespace std; int minCount(int K) { // we will only store min counts // of sum upto 100 int memo[100]; // initialize with INT_MAX for (int i = 0; i < 100; i++) { memo[i] = INT_MAX; } // memo[0] = 0 as 0 is // made from 0 elements memo[0] = 0; // fill memo array with min counts // of elements that will constitute // sum upto 100 for (int i = 1; i < 100; i++) { memo[i] = min(memo[i - 1] + 1, memo[i]); } for (int i = 10; i < 100; i++) { memo[i] = min(memo[i - 10] + 1, memo[i]); } for (int i = 25; i < 100; i++) { memo[i] = min(memo[i - 25] + 1, memo[i]); } // min_count will store min // count of elements chosen long min_count = 0; // starting from end iterate over // each 2 digits and add min count // of elements to min_count while (K > 0) { min_count += memo[K % 100]; K /= 100; } return min_count; } // Driver code int main() { int K = 69; cout << minCount(K) << endl; return 0; } |
Java
// Java implementation of the above approach class GFG { static int minCount(int K) { // we will only store min counts // of sum upto 100 int memo[] = new int[100]; // initialize with INT_MAX for (int i = 0; i < 100; i++) { memo[i] = Integer.MAX_VALUE; } // memo[0] = 0 as 0 is // made from 0 elements memo[0] = 0; // fill memo array with min counts // of elements that will constitute // sum upto 100 for (int i = 1; i < 100; i++) { memo[i] = Math.min(memo[i - 1] + 1, memo[i]); } for (int i = 10; i < 100; i++) { memo[i] = Math.min(memo[i - 10] + 1, memo[i]); } for (int i = 25; i < 100; i++) { memo[i] = Math.min(memo[i - 25] + 1, memo[i]); } // min_count will store min // count of elements chosen int min_count = 0; // starting from end iterate over // each 2 digits and add min count // of elements to min_count while (K > 0) { min_count += memo[K % 100]; K /= 100; } return min_count; } // Driver code public static void main (String[] args) { int K = 69; System.out.println(minCount(K)); } } // This code is contributed by AnkitRai01 |
Python3
# Python3 implementation of the above approach def minCount(K): # we will only store min counts # of sum upto 100 memo=[10**9 for i in range(100)] # memo[0] = 0 as 0 is # made from 0 elements memo[0] = 0 # fill memo array with min counts # of elements that will constitute # sum upto 100 for i in range(1,100): memo[i] = min(memo[i - 1] + 1, memo[i]) for i in range(10,100): memo[i] = min(memo[i - 10] + 1, memo[i]) for i in range(25,100): memo[i] = min(memo[i - 25] + 1, memo[i]) # min_count will store min # count of elements chosen min_count = 0 # starting from end iterate over # each 2 digits and add min count # of elements to min_count while (K > 0): min_count += memo[K % 100] K //= 100 return min_count # Driver code K = 69 print(minCount(K)) # This code is contributed by mohit kumar 29 |
C#
// C# implementation of the above approach using System; class GFG { static int minCount(int K) { // we will only store min counts // of sum upto 100 int []memo = new int[100]; // initialize with INT_MAX for (int i = 0; i < 100; i++) { memo[i] = int.MaxValue; } // memo[0] = 0 as 0 is // made from 0 elements memo[0] = 0; // fill memo array with min counts // of elements that will constitute // sum upto 100 for (int i = 1; i < 100; i++) { memo[i] = Math.Min(memo[i - 1] + 1, memo[i]); } for (int i = 10; i < 100; i++) { memo[i] = Math.Min(memo[i - 10] + 1, memo[i]); } for (int i = 25; i < 100; i++) { memo[i] = Math.Min(memo[i - 25] + 1, memo[i]); } // min_count will store min // count of elements chosen int min_count = 0; // starting from end iterate over // each 2 digits and add min count // of elements to min_count while (K > 0) { min_count += memo[K % 100]; K /= 100; } return min_count; } // Driver code static public void Main () { int K = 69; Console.WriteLine(minCount(K)); } } // This code is contributed by ajit |
9
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:
- Generate a combination of minimum coins that sums to a given value
- Count of distinct sums that can be obtained by adding prime numbers from given arrays
- Count number of ordered pairs with Even and Odd Sums
- Partition a set into two subsets such that the difference of subset sums is minimum
- Index with Minimum sum of prefix and suffix sums in an Array
- Find original array from encrypted array (An array of sums of other elements)
- Replace all elements by difference of sums of positive and negative numbers after that element
- Count of pairs between two arrays such that the sums are distinct
- Maximum Length of Sequence of Sums of prime factors generated by the given operations
- Flip minimum signs of array elements to get minimum sum of positive elements possible
- Count number of elements between two given elements in array
- Minimum elements to change so that for an index i all elements on the left are -ve and all elements on the right are +ve
- Count of Array elements greater than all elements on its left and at least K elements on its right
- Count of Array elements greater than all elements on its left and next K elements on its right
- Count subarrays such that remainder after dividing sum of elements by K gives count of elements
- Count of binary strings of length N having equal count of 0's and 1's and count of 1's ≥ count of 0's in each prefix substring
- Find all distinct subset (or subsequence) sums of an array
- Find pairs in array whose sums already exist in array
- Find k pairs with smallest sums in two arrays
- Partition into two subarrays of lengths k and (N - k) such that the difference of sums is maximum
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.

