Data Structures & AlgorithmsHardProduct Company SDE · ICPC regional
Count the 4-element subsets of {1, 2, ..., 12} whose sum is divisible by 5. Count by brute force over combinations.
Count the 4-element subsets of {1, 2, ..., 12} whose sum is divisible by 5. Count by brute force over combinations.
from itertools import combinations print(sum(1 for t in combinations(range(1,13),4) if sum(t)%5==0))What is printed?
- A. 99
- B. 126
- C. 2376
- D. 44
Correct answer: A. 99
Subsets are unordered, so combinations are the right enumeration and the divisibility test is on the sum by 5. Testing divisibility by 4 is a different condition, counting ordered arrangements multiplies the count by 24, and using 3-element subsets is a different size.
More Coding Interview Problems questions
- Find the smallest positive integer x such that x ≡ 2 (mod 3), x ≡ 3 (mod 5) and x ≡ 2 (mod 7). Solve by brute-force search. python…
- How many trailing zeros does 100! have in base 10? Use Legendre's formula with powers of 5, counted by running the loop over p = 5, 25…
- The string s = "abcbdab" needs characters deleted to become a palindrome. What is the minimum number of deletions? Use n minus the longest…
- A 4×4 grid has rows "..#.", ".##.", "##.#", "#...". Entering a cell costs 1 if it is '#' and 0 otherwise, and you may move in four…
- Array c = [1, 2, 3, 0, 3, 3, 0, 6]. In how many ways can it be split into three non-empty contiguous parts whose sums are all equal? Run…
- Array d = [1, 2, 1, 3, 4, 3, 5]. Count the contiguous subarrays containing exactly three distinct values. Run the brute-force code. python…