# Javascript Algorithms and Data Structures Challenge: Day 4

Here is something fun.

Given a circle of N people, they are sitting side by side and forming a circle, and they are numbered from 1 to N, for example, the first person is 1 and the last is 100; Starting with the first person, they are called 1, 2, 3; 1,2,3 and 1,2,3 the person being called 3, is removed from the circle; this continues until last two people are left, what are their original numbers?

In order to solve this problem, we can start with a line instead of a circle, assuming 100 people sit in a line, the first person removed is #3 and the last person removed is #99; the last person is #100.

To form a circle, you can drag that last person #100 to the beginning of the line, and the cycle will begin with #100.

Evidently, this is recursion problem, you have to manually move the last person in the line from the tail to the head in order to start the line counting again.

Here is the code.

const arr = [];

for (let i = 0; i < 100; i++) {

arr.push(i + 1);

}//You can use fill method to create the array, but I don't want to // memorize too many methods.function cycle(array) {

let last;

if (array.lenghth === 2) {

return array;

}

let short = array.filter((v, i, r) => {

if ((i + 1) % 3 === 0) {

last = i;

return false; } else {

return true;

}

})

if (!last) {

return short ;

}

for (let i = 0; i < (array.length - 1 - last); i++) {

let tmp = short.pop(); //pop returns the actual ele.

short.unshift(tmp);

}

cycle(short);

}

console.log(remove(arr))

//[58,91]

Someone else came up with another solution, which worked well too, but I didn’t quite understand the code, so please explain it to me.

`var a = new Array(100).fill(0).map(function (ele, index) { return index + 1 });`

var numberOff = 0;

var index = 0;

while (a.length > 2) {

numberOff++;

if (numberOff !== 3) {

index++;

} else {

numberOff = 0;

var number = a.splice(index, 1);

console.log(number); // [3], [6], [9], …

}

if (index >= a.length) index = 0;

}

console.log(a); // [58, 91]