title: Josephus Algorithm
body: <p>I was reading the algorithm for the <a href="https://en.wikipedia.org/wiki/Josephus_problem" rel="noreferrer">Josephus Problem</a>.</p>
  
  <p>I came across the following algorithm :</p>
  
  <pre><code>int josephusIteration(int n,int k) {
      int a=1;
      for(int i=1;i&lt;=n;i++) {
          a=(a+k-1)%i+1;
      }
      return a;
  }
  </code></pre>
  
  <p>I couldn't understand its logic. Suppose n=5 and k=2.</p>
  
  <pre><code>i=1, a=1
  i=2, a=1
  i=3, a=3
  i=4, a=1
  i=5, a=3
  </code></pre>
  
  <p>Can anybody explain this by giving an example?</p>