Data Structures & AlgorithmsHardGATE CS
50 people stand in a circle numbered 1 to 50. Starting the count at person 1, every 7th person (counting the first as 1) is removed, and counting resumes with the next person. Which numbered person is the last survivor?
- A. 8
- B. 41
- C. 1
- D. 45
Correct answer: C. 1
Use J(1)=0 and J(i)=(J(i-1)+7) mod i, then add 1: the survivor is person 1. Changing k by one moves the survivor to person 45 or 41.
More Coding Interview Problems questions
- How many subsets of {1, 2, ..., 15} (including the empty set) contain no two consecutive integers and have an even sum?
- How many integers n with 1 <= n <= 100000 are divisible by exactly one of the numbers 6, 10 and 15?
- How many primes p < 500 have p - 2 or p + 2 also prime (that is, belong to at least one twin-prime pair, counted as individual primes)?
- 0/1 knapsack with capacity 27: weights [13, 8, 9, 14, 12, 4, 11] and values [25, 19, 16, 27, 22, 8, 23] (item i has weight w[i] and value…
- How many structurally distinct binary trees with 8 nodes have at most 4 levels (a single root alone is 1 level)?
- How many binary strings of length 12 contain neither 101 nor 010 as a substring?