0/1 Knapsack Problem Statement
A thief is planning to rob a store and can carry a maximum weight 'W' in their knapsack. The store contains 'N' items, each with a known weight and value. Given these constraints, determine the maximum profit that the thief can generate by selecting items to steal, without breaking any item.
Note: Items cannot be broken; they must be taken whole.
Example:
Input:
N = 4, W = 10, weights = [6, 1, 5, 3], values = [3, 6, 1, 4]
Output:
13
Explanation:
The best way to fill the knapsack is by selecting items with weights 6, 1 and 3, making the total value of the knapsack to be 3 + 6 + 4 = 13.
Constraints:
1 <= T <= 10
1 <= N <= 10^3
1 <= W <= 10^3
1 <= weights[i] <= 10^3
1 <= values[i] <= 10^3
Input:
The first line contains a single integer 'T', representing the number of test cases.
The 'T' test cases are as follows:
The first line contains two integers 'N' and 'W', denoting the number of items and the maximum weight the thief can carry.
The second line contains 'N' space-separated integers representing the weights of the items.
The third line contains 'N' space-separated integers representing the values of the items.
Output:
The output for each test case is a single line containing the maximum profit the thief can generate.

AnswerBot
4mo
The 0/1 Knapsack Problem involves maximizing profit by selecting items with known weights and values to fit within a knapsack of limited capacity.
Create a 2D array to store the maximum profit that can...read more
Help your peers!
Add answer anonymously...
Nagarro Software Developer interview questions & answers
A Software Developer was asked 8mo agoQ. Explain Kafka and how you would implement it.
A Software Developer was asked 9mo agoQ. Is the directory a file?
A Software Developer was asked 9mo agoQ. What is memory segmentation?
Popular interview questions of Software Developer
A Software Developer was asked 8mo agoQ1. Explain Kafka and how you would implement it.
A Software Developer was asked 9mo agoQ2. Is the directory a file?
A Software Developer was asked 9mo agoQ3. What is memory segmentation?
Stay ahead in your career. Get AmbitionBox app


Trusted by over 1.5 Crore job seekers to find their right fit company
80 L+
Reviews
10L+
Interviews
4 Cr+
Salaries
1.5 Cr+
Users
Contribute to help millions
AmbitionBox Awards
Get AmbitionBox app

