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

Help your peers!
Select
Add answer anonymously...

Swiggy Software Developer interview questions & answers

A Software Developer was asked Q. Subsequences of String Problem Statement You are provided with a string 'STR' th...read more
A Software Developer was asked Q. Unique Binary Search Trees Problem Statement Given an integer 'N', your task is ...read more
A Software Developer was asked Q. Count Ways To Travel Triangular Pyramid Bob is given a triangular pyramid with v...read more

Popular interview questions of Software Developer

A Software Developer was asked Q1. Subsequences of String Problem Statement You are provided with a string 'STR' th...read more
A Software Developer was asked Q2. Unique Binary Search Trees Problem Statement Given an integer 'N', your task is ...read more
A Software Developer was asked Q3. Count Ways To Travel Triangular Pyramid Bob is given a triangular pyramid with v...read more
Swiggy Software Developer 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