finished Josephus-Problem in C#, how to make this more efficient? using System;
using System.Collections.Generic;

namespace Function_JosephusSurvivor
{
    class Program
    {
        static void Main(string[] args)
        {
            //Debug purposes
            Console.WriteLine(JosSurvivor(7, 3));

            Console.ReadKey();
        }

        //Actual function:
        public static int JosSurvivor(int n, int k)
        {
            // n = amount of people(array size)
            // k = every k'th index will be yeeted

            List&lt;int&gt; list = new List&lt;int&gt;();
            int iCounter = 1; //always count till the size of the list
            int iWatcher = 1; //always count till the size of k
            for (int i = 1; i &lt;= n; i++)
            {
                list.Add(i);
            }

            do
            {

                if (iWatcher+1 == k)
                {
                    list.RemoveAt(iCounter);
                    iWatcher = 0;
                }

                iWatcher++;
                iCounter++;

                if (iCounter == list.Count) // -1 because index
                {
                    iCounter = 0;
                }
                else if (iCounter &gt; list.Count)
                {
                    iCounter = 1; //if one is jumped due to deleteting
                }

            } while (list.Count != 1);

            return list[0]; //winner
        }
    }
}


My question is: 

how could you make this more efficient? What could I do better?

I'm trying to submit this as my solution on a practicing page. Unfortunately, it always times out. Therefore I need to make this more efficient I believe.

Thanks in advance :)!

Also feel free to add fitting Tags :)!
List&lt;int&gt; list = new List&lt;int&gt;();
        int iCounter = 1; //always count till the size of the list
        int iWatcher = 1; //always count till the size of k
        for (int i = 1; i &lt;= n; i++)
        {
            list.Add(i);
        }



You can initialize list more elegantly:

List&lt;int&gt; list = Enumerable.Range(1, n).ToList();




For n = 2; k = 1 it runs infinitely?



Your counting and indexing seem ok, but are a little difficult to comprehend. It can be done a lot easier using modular operations:

static int JosSurvivor(int n, int k)
{
  List&lt;int&gt; positions = Enumerable.Range(1, n).ToList();
  int index = k % n - 1; // %n is important for k &gt; n

  while (positions.Count &gt; 1)
  {
    if (index &lt; 0) index += positions.Count;  // when (index + k) % poisitons.Count == 0 then index becomes -1 from the line below
    positions.RemoveAt(index);
    index = (index + k) % positions.Count - 1;
  }

  return positions[0];
}




Another approach building on the same principle is to maintain a fixed array of indices and then left-shift the reminder of the indices for each found index while decrement n by 1:

static int JosSurvivor(int n, int k)
{
  int[] positions = Enumerable.Range(1, n).ToArray();
  int index = k % n - 1;

  while (n &gt; 1)
  {
    if (index &lt; 0) index += n;
    Array.Copy(positions, index + 1, positions, index, n - index - 1);
    n--;
    index = (index + k) % n - 1;
  }

  return positions[0];
}


There seems to be a little performance gain - but not significant.



Yet another version that uses a fixed array of flags, that are set for each found index:

static int JosSurvivor(int n, int k)
{
  int counter = 0;
  int index = -1;
  int runner = 0;
  bool[] positions = new bool[n];

  while (counter &lt; n - 1)
  {
    runner += k;
    int temp = 0;
    do
    {
      index = (index + 1) % n;
    } while (positions[index] || ++temp &lt; k);

    if (runner &gt; 0 &amp;&amp; runner % k == 0)
    {
      positions[index] = true;
      counter++;
    }
  }

  for (int i = 0; i &lt; n; i++)
  {
    if (!positions[i])
      return i + 1;
  }

  throw new InvalidOperationException("No last position found");
}


This is very fast for small ks - even for large ns - but becomes slower when k increases.