/*
Online Java - IDE, Code Editor, Compiler
Online Java is a quick and easy tool that helps you to build, compile, test your programs
online.
*/
import java.util.Arrays;

public class Main
{
    public static void main(String[] args)
    {
        shiftPeople sp = new shiftPeople();
        sp.leftOrRight();
    }
}

class shiftPeople
{
    boolean isStartZero=false;
    boolean hasMultiplePeople=false;
    boolean hasMinimumSinglePerson=false;
    
    int count=0;
    int distanceLeft=0;
    int distanceRight=0;
    int temp;
    int pos[]=new int[2];
    int singlePersonLocation;
    
    double averageMoves;
    int numPeopleMoving;
    int numberPeopleMovingForAverage;
    
    //int[] people = new int[]{1, 0, 1, 0, 1}; //PASS
     //int[] people = new int[]{0, 0, 1, 0, 1}; //PASS
    //int[] people = new int[]{1, 0, 1, 1,0, 1}; //PASS
    //int[] people = new int[]{1, 0,0, 1, 1,0, 1}; //PASS
    
    //int[] people = new int[]{1, 0,0, 1, 1,0,0,1,1,1}; //PASS
    
    
      //int[] people = new int[]{0, 1, 0, 1, 1};  //**PASS
      //int[] people = new int[]{0, 1, 0, 1};  //**PASS
      
      //int[] people = new int[]{1, 0, 0, 1};  //PASS
      //int[] people = new int[]{1, 0, 0, 1, 1, 1, 0, 0, 0};  //PASS
       
       //int[] people = new int[]{0, 1, 0, 0, 1, 1, 0, 0, 1};  //PASS
       int[] people = new int[]{0, 1, 0, 0, 1, 0, 1, 0, 1};  
      //int[] people = new int[]{1, 0, 0, 0, 1, 0, 1, 0, 1};  //PASS
       
       //int[] people = new int[]{0, 1, 0,0, 1, 0, 0, 0, 0, 0};  //PASS
       //int[] people = new int[]{0, 1, 0,0, 1, 0, 0, 0, 0, 1}; //PASS
      //int[] people = new int[]{0, 1, 0, 0, 1, 0, 1};  // PASS
      
      
     //int[] people = new int[]{0, 1, 0, 0, 1, 0};  //PASS
       //int[] people = new int[]{1,1};  //PASS
      //int[] people = new int[]{0,0};  //PASS
       //int[] people = new int[]{1,0,0}; //PASS
      //int[] people = new int[]{0,1,0};  //PASS
      //int[] people = new int[]{0,0,1};  //PASS
    
    int length=people.length;
    int numberPeople=countPeople();
    
    public int countPeople()
    {
        for (int i=0;i<length; i++)
        {
            if (people[i]==1)
            {
                numberPeople=numberPeople+1;
            }
        }
        return numberPeople;
    }
    
    public shiftPeople()
    {
        int farEnd;
	    int start=0;
	    String directionMove;
	    
	    System.out.println("Length of aisle is: " + length);
	    System.out.println("Number people is: " + numberPeople);
        System.out.println("This is the original aisle: " + Arrays.toString(people));
        
        System.out.println("A technique of closest neighbouring people from start and end of aisle will determine");
        System.out.println("the verdict (move people left or right)");
        System.out.println("A single person will be favoured movement direction in their closest side");
        System.out.println("****NOTE VERDICT IS NOT ALWAYS CORRECT DECISION");
        
        System.out.println("\n****Calculating proximity of two people from left hand side****");
        farEnd=length;
	    
	    if(start==0)
	    {
	        isStartZero=true;
	        directionMove="right";
	    }
	    else
	    {
	        directionMove="left";
	    }
	    
	    sideToSide(start,farEnd);
	    beginMove(start,farEnd,directionMove);
	    
	    isStartZero=false;
	    
	    System.out.println("\n****Calculating proximity of two people from right hand side****");
	    start=length-1;
	    farEnd=0;
	    
	    if(start==0)
	    {
	        isStartZero=true;
	        directionMove="right";
	    }
	    else
	    {
	        directionMove="left";
	    }
	    
	    sideToSide(start,farEnd);
	    beginMove(start,farEnd,directionMove);
    }
	
    public void sideToSide(int start, int otherEnd)
    {
        for (int i=start; isStartZero ? i<otherEnd : i>=otherEnd; i = isStartZero ? i + 1 : i - 1)
        {
            System.out.println("this is value of person " + i + " :" + people[i]);
            
            if (count==2)
            {
                break;
            }
            
            if (people[i]==1)
            {
                pos[count]=i;
                count++;
                
                if (count==1)
                {
                    singlePersonLocation=i;
                    hasMinimumSinglePerson=true;
                }
            }
        }
        
        if (count==2)
        {
            if (start==0)
            {
                distanceLeft = pos[1]-pos[0]-1;
                System.out.println("Distance is:" + distanceLeft + "\n");
            }
            else
            {
                distanceRight = pos[0]-pos[1]-1;
                System.out.println("Distance is:" + distanceRight + "\n");
            }
            hasMultiplePeople=true;
        }
        
        else if (count==1)
        {
            System.out.println("One person in the aisle: " + "(location:" + singlePersonLocation+")");
        }
        
        else
        {
            System.out.println("Empty aisle");
        }
        count=0;
    }
    
    public void leftOrRight()
    {
        if (hasMinimumSinglePerson)
        {
            if (distanceLeft>distanceRight)
            {
                System.out.println("\n******VERDICT: " + "People to move right\n\n");
            }
            else if (distanceRight>distanceLeft)
            {
                System.out.println("\n******VERDICT: " + "People to move left\n\n");
            }
            else
            {
                if (distanceLeft==0 && distanceRight==0 && !hasMultiplePeople)
                {
                    if ((length-1-singlePersonLocation)<singlePersonLocation)
                    {
                        System.out.println("\n******VERDICT: " + "Move single person to right\n\n");
                    }
                    else if (singlePersonLocation-0<(length-1)-singlePersonLocation)
                    {
                        System.out.println("\n******VERDICT: " + "Move single person to left\n\n");
                    }
                    else
                    {
                        System.out.println("\n******VERDICT: " + "no difference moving single left or right\n\n");
                    }
                }
                else
                {
                    System.out.println("\n******VERDICT: " + "no difference moving left or right\n\n");
                }
            }
        }
    }
    
    public boolean checkHasFinished(boolean hasConfirmedFinishedMoving, String directionMove)
    {
        int interestedLocation;
        
        for (int i=0;i<numberPeople;i++)
        {
            if (directionMove=="right")
            {
                interestedLocation=(length-(i+1));
            }
            else
            {
                interestedLocation=i;
            }
            
            if (!(people[interestedLocation]==1))
            {
                hasConfirmedFinishedMoving=false;
            }
        }
        return hasConfirmedFinishedMoving;
    }
    
    public void beginMove(int start, int otherEnd, String directionMove)
    {
        boolean hasFinishedMoving=true;
        boolean hasPersonPrevMove=false;
        boolean hasPreviousPerson=false;
        boolean hasImprovise=false;
        boolean hasIncreasedRowScan=false;
        boolean hasPersonMove=false;
        boolean hasPersonFinishedMoving=true;
        boolean hasPopulatedPreviousPerson=false;
        boolean hasPerformedThis=false;
        
        int moves=0;
        int [] original = new int [length];
        System.arraycopy(people, 0, original, 0, length);
        int k=0;
        
        int [] recordPersonMoved = new int[50];
        int numberScansRow=0;
        int currentNumberScansRow=0;
        int indexMove;
        
        do
        {
            numberScansRow++;
            hasFinishedMoving=true;
            
            for (int i=start;   isStartZero ? i<(otherEnd-1) : i>otherEnd;    i = isStartZero ? i + 1 : i - 1)
            {
                //System.out.println("VALUE OF I: " + i);
                
                if (directionMove.equals("left"))
                {
                    indexMove=(i-1);
                }
                else
                {
                    indexMove=(i+1);
                }
                 
                if ((people[indexMove]==0) && (people[i]==1))
                {
                    hasPersonMove=true;
                    
                    if (!hasPersonPrevMove && hasPersonMove)
                    {
                        if (i==(length-1))
                        {
                            k=1;
                        }
                        
                        for (int m=0; m<k;m++)
                        {
                            if (i==recordPersonMoved[m])
                            {
                                System.out.println("\n1START POS:  => " + Arrays.toString(people) + "   Position:" + 0 + " - " + "Position:" + (length-1));
                                System.out.println("Same person continuing move at position: " + i);
                                hasPersonFinishedMoving=false;
                                currentNumberScansRow=numberScansRow;
                                break;
                            }
                            
                            else if (m==(k-1))
                            {
                                System.out.println("\n2START POS:  => " + Arrays.toString(people) + "   Position:" + 0 + " - " + "Position:" + (length-1));
                                System.out.println("New Person has commenced movement at position: " + i);
                                hasPersonFinishedMoving=false;
                                numPeopleMoving=numPeopleMoving+1;
                                
                                System.out.println("*************************NUMBER PEOPLE MOVING: " + numPeopleMoving);
                                currentNumberScansRow=numberScansRow;
                            }
                        }
                        
                        if (k==0 && numberPeople!=0)
                        {
                            System.out.println("\n4START POS:  => " + Arrays.toString(people) + "   Position:" + 0 + " - " + "Position:" + (length-1));
                            System.out.println("New Person has commenced movement at position: " + i);
                            System.out.println("*************************NUMBER PEOPLE MOVING: " + numPeopleMoving);
                            hasPersonFinishedMoving=false;
                            numPeopleMoving=numPeopleMoving+1;
                                
                            currentNumberScansRow=numberScansRow;
                        }
                    }
                    
                    if (numberScansRow>currentNumberScansRow && !hasPersonFinishedMoving && !hasPopulatedPreviousPerson && !hasPerformedThis)
                    {
                        System.out.println("\n3START POS:  => " + Arrays.toString(people) + "   Position:" + 0 + " - " + "Position:" + (length-1));
                        System.out.println("New Person has commenced movement at position: " + i);
                        System.out.println("*************************NUMBER PEOPLE MOVING: " + numPeopleMoving);
                        hasIncreasedRowScan=true;
                        hasPopulatedPreviousPerson=true;
                    }
                    hasPerformedThis=false;
                    
                    if (hasPopulatedPreviousPerson)
                    {
                        people[i]=0; 
                        people[indexMove]=1;
            
                        if (!(people[i]==1 && people[i-1]==1))
                        {
                            System.out.println("fin             " + Arrays.toString(people)); 
                            hasImprovise=true;
                        }
                    }
                    
                    people[i]=0; 
                    people[indexMove]=1;
                    moves++;
                    
                    if (!hasIncreasedRowScan && !hasImprovise)
                    {
                        System.out.println("                " + Arrays.toString(people));    
                    }
                    hasIncreasedRowScan=false;
                    hasImprovise=false;
                    
                    hasPersonPrevMove=true;
                }
                
                else if (hasPersonMove && people[i]!=0)
                {   
                    if (numberScansRow>currentNumberScansRow && currentNumberScansRow!=0)
                    {
                        currentNumberScansRow=numberScansRow;
                        System.out.println("PREVIOUS PERSON FINISHED MOVING AT POSITION: " + 0);
                        hasPersonFinishedMoving=true;
                        System.out.println("REGISTERING UNIQUE ID FOR PREVIOUS PERSON MOVED ONTO: " + 0);
                        recordPersonMoved[k]= 0;
                        k++;
                        hasPreviousPerson=true;
                    }
                    else
                    {
                        System.out.println("PERSON FINISHED MOVING AT POSITION: " + i);
                        hasPersonFinishedMoving=true;
                    }
                    
                    hasPersonMove=false;
                    hasPersonPrevMove=false;
                    
                    if (numberScansRow>currentNumberScansRow && currentNumberScansRow!=0)
                    {
                        System.out.println("REGISTERING UNIQUE ID FOR PERSON MOVED ONTO: " + 0);
                    }
                    else
                    {
                        if(!hasPersonFinishedMoving)
                        {
                            System.out.println("PERSON FINISHED MOVING AT POSITION: " + i);
                        }
                        
                        if (hasPreviousPerson)
                        {
                            System.out.println("PERSON FINISHED MOVING AT POSITION: " + i);
                            hasPreviousPerson=false;
                        }
                        
                        System.out.println("REGISTERING UNIQUE ID FOR PERSON MOVED ONTO: " + i);
                    }
                    hasPopulatedPreviousPerson=false;
                    
                    recordPersonMoved[k]= i;
                    k++;
                }
            }
            hasFinishedMoving=checkHasFinished(hasFinishedMoving,directionMove);
            
        }while(!hasFinishedMoving);
        
        System.out.println("Number people moved: " + numPeopleMoving);
        k=0;
        
        if (hasPersonMove && people[0]==1 && numberPeople==1)
        {
            System.out.println("PERSON FINISHED MOVING AT POSITION: " + 0);
            hasPersonFinishedMoving=true;
        }
            
        System.out.println("\n\n---------------------------------------------------------");
        System.out.println("Total number of moves " +directionMove +": "+ moves);
        System.out.println("FINISHED MOVEMENT:"+ Arrays.toString(people));
        System.out.println("Number people moving for average: " + numPeopleMoving);
        
        averageMoves = (double)moves/(double)numPeopleMoving;
        System.out.println("AVERAGE MOVE PER PERSON: " + averageMoves);
        System.out.println("---------------------------------------------------------");
    }
}