Flipkart Coding Question – Solved

8 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

| You are given a board of size M Γ— N where each cell can be either empty ('O') o… |
| Undirected Coloured Graph Shortest Path You are given an undirected weight… |
| Village Voyage A computer game "Village Voyage" has N villages (labeled 1 to… |
| Academic Decathlon Students are being selected for an academic decathlon tea… |
| Sum of Arrays Given two arrays each of length n, arr1 and arr2, in one opera… |
| Count Swaps During Custom Sorting Analyze the efficiency of the following so… |