Data Structures & AlgorithmsHardGATE CS · ICPC regional
Items (weight, value) are [(5, 60), (4, 40), (6, 50), (3, 30), (2, 15), (7, 70)]. Each item can be taken at most once, and the total weight must not exceed 8. What is the maximum total value?
- A. 90
- B. 70
- C. 100
- D. 105
Correct answer: A. 90
Standard 0/1 knapsack: dp[c] = max(dp[c], dp[c − w] + v) with c scanned downwards so each item is used once. Greedy by value, or by value/weight ratio (fractional knapsack), can be beaten by a combination that packs the capacity exactly.
Practise more Arrays, Strings, Linked Lists, Stacks, Queues and Hashing questionsArrays, Strings, Linked Lists, Stacks, Queues and Hashing: notes and key points
More Arrays, Strings, Linked Lists, Stacks, Queues and Hashing questions
- A shop has these items as (weight, value, number available): [(2, 10, 2), (3, 14, 3), (5, 30, 1)]. You may take at most the listed number…
- How many subsets of [2, 3, 5, 7, 4, 1, 6, 8] (chosen by position, the empty set excluded) have a sum of exactly 10?
- The numbers [31, 26, 33, 21, 40] are split into two groups (every number goes to exactly one group). What is the smallest possible…
- Each number in [1, 2, 3, 4, 5, 6, 7] gets a sign '+' or '-', and all are then added. How many sign choices give a total of exactly 4?
- How many NON-EMPTY subsets of [4, 7, 2, 9, 5, 3, 8, 6] have a sum divisible by 3? Subsets are chosen by position.
- Items as (weight, value) are [(3, 5), (4, 6), (5, 9), (7, 13), (2, 8)]. Each item can be used at most once. What is the maximum value of a…