This code is implemented and provided by Ajit from GNIOT college, Greater Noida
The Problem statement:
n number of people are standing in a circle. The person whose position is first,
has a sword.
He kills the next person who is standing next to him (whose position is second)
and passes the sword to the person whose position is third.
This process continues in the circular way till only one person remains alive.
What will be the position of that person who remains alive at last?
The Algorithm:
Step 1 : Get the total number of people in the circle.
Step 2 : We need to store them in a collection(I have used ArrayList).
Step 3 : We need a Infinite loop which terminates when only
one person remains in the collection.
Step 4 : Now we need to remove persons in a alternative way
a. first get the last person in the circle before using the removable operation
b. use a loop to remove persons in alternative way
( use i + 1, where i is the flow controller variable of inner loop)
c. after one circulation get the last man in the collection.
d. check that if last man before removal and last man after removable
are same then the first person must be removed
( make i = -1, because we use (i + 1) to remove) else make i = 0.
Step 5 : Now our code is ready to solve that problem...
########################################################
For example if input is 100 then output will be 73.
it means if there are 100 persons in a circle then position of the last man is 73.
########################################################
Note : Please save this class inside LastManStanding .java file in Eclipse IDE
import java.util.ArrayList;
import java.util.Scanner;
public class LastManStanding
{
public static int getLastMan(int person)
{
ArrayList<Integer> list = new ArrayList<Integer>();
for (int i = 1; i <= person; i++)
{
list.add(i);
}
int i = 0;
int lastPerson = 0;
int lastPersonAfterOneCirculation = 0;
while (true)
{
lastPerson = list.get(list.size() - 1);
while ((i + 1) < list.size())
{
System.out.println("Person number "+list.remove(i + 1)+" got killed");
i++;
}
lastPersonAfterOneCirculation = list.get(list.size() - 1);
if (lastPersonAfterOneCirculation == lastPerson)
{
i = -1;
}
else
{
i = 0;
}
if (list.size() == 1)
{
break;
}
System.out.print("Current status of remaining person "+list + "\n");
}
return list.get(0);
}
public static void main(String[] args)
{
Scanner s = new Scanner(System.in);
System.out.print("Enter the total no of person : ");
int person = s.nextInt();
int lastMan = LastManStanding.getLastMan(person);
System.out.println("Last Man : " + lastMan);
} // end of main
} // end of class
########################################################
Output :
Enter the total no of person : 6
Person number 2 got killed
Person number 4 got killed
Person number 6 got killed
Current status of remaining person [1, 3, 5]
Person number 3 got killed
Current status of remaining person [1, 5]
Person number 1 got killed
Last Man : 5
The Problem statement:
n number of people are standing in a circle. The person whose position is first,
has a sword.
He kills the next person who is standing next to him (whose position is second)
and passes the sword to the person whose position is third.
This process continues in the circular way till only one person remains alive.
What will be the position of that person who remains alive at last?
The Algorithm:
Step 1 : Get the total number of people in the circle.
Step 2 : We need to store them in a collection(I have used ArrayList).
Step 3 : We need a Infinite loop which terminates when only
one person remains in the collection.
Step 4 : Now we need to remove persons in a alternative way
a. first get the last person in the circle before using the removable operation
b. use a loop to remove persons in alternative way
( use i + 1, where i is the flow controller variable of inner loop)
c. after one circulation get the last man in the collection.
d. check that if last man before removal and last man after removable
are same then the first person must be removed
( make i = -1, because we use (i + 1) to remove) else make i = 0.
Step 5 : Now our code is ready to solve that problem...
########################################################
For example if input is 100 then output will be 73.
it means if there are 100 persons in a circle then position of the last man is 73.
########################################################
Note : Please save this class inside LastManStanding .java file in Eclipse IDE
import java.util.ArrayList;
import java.util.Scanner;
public class LastManStanding
{
public static int getLastMan(int person)
{
ArrayList<Integer> list = new ArrayList<Integer>();
for (int i = 1; i <= person; i++)
{
list.add(i);
}
int i = 0;
int lastPerson = 0;
int lastPersonAfterOneCirculation = 0;
while (true)
{
lastPerson = list.get(list.size() - 1);
while ((i + 1) < list.size())
{
System.out.println("Person number "+list.remove(i + 1)+" got killed");
i++;
}
lastPersonAfterOneCirculation = list.get(list.size() - 1);
if (lastPersonAfterOneCirculation == lastPerson)
{
i = -1;
}
else
{
i = 0;
}
if (list.size() == 1)
{
break;
}
System.out.print("Current status of remaining person "+list + "\n");
}
return list.get(0);
}
public static void main(String[] args)
{
Scanner s = new Scanner(System.in);
System.out.print("Enter the total no of person : ");
int person = s.nextInt();
int lastMan = LastManStanding.getLastMan(person);
System.out.println("Last Man : " + lastMan);
} // end of main
} // end of class
########################################################
Output :
Enter the total no of person : 6
Person number 2 got killed
Person number 4 got killed
Person number 6 got killed
Current status of remaining person [1, 3, 5]
Person number 3 got killed
Current status of remaining person [1, 5]
Person number 1 got killed
Last Man : 5