Experiment 8
Experiment 8
Aim:- Given an array of integers and a target sum, determine the sum nearest to but not exceeding the
target that can be created. To create the sum, use any element of your array zero or more times.
For example, if arr=[2,3,4] and your target sum is 10 , you might select [2,2,2,2,2],[2,2,3,3] or [3,3,3,1] .
Function Description
Complete the unboundedKnapsack function in the editor below. It must return an integer that represents
k: an integer
Input Format
- The first line contains two integers n and k, the length of arr and the target sum.
Output Format
Print the maximum sum for each test case which is as near as possible, but not exceeding, to the target sum
on a separate line.
Sample Input
2
3 12
169
59
34448
Code:
#include <iostream>
#include <vector>
#include <algorithm>
return dp[k];
}
int main() {
// Input values
int t;
cin >> t;
while (t--) {
int n, k;
cin >> n >> k;
vector<int> arr(n);
for (int i = 0; i < n; i++) {
cin >> arr[i];
}
return 0;
}
Output: