Flipkart Coding Question – Solved

2 Live
Modified Knapsack Problem The Knapsack problem is a well-known problem in the field of computer programming and problem-solving. To make it more interesting, an interviewer uses a modified version of the problem. Given n items, where the weight of the i-th item is 2^i, and the cost of the i-th item is cost[i], find the minimum amount needed to purchase the items such that the combined weight of the purchased items is at least minWeight. Example Consider n = 5 ,cost = [2, 5, 7, 11, 25] ,minWeight = 26 One of the optimal ways to purchase the items is as follows: Buy 2 units of the 0th item and 3 units of the 3rd item. Total cost = (2 Γ— 2) + (3 Γ— 11) = 37. Total weight = (2 Γ— 2^0) + (3 Γ— 2^3) = 26, which is at least minWeight. Return the total cost of the items, 37. Function Description Complete the function getMinimumCost in the editor below. getMinimumCost has the following parameters: - int cost[n]: the cost of each item - int minWeight: the minimum combined weight

Asked in: Flipkart

Image of the Question

Question Image

All Testcases Passed βœ”



Passcode Image

Solution


Please login to view the solution


Related Questions

| Given an n x m grid, where rows are numbered from 7 to n and columns from 1 to … |
| There are 'N' coders standing in a line, where i denotes the ith position of a … |
| A birthday party was attended by N number of kids, and each kid was given a uni… |
| Given a matrix of size m * n, where m denotes the number of rows (starting with… |
| A traveler is traveling from the city of Zeta to Omega. He starts with X amount… |
| As an operations engineer at Amazon, you are responsible for organizing the dis… |