Data Structures & AlgorithmsModerate
Count the NON-EMPTY subsets of the 12 items 3 5 6 7 2 9 4 8 1 10 11 12 (chosen by position) whose sum is divisible by 4.
- A. 1023
- B. 1024
- C. 511
- D. 1022
Correct answer: A. 1023
Subset counting by sum residue uses a DP over residues mod 4 where each item either joins a subset or not; the empty subset has residue 0 and must be excluded. Counting residue 2, or restricting to even sizes, answers different questions.
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 NON-EMPTY subsequences of 1 2 3 5 6 8 9 (subsets chosen by position) have (maximum − minimum) ≤ 2?
- How many NON-EMPTY subsequences of 2 3 4 5 6 8 (position subsets) have a product congruent to 1 modulo 7?
- How many subsequences of EXACTLY three elements of 1 2 3 4 5 6 7 8 (chosen by position) have an EVEN sum?
- In the string "aabbaab", how many pairs of positions i < j form the subsequence 'ab' (S[i] = 'a' and S[j] = 'b')?
- How many index triples i < j < k in "aabbccabc" spell the subsequence 'abc' (characters need not be adjacent)?
- How many subsets of {1, 2, ..., 9} have sum exactly 10 (the empty set is not counted, and each value is used at most once)?