Intuition
The problem wants us to eliminate in every round, an index and return the last person standing.
The criteria is that the contenders are standing in a circular line, and we are given an int k
. Starting from contender 0, we eliminate the contender at index k
. If k
goes beyond len(ar)
we start from 0 again (remember it’s a circular list).
This is the perfect use case of the modulo
operator. The nature of the modulo operator simulates a circular list, which is exactly what we want.
If
A
<B
, thenA
%B
=A
If
A
>B
, thenA
%B
=C
whereC
is the remainder ofA
andB
Code
Python3
Big O Analysis
-
Runtime
The runtime complexity here is since are simulating the rules of the game.
-
Memory
The memory usage is since we utilise an array to store current contenders.
— A