Data Structures & AlgorithmsModerateTCS NQT · counting
Problem: How many 2-element subsets of {1, 2, 3, 4, 5} have an even sum? Run the code.
Problem: How many 2-element subsets of {1, 2, 3, 4, 5} have an even sum? Run the code.
from itertools import combinations print(sum(1 for a, b in combinations([1, 2, 3, 4, 5], 2) if (a + b) % 2 == 0))What is printed?
- A. 6
- B. 3
- C. 4
- D. 5
Correct answer: C. 4
Even sums come from two odd numbers (3 choose 2 = 3 pairs) or two even numbers (1 pair), for 4 in total.
More Coding Interview Problems questions
- Problem: Which statement about the sparse table data structure for static range-minimum queries is correct?
- Problem: Which statement about heavy-light decomposition for path queries on a tree is correct?
- Problem: Which statement about Fermat's little theorem is correct?
- Problem: What is the time for one point update and one rectangle-sum query in a 2D Fenwick tree on an n x n grid?
- Problem: Why can subtree queries on a rooted tree be answered with a range query on an array?
- Problem: An undirected graph has 10 vertices (0–9) and 6 edges: 0-1, 1-2, 3-4, 5-6, 6-7, 7-5. What is the minimum number of new edges that…