0/1 Knapsack Problem Statement

A thief is planning to rob a store and can carry a maximum weight of 'W' in his knapsack. The store contains 'N' items where the ith item has a weight of 'wi' and a value of 'vi'. Given the maximum weight capacity of the knapsack, determine the maximum total value of items that the thief can steal.

Input:

The first line contains an integer 'T' representing the number of test cases.
Each test case includes:
1. An integer 'N' denoting the number of items.
2. A line with 'N' space-separated integers representing the weights of the items.
3. A line with 'N' space-separated integers representing the values of the items.
4. An integer 'W' denoting the maximum weight capacity of the knapsack.

Output:

For each test case, output a single integer denoting the maximum value that can be stolen. Each result should be printed on a new line.

Example:

Input:
2
3
10 20 30
60 100 120
50
3
1 2 3
10 20 30
5
Output:
220
50

Constraints:

  • 1 <= T <= 10
  • 1 <= N <= 10^2
  • 1 <= wi <= 50
  • 1 <= vi <= 10^2
  • 1 <= W <= 10^3

Follow-Up:

Can the problem be solved using a space complexity of not more than O(W)?
AnswerBot
4mo

Yes, the 0/1 Knapsack Problem can be solved using dynamic programming with a space complexity of not more than O(W).

  • Use a 1D array to store the maximum value that can be stolen for each weight capacit...read more

Yogesh Bodke
2y

(i) = the highest total value that can be achieved from a knapsack with capacity i. V (8) = max[r1 + V (5), r2 + V (0), r3 + V (3)] = max[4 + 5, 6+0, 5 + 4] = 9. Thus, the optimal value is 9, which is...read more

Help your peers!
Select
Add answer anonymously...

TCS System Engineer interview questions & answers

A System Engineer was asked 1w agoQ. What are the best practices for making API calls in Python?
A System Engineer was asked 1w agoQ. How proficient are you in Python?
A System Engineer was asked 2w agoQ. What is Salesforce?

Popular interview questions of System Engineer

A System Engineer was asked 1w agoQ1. What are the best practices for making API calls in Python?
A System Engineer was asked 1w agoQ2. How proficient are you in Python?
A System Engineer was asked 2w agoQ3. What is unsupervised and supervised machine learning?

Top HR questions asked in TCS System Engineer

A System Engineer was asked 1mo agoQ1. Are you open to the possibility of relocating?
A System Engineer was asked 1mo agoQ2. What can you explain about internships and the differences between mini and majo...read more
A System Engineer was asked 1mo agoQ3. Can you describe the project you completed at the university level?
TCS System Engineer Interview Questions
Stay ahead in your career. Get AmbitionBox app
play-icon
play-icon
qr-code
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

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

Follow Us
  • Youtube
  • Instagram
  • LinkedIn
  • Facebook
  • Twitter
Profile Image
Hello, Guest
AmbitionBox Employee Choice Awards 2025
Winners announced!
awards-icon
Contribute to help millions!
Write a review
Write a review
Share interview
Share interview
Contribute salary
Contribute salary
Add office photos
Add office photos
Add office benefits
Add office benefits