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.
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)
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 = 250166
Let 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 = 01
add minimum number of elements to min_count sums to 1.
min_count = min_count + 1,
Now, min_count = 7, last 2 digits = 25
add 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.
- Count number of ordered pairs with Even and Odd Sums
- Minimum number of elements to be removed such that the sum of the remaining elements is equal to k
- Count number of elements between two given elements in array
- 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
- Count of pairs between two arrays such that the sums are distinct
- Flip minimum signs of array elements to get minimum sum of positive elements possible
- Minimum number of subsets with distinct elements
- Minimum number of changes such that elements are first Negative and then Positive
- Count number of even and odd elements in an array
- Minimum number of moves to make all elements equal
- Minimum number of operations on an array to make all elements 0
- Count number of smallest elements in given range
- Count pairs of elements such that number of set bits in their AND is B[i]
- Count the number of elements in an array which are divisible by k
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to firstname.lastname@example.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.