1000 men in circle

1000 men are standing in a circle and we remove every alternate person from the circle in circular fashion 1 by 1

by Mubasshir Pawle

JavaScript

//input
var n = 100;

//init array
var people = [];
for (var i = 0; i < n; i++) {
  people[i] = i + 1;
}

//start eliminate
while (people.length > 1) {
  for (var i = 0; i < people.length; i = i + 2) {
    //check if next element is last +1, then remove first
    if ((i + 1) === people.length) {
      //making rmoved element as 0 
      people[0] = 0;
      continue;
    }
    //making rmoved element as 0 
    people[i + 1] = 0;
  }
  //remove 0
  people = people.filter(function(element) {
    return element > 0;
  });
}

alert('Last person:' + people);