Data Structures & AlgorithmsModerateCodeforces Div2 C · complexity estimate
Problem: For n = 10^5 elements and q = 10^5 queries, a sqrt-decomposition answers each query in O(sqrt n). Roughly how many elementary operations does this take in total?
- A. About 10^10
- B. About 10^5
- C. About 3 * 10^7
- D. About 2 * 10^5 * log(10^5)
Correct answer: C. About 3 * 10^7
sqrt(10^5) is about 316, and 10^5 queries times 316 gives about 3.2 * 10^7 operations, which runs comfortably. The last option describes a segment tree, not sqrt decomposition.
More Coding Interview Problems questions
- Problem: How many scalar multiplications does the naive product of a 3 x 4 matrix and a 4 x 2 matrix need?
- Problem: Using the cost of a spanning tree on the complete graph K4 (Cayley's formula n^(n-2)), how many spanning trees does K4 have?
- Problem: How many decimal palindromes lie in [1, 1000]? Run the code. python print(sum(1 for x in range(1, 1001) if str(x) ==…
- Problem: What is the sum of the squares of the odd numbers from 1 to 10? Run the code. python print(sum(x x for x in range(1, 11) if x % 2…
- Problem: How many iterations does Euclid's algorithm take for gcd(1071, 462)? Count the loop passes of the code. python a, b = 1071, 462…
- Problem: From the left, how many buildings of heights [3, 1, 4, 2, 5] are visible (a building is visible if it is strictly taller than all…