Data Structures & AlgorithmsModerateCodeforces Div2 B · complexity round
Problem: What is the time complexity of this loop in terms of n?
Problem: What is the time complexity of this loop in terms of n?
for i in range(1, n + 1):
for j in range(i, n + 1, i):
work()- A. O(n^2)
- B. O(n sqrt n)
- C. O(n)
- D. O(n log n)
Correct answer: D. O(n log n)
The inner loop runs about n/i times, and summing n/i over i = 1..n gives the harmonic series n * H(n), which is O(n log n).
More Coding Interview Problems questions
- Problem: Count right/down paths from the top-left to the bottom-right of the 3x3 grid below, where '#' is blocked. The code returns the…
- Problem: What does the following print? python def add(x, bucket=[]): bucket.append(x) return bucket add(1) add(2) print(add(3))
- Problem: What does the following print for the array [7, 10, 4, 3, 20, 15] with k = 3? python import heapq def kth(a, k): h = [] for x in…
- Problem: What does the following print? python ops = [('b', 2), ('a', 2), ('c', 1), ('d', 3)] print([x for x, in sorted(ops, key=lambda t…
- Problem: Coins of values {1, 5, 6, 9} are available, unlimited copies each. What is the minimum number of coins needed to make exactly 11?
- Problem: In the O(n log n) longest increasing subsequence algorithm, the array tails[k] is maintained over the input. What does tails[k]…