Data Structures & AlgorithmsHardICPC regional · counting
Problem: How many 3-element subsets of {1, 2, 3, 4, 5, 6} have a sum divisible by 3? Run the code.
Problem: How many 3-element subsets of {1, 2, 3, 4, 5, 6} have a sum divisible by 3? Run the code.
from itertools import combinations print(sum(1 for c in combinations(range(1, 7), 3) if sum(c) % 3 == 0))What is printed?
- A. 6
- B. 4
- C. 48
- D. 8
Correct answer: D. 8
Residues mod 3 of 1..6 are two each of 0, 1, 2; a triple sums to 0 mod 3 if it is all one residue (3 × C(2,3) = 0) or one of each (2 × 2 × 2 = 8), so 8. Assuming one third of the 20 triples gives 6, using range(1, 6) gives 4, and counting ordered triples gives 48.
More Coding Interview Problems questions
- Problem: Minimum number of deletions so that no two adjacent characters of 'aaabbb' are equal. Run the code. python s = 'aaabbb' kept = []…
- Problem: What is the maximum product of positive integer parts when 10 is split into at least two parts? Run the DP. python n = 10 dp =…
- Problem: How many integer partitions does 6 have (unordered sums of positive integers equal to 6)? Run the DP. python n = 6 p = [1] + [0]…
- Problem: How many ordered pairs (x, y) with 1 <= x, y <= 10 have x y <= 10? Run the code. python print(sum(1 for x in range(1, 11) for y…
- Problem: How many integers in [1, 1000] contain the digit 0? Run the code. python print(sum(1 for x in range(1, 1001) if '0' in str(x)))…
- Problem: What is the minimum number of perfect squares (1, 4, 9, ...) that sum to 12? Run the DP. python INF = 10 9 dp = [0] + [INF] 12…