Please solve the number quiz 👇
“113 people are standing in a circle in order 1 to 113 Number 1 has a sword. He kills the next person, i.e. #2 and passes the sword to next, i.e. #3, who kills the next person, i.e. #4 and passes the sword to next, #5, who then kills the next, i.e. #6. This continues until only one is left standing. Who is the last man standing?”
Code:
package bostest;
import java.util.ArrayList;
import java.util.List;
public class KillingAlternate {
public KillingAlternate() {
int maxno = 113;
List<Integer> arrList = new ArrayList<Integer>();
for (int i = 1; i <= maxno; i++) {
arrList.add(i);
}
int ii = 0;
long cnt = 0;
List<Integer> removeList = new ArrayList<Integer>();
while (true) {
if (arrList.size() == 1) {
break;
}
if (ii >= arrList.size()) {
ii = 0;
arrList.removeAll(removeList);
}
if (cnt % 2 == 1) {
removeList.add(arrList.get(ii));
}
cnt++;
ii++;
}
System.out.println("Answer Survivor is =" + arrList);
}
public static void main(String[] args) {
new KillingAlternate();
}
}
--------------------------------------------------------------------------------------------
Answer Survivor is =[99]