Data Structures & AlgorithmsHardCodeforces Div2 B · recurrence problems
Problem: n = 10 people stand in a circle and every 3rd person is removed until one remains (counting starts at person 1 and the removed person's neighbour is next). Using the recurrence J(1) = 0, J(i) = (J(i-1) + 3) mod i and answering 1-indexed, which person survives?
- A. 2
- B. 7
- C. 10
- D. 4
Correct answer: D. 4
The recurrence gives J(10) = 3 (0-indexed), so the survivor is person 4 in 1-indexed numbering. Reporting the 0-indexed value 3 is a common off-by-one trap.
More Coding Interview Problems questions
- Problem: Compute F(100) mod 1000 where F(0) = 0, F(1) = 1 and F(n) = F(n-1) + F(n-2). python def mul(A, B, m): return [[sum(A[i][k]…
- Problem: Count the inversions of the array [3, 1, 4, 1, 5, 9, 2, 6], where an inversion is a pair of positions i < j with a[i] > a[j]…
- Problem: Huffman coding is applied to symbol frequencies 5, 9, 12, 13, 16, 45, always merging the two smallest weights. What is the total…
- Problem: Find the maximum of every window of size 3 in the array below, printing the list of window maxima. python from collections import…
- Problem: Compute the Z-array (z[i] = length of the longest common prefix of s and s[i:], with z[0] set to 0) for s = 'aabxaab'. python def…
- Problem: Count the palindromic substrings of 'abacaba' (every occurrence counts, so single characters count too). python def count(s)…