Data Structures & AlgorithmsHardCodeforces Div2 B · recurrence
Problem: What does the Josephus recurrence J(1)=0, J(i)=(J(i-1)+2) mod i give for n = 7 (answer 1-indexed, every second person eliminated)? Run the code.
Problem: What does the Josephus recurrence J(1)=0, J(i)=(J(i-1)+2) mod i give for n = 7 (answer 1-indexed, every second person eliminated)? Run the code.
J = 0
for i in range(2, 8):
J = (J + 2) % i
print(J + 1)What is printed?- A. 7
- B. 6
- C. 5
- D. 3
Correct answer: A. 7
With n = 7 = 4 + 3 and k = 2, the survivor is 2L + 1 = 2 * 3 + 1 = 7. The trap 3 comes from the 0-indexed value confusion.
More Coding Interview Problems questions
- Problem: How many integers in [1, 100] are divisible by both 3 and 5? Run the code. python print(sum(1 for x in range(1, 101) if x % 3 ==…
- Problem: Which statement about building a segment tree over an array of n elements is correct?
- Problem: What is the maximum product of three numbers in [-10, -10, 5, 2]? Run the code. python a = sorted([-10, -10, 5, 2])…
- Problem: What is the maximum subarray sum over all prefixes of [-2, 1, -3, 4]? Print the list of running maximum-subarray values with…
- Array a = [1, -2, 3, 2, -1, 4, -3, 2]. What is the length of the longest contiguous subarray whose sum is exactly 4?
- Array a = [3, -5, 2, -1, -4, 2, 1, -6, 2]. How many contiguous subarrays of length exactly 4 have a negative sum?