Posted by: helpjava11 June 10, 2014
NEW PUZZLE for Sajha Intellects Day 6
Login in to Rate this Post:     1       ?         Liked by
Perfect answer by NAS: +2

Here is my version of code:

import java.util.ArrayList;
import java.util.List;


public class jpt1 {
static int toalPeople=1000;
static int result=0;
static int x;
static int count = 1000;
public static void main(String[] args) {
List result= numberLeft(toalPeople);
System.out.println("last remaining person: "+ result);

}

static public List numberLeft(int x){
ArrayList list = new ArrayList();
for (int i = 1; i <= toalPeople; i++) {
list.add(i);

}
System.out.println("list "+list);
while(count>1){
for (int i = 0; i < list.size()-1; i++) {
list.remove(i+1);
}
System.out.println(" Remaining people : "+list.size());
count = list.size();
}
System.out.println();


return list;
}
}
Output:
Remaining people : 500
Remaining people : 250
Remaining people : 125
Remaining people : 63
Remaining people : 32
Remaining people : 16
Remaining people : 8
Remaining people : 4
Remaining people : 2
Remaining people : 1

last remaining person: [1]
Read Full Discussion Thread for this article