Data Structures & AlgorithmsHardCodeforces Div2 B · residues
Problem: How many pairs (i < j) of the array 1..14 have a[i] + a[j] divisible by 7? Run the code.
Problem: How many pairs (i < j) of the array 1..14 have a[i] + a[j] divisible by 7? Run the code.
a = list(range(1, 15)) print(sum(1 for i in range(len(a)) for j in range(i + 1, len(a)) if (a[i] + a[j]) % 7 == 0))What is printed?
- A. 26
- B. 15
- C. 13
- D. 12
Correct answer: C. 13
Each residue mod 7 occurs twice in 1..14: (0,0) gives 1 pair and (1,6), (2,5), (3,4) give 2 × 2 = 4 each, total 13. Counting ordered pairs gives 26, allowing i = j adds 7 + 7 and 14 + 14 for 15, and forgetting the (0,0) class gives 12.
More Coding Interview Problems questions
- Problem: Which is the worst-case time complexity of the median-of-medians selection algorithm for finding the k-th smallest element of n…
- Problem: What is the worst-case number of comparisons to merge two sorted lists of lengths m and n?
- Problem: For coprime m and n, which identity for Euler's totient function is correct?
- Problem: An undirected graph has vertices 0–7 and edges 0-1, 0-2, 1-2, 2-3, 4-5, 5-6, 6-4, 6-7. How many connected components contain at…
- Problem: Vertices 0–5 form a directed graph with arcs 0→1, 1→2, 2→3, 3→1, 3→4, 4→5, 5→4. Let S be the set of vertices reachable from 0…
- Problem: Kruskal's algorithm on the edges 0-1 (3), 1-2 (1), 0-2 (2), 2-3 (5), 1-3 (4), 3-4 (2), 2-4 (6) processes the edges in increasing…