Data Structures & AlgorithmsVery hardICPC regional · meet in the middle
Problem: For a set of 40 items split into two halves of 20, the meet-in-the-middle approach solves subset-sum queries in roughly which time?
- A. 2^40, about 10^12 operations
- B. 40^2, about 1600 operations
- C. 40!, which is infeasible
- D. 2^20 * log, about 2 * 10^7 operations
Correct answer: D. 2^20 * log, about 2 * 10^7 operations
Each half has 2^20 subset sums, and sorting plus binary search (or a hash set) processes them in about 2^20 log operations. Enumerating all 2^40 subsets directly is the trap.
More Coding Interview Problems questions
- Problem: Grundy numbers for the subtraction game where a move removes 1 or 2 stones: what are g(0) through g(5)? Run the code. python g =…
- Problem: Using the bubble sort rule, what is the array after exactly one left-to-right pass over [3, 1, 2]? Run the code. python a = [3…
- Problem: How many legal moves does a knight have from the corner (0,0) of an 8 x 8 board? Run the code. python moves = [(1, 2), (2, 1)…
- Problem: What is 7^222 mod 10? Run the code. python print(pow(7, 222, 10)) What is printed?
- Problem: How many integers in [1, 500] are divisible by 4 or by 6? Run the code. python print(sum(1 for x in range(1, 501) if x % 4 == 0…
- Problem: How many ways can [1, 2, 3, 4] be split into two non-empty contiguous parts with equal sums? Run the code. python a = [1, 2, 3…