Created
May 10, 2020 06:03
-
-
Save LTroya/12e5410ff8a99aa510535e862b5b7a05 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.util.ArrayList; | |
import java.util.Scanner; | |
public class Main { | |
public static void main(String args[]) { | |
Integer[] params = getParams(); | |
int size = params[0]; | |
int interval = params[1] + 1; | |
int lastIndex = 0; | |
ArrayList<Integer> children = createArrayList(size); | |
while (children.size() >= 2) { | |
int indexToDestroy = findNextIndexToRemove(interval, children.size() - 1, lastIndex); | |
children.remove(indexToDestroy); | |
lastIndex = indexToDestroy; | |
} | |
for (Integer value : children) { | |
System.out.println("Value: " + value); | |
} | |
} | |
/** | |
* Find the next index to remove from the array | |
* | |
* @param interval to remove | |
* @param limit the max limit allowed to restore the currentIndex | |
* @param initIndex last position removed to start from it the next iteration | |
* @return next index to remove | |
*/ | |
public static int findNextIndexToRemove(int interval, int limit, int initIndex) { | |
int nextIndex = 0, counter = 1, currentIndex = initIndex; | |
// I use while true because I don't know when to break it. So I will do it manually | |
while (true) { | |
if (currentIndex > limit) { | |
currentIndex = 0; | |
} | |
nextIndex = currentIndex++; | |
// If the round to iterate is the same at the interval, | |
// break the loop | |
if (counter == interval) break; | |
counter++; | |
} | |
return nextIndex; | |
} | |
/** | |
* Get array size and interval to calculate the final value | |
* | |
* @return Integer[] | |
*/ | |
public static Integer[] getParams() { | |
Scanner reader = new Scanner(System.in); | |
String input = null; | |
int interval = -1, size = -1; | |
do { | |
System.out.print("Ingresar numero de ninos e intervalo de eliminacion => "); | |
input = reader.nextLine(); | |
String[] values = input.split(" "); | |
// Validate if the user enter a parseable value | |
if (values.length == 2) { | |
size = parseStringValue(values[0]); | |
interval = parseStringValue(values[1]); | |
} | |
} while ((size < 1 || size > 1000) || (interval < 1 || interval > 100)); | |
return new Integer[]{size, interval}; | |
} | |
/** | |
* Create an initialize an array list with the provided size | |
* | |
* @param size create a arraylist of the given size | |
* @return list with values | |
*/ | |
public static ArrayList<Integer> createArrayList(int size) { | |
ArrayList<Integer> list = new ArrayList<>(); | |
for (int index = 1; index <= size; index++) { | |
list.add(index); | |
} | |
return list; | |
} | |
/** | |
* Check if a string is a valid number. Otherwise return -1 since | |
* it is a invalid value | |
* | |
* @param value to parse, if it not a valid number, it will return -1, an invalid value for the system | |
* @return a number | |
*/ | |
public static int parseStringValue(String value) { | |
return isNumeric(value) ? Integer.parseInt(value) : -1; | |
} | |
/** | |
* Check if a string is a number | |
* | |
* @param strNum number to validate | |
* @return true if the number is valid, otherwise false | |
*/ | |
public static boolean isNumeric(String strNum) { | |
if (strNum == null) { | |
return false; | |
} | |
try { | |
double d = Integer.parseInt(strNum); | |
} catch (NumberFormatException nfe) { | |
return false; | |
} | |
return true; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment