Colourful Knapsack Problem Statement

You are given N stones labeled from 1 to N. The i-th stone has the weight W[i]. There are M colors labeled by integers from 1 to M. The i-th stone has the color C[i] which is an integer between 1 to M, both inclusive.

Your task is to fill a Knapsack with these stones. The Knapsack can hold a total weight of X. You need to choose exactly M stones, one of each color. The total weight of the stones should not exceed X. Since the Knapsack has a capacity X, you should aim to fill it as much as possible — minimizing the unused capacity.

Input:

 The first line contains three integers 'N', 'M', and 'X', where 'N' represents the total number of stones, 'M' represents the total number of color stones, and 'X' represents the total weight capacity of the knapsack.  The second line contains 'N' integers, W[1], W[2], ... W[N], representing the weights of the stones.  The third line contains 'N' integers C[1], C[2], ... C[N], representing the colors of the stones. 

Output:

 Print the minimum unused capacity of the Knapsack as a single integer. If there is no way to fill the Knapsack, print -1. 

Example:

Input:
N = 5, M = 3, X = 10
W = [1, 3, 3, 5, 5]
C = [1, 2, 3, 1, 2]
Output:
1

Constraints:

  • 1 <= M <= N <= 100
  • 1 <= X <= 10000
  • 1 <= W[i] <= 100
  • 1 <= C[i] <= M
Note:

The time limit for this problem is 1 second.

AnswerBot
4mo

The task is to fill a Knapsack with stones of different colors and weights, minimizing unused capacity.

  • Given N stones with weights W and colors C, choose M stones (one of each color) to fill Knapsack ...read more

Help your peers!
Select
Add answer anonymously...

Adobe Product Intern interview questions & answers

A Product Intern was asked 5mo agoQ. Given a knapsack with a maximum weight capacity W and a set of items, each with ...read more
A Product Intern was asked Q. Explain C++ memory management and pointers.
A Product Intern was asked Q. What is the difference between a process and a thread?

Popular interview questions of Product Intern

A Product Intern was asked 5mo agoQ1. Given a knapsack with a maximum weight capacity W and a set of items, each with ...read more
A Product Intern was asked Q2. Explain C++ memory management and pointers.
A Product Intern was asked Q3. What is the difference between a process and a thread?
Adobe Product Intern 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