Knapsack

There is a potter who makes pottery wares. With his saving, he can purchase some pottery clay, say ‘K’ units to make pottery wares. He can transform this clay into ‘N’ different items. Each item requires a certain amount of day for production. From past experiences, he knows the profit he will make on each item. he wishes to maximize the profit.

Given the amount of clay he has, the number of things that can be made, the days required, and the profit associated with items. Help him find the maximum profit that he can earn.

Input format :
The very first line contains an integer ‘T’ which denotes the number of test cases.

The first line of each test case contains two space-separated integers, ‘N’ and ‘K’, where ‘N’ is the number of things that can be made and ‘K’ is the number of units of clay he has.

The second line of each test case contains ‘N’ integers, where each integer denotes the number of units of clay required for that item.

The third line of each test case contains ‘N’ integers, where each integer denotes the amount of profit the potter can get if that item gets sold out.
Output Format :
For every test case,
Return the maximum profit the potter can make in a new line.
Note :
You don’t need to print anything. It has already been taken care of. Just implement the given function.
Constraints :
1 <= T <= 100  
1 <= N <= 100 
1 <= K <= 100 
1 <= profit[i] <= 10000 
1 <= Clay[i] <= 2000
Where ‘T’ is the number of test cases , ‘N’ is the number of integers , ‘K’ is the number of units of clay ,Clay[i] is the amount of clay used to build that ith item.

Time Limit: 1sec
CodingNinjas
author
2y

This was a direct application of 0-1 knapsack problem. So dynamic programming approach of knapsack was applied to this problem.
We built the 2d array in bottom up manner.

CodingNinjas
author
2y
Brute Force

We don’t know whether after picking the first item we will reach an optimal solution. It may happen that this item costs too much and its profit is not much, or an item can cost very less w...read more

CodingNinjas
author
2y
DP

The idea here is the same, we will make two function calls, but this time we will try to reduce the repetitive calls by using a 2-D array.

In this array, the number of rows denotes the number of ite...read more

CodingNinjas
author
2y
DP

The idea here is the same, we will make two function calls, but this time we will try to reduce the repetitive calls by using a 1-D array.


In this array, the elements denote the maximum profit we ca...read more

Mantu Gupta
2y

Gsushshsisbjss. eieheieheheoeh

Add answer anonymously...
Wipro Software Engineer Interview Questions
Stay ahead in your career. Get AmbitionBox app
qr-code
Helping over 1 Crore job seekers every month in choosing their right fit company
65 L+

Reviews

4 L+

Interviews

4 Cr+

Salaries

1 Cr+

Users/Month

Contribute to help millions
Get AmbitionBox app

Made with ❤️ in India. Trademarks belong to their respective owners. All rights reserved © 2024 Info Edge (India) Ltd.

Follow us
  • Youtube
  • Instagram
  • LinkedIn
  • Facebook
  • Twitter