Data Structures & AlgorithmsModerateGATE CS · TCS NQT
How many DISTINCT values can the sum of a subset of [1, 2, 4, 8, 3] take? The empty subset (sum 0) is included.
- A. 32
- B. 18
- C. 19
- D. 21
Correct answer: C. 19
Maintain the set of reachable sums: each new number x adds the shifted copy {s + x}. Counting subsets instead of sums overcounts whenever two different subsets share the same total.
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 subsets of [2, 5, 3, 7, 1] (chosen by position, empty subset included) have a total sum at most 8?
- How many permutations of 1 to 6 have NO fixed point (that is, p[i] ≠ i for every position i)?
- How many strings of length 6 over {a, b} have NO position i with s[i] = s[i+2] (for every valid i)?
- How many arrays of length 4 with entries from {1, 2, ..., 3} are NON-INCREASING (each entry is at least the next)?
- Daily prices are [3, 2, 6, 5, 0, 3, 8, 1, 9]. You may complete at most 3 buy-then-sell transactions (one share at a time, sell before the…
- What is the maximum sum of a non-empty contiguous subarray of [4, -6, 3, -1, 7, -2, 5] that contains AT MOST one negative element?