Answer:
The code below addresses the problem statement with appropriate comments for explanation.
Explanation:
public class Stall
{
public static void main(String[] args)
{
boolean[] arr = new boolean[10];
while(hasFreeSpots(arr)) // as long as there is a free slot
{
arr[position(arr)] = true;
// position(arr) gets the position of slot a person would most likely enter at(view problem statement)
// setting that position true(position occupied) by arr[position(arr)]
System.out.println(stallView(arr)); // printing current state of the stall using 'X' and '_'
}
}
/**
* Find the empty position that is in the middle of the sub-array of largest group of empty spots
* @param arr an array of boolean values
* @return returns an an integer value. Optimal position to be occupied.
*/
public static int position(boolean[] arr)
{
int first = 0; // first stores the starting position of "longest sequence of unoccupied places"
int last = 0; // last stores the ending position of "longest sequence of unoccupied places"
int temp1 = 0; // temp1 stores the starting position of "current sequence of unoccupied places"
int temp2 = 0; // temp2 stores the ending position of "current sequence of unoccupied places"
for(int i = 0; i < arr.length; i++) // for each slot in the Stall
{
if(!arr[i]) // if slot i is not occupied
{
temp1 = i; // store starting position of "current sequence of unoccupied places" in temp1
while(i < arr.length && !arr[i] ) // if i is a valid index and as long as this slot is not occupied increment i
{
i++;
}
// we break out of the loop either if i is invalid index(reached end of the array)
// or if the slot is occupied by a person already
temp2 = i; // store ending position of "current sequence of unoccupied places" in temp2
if(Math.abs(first - last) < Math.abs(temp1 - temp2))
// if current sequence is longer than longest sequence, update it
{
first = temp1;
last = temp2;
}
}
}
return (first + last) / 2; // return middle index of first and last
}
/**
* Checks if there are empty spots in an array, basically checks if there is a false boolean value
* thats representing an empty spot in an array
* @param arr the input array, of boolean values
* @return returns a boolean value, true if there are false elements inside the array
*/
public static boolean hasFreeSpots(boolean[] arr)
{
boolean rtn = true; // initializing rtn to 'true'
for(int i = 0; i < arr.length; i++) // for every slot in the Stall
{
if(!arr[i]){rtn = false;} // sets rtn to false if a free spot found
}
return !rtn; // returns true if a free spot found and returns false if no free spot found
}
/**
* X for true, _ for false. Representing occupied(X) and free stalls(_)
* @param arr a boolean array
* @return returns a string representation if the input
*/
public static String stallView(boolean[] arr)
{
String str = ""; // initializing str to be an empty String
for(int i = 0; i < arr.length; i++) // for every slot in Stall
{
if(arr[i]){str += " X";} // Add X to str if slot is occupied
else {str += " _";} // prints _ to str if slot not occupied
}
return str; // return str(pictorial representation of Stall with 'X', '_')
}
}