Maximize count of array elements required to obtain given sum
Last Updated :
23 Jan, 2022
Given an integer V and an array arr[] consisting of N integers, the task is to find the maximum number of array elements that can be selected from array arr[] to obtain the sum V. Each array element can be chosen any number of times. If the sum cannot be obtained, print -1.
Examples:
Input: arr[] = {25, 10, 5}, V = 30
Output: 6
Explanation:
To obtain sum 30, select arr[2] (= 5), 6 times.
Therefore, the count is 6.
Input: arr[] = {9, 6, 4, 3}, V = 11
Output: 3
Explanation:
To obtain sum 11, possible combinations is : 4,4,3
Therefore, the count is 3
Naive Approach: The simplest approach is to recursively find the maximum number of array elements to generate the sum V using array elements from indices 0 to j before finding the maximum number of elements required to generate V using elements from indices 0 to i where j < i < N. After completing the above steps, print the count of array elements required to obtain the given sum V.
Time Complexity: O(VN)
Auxiliary Space: O(N)
Efficient Approach: To optimize the above approach, the idea is to use Dynamic Programming. Follow the below steps to solve the problem:
- Initialize an array table[] of size V + 1 where table[i] will store the optimal solution to obtain sum i.
- Initialize table[] with -1 and table[0] with 0 as 0 array elements are required to obtain the value 0.
- For each value from i = 0 to V, calculate the maximum number of elements from the array required by the following DP transition:
table[i] = Max(table[i – arr[j]], table[i]), Where table[i-arr[j]]!=-1
where 1 ? i ? V and 0 ? j ? N
- After completing the above steps, print the value of the table[V] which is the required answer.
Below is the implementation of the above approach:
C++14
#include <bits/stdc++.h>
using namespace std;
int maxCount(vector< int > arr, int m, int V)
{
vector< int > table(V + 1);
table[0] = 0;
for ( int i = 1; i <= V; i++)
table[i] = -1;
for ( int i = 1; i <= V; i++) {
for ( int j = 0; j < m; j++) {
if (arr[j] <= i) {
int sub_res = table[i - arr[j]];
if (sub_res != -1 && sub_res + 1 > table[i])
table[i] = sub_res + 1;
}
}
}
return table[V];
}
int main()
{
vector< int > arr = { 25, 10, 5 };
int m = arr.size();
int V = 30;
cout << (maxCount(arr, m, V));
return 0;
}
|
Java
import java.io.*;
class GFG {
static int maxCount( int arr[], int m, int V)
{
int table[] = new int [V + 1 ];
table[ 0 ] = 0 ;
for ( int i = 1 ; i <= V; i++)
table[i] = - 1 ;
for ( int i = 1 ; i <= V; i++) {
for ( int j = 0 ; j < m; j++) {
if (arr[j] <= i) {
int sub_res = table[i - arr[j]];
if (sub_res != - 1
&& sub_res + 1 > table[i])
table[i] = sub_res + 1 ;
}
}
}
return table[V];
}
public static void main(String[] args)
{
int arr[] = { 25 , 10 , 5 };
int m = arr.length;
int V = 30 ;
System.out.println(maxCount(arr, m, V));
}
}
|
Python3
def maxCount(arr, m, V):
table = [ 0 for i in range (V + 1 )]
table[ 0 ] = 0
for i in range ( 1 , V + 1 , 1 ):
table[i] = - 1
for i in range ( 1 , V + 1 , 1 ):
for j in range ( 0 , m, 1 ):
if (arr[j] < = i):
sub_res = table[i - arr[j]]
if (sub_res ! = - 1 and
sub_res + 1 > table[i]):
table[i] = sub_res + 1
return table[V]
if __name__ = = '__main__' :
arr = [ 25 , 10 , 5 ]
m = len (arr)
V = 30
print (f'Maximum number of array elements required :
{maxCount(arr, m, V)}')
|
C#
using System;
class GFG{
static int maxCount( int [] arr,
int m, int V)
{
int [] table = new int [V + 1];
table[0] = 0;
for ( int i = 1; i <= V; i++)
table[i] = -1;
for ( int i = 1; i <= V; i++)
{
for ( int j = 0; j < m; j++)
{
if (arr[j] <= i)
{
int sub_res = table[i - arr[j]];
if (sub_res != -1 &&
sub_res + 1 > table[i])
table[i] = sub_res + 1;
}
}
}
return table[V];
}
static void Main()
{
int [] arr = {25, 10, 5};
int m = arr.Length;
int V = 30;
Console.WriteLine(maxCount(arr,
m, V));
}
}
|
Javascript
<script>
function maxCount(arr, m, V)
{
let table = [];
table[0] = 0;
for (let i = 1; i <= V; i++)
table[i] = -1;
for (let i = 1; i <= V; i++) {
for (let j = 0; j < m; j++) {
if (arr[j] <= i) {
let sub_res = table[i - arr[j]];
if (sub_res != -1
&& sub_res + 1 > table[i])
table[i] = sub_res + 1;
}
}
}
return table[V];
}
let arr = [ 25, 10, 5 ];
let m = arr.length;
let V = 30;
document.write(maxCount(arr, m, V));
</script>
|
Time Complexity: O(N * V)
Auxiliary Space: O(N)
Share your thoughts in the comments
Please Login to comment...