Data Structures & AlgorithmsVery hardGATE CS · TCS NQT
With weight limit 15, what is the maximum number of items from [5, 3, 8, 2, 7, 4] you can carry (choose any subset)?
- A. 5
- B. 4
- C. 3
- D. 6
Correct answer: B. 4
Choose the lightest items first: 2 + 3 + 4 + 5 = 14 gives four items, and adding 7 would exceed 15. The greedy by weight is optimal for maximising count.
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
- How many integers from 1 to 500 inclusive are divisible by 4 but not by 6?
- How many different output orders can a stack produce from the input 1, 2, 3, 4 pushed in order?
- In "abccbaxyz", take the longest palindromic substring (the first one if tied) and count its distinct characters.
- In "abcdbefg", what is the 0-based index of the first character that repeats an earlier character?
- How many integers from 1 to 60 inclusive are multiples of 3 or of 4 but not of 12?
- What is the length of the longest common prefix of "interview", "internet" and "interval"?