/*
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();
        sp.beginMove();
    }
}

class shiftPeople
{
    boolean hasRegisteredMovementToZero;
    boolean hasPersonPrevMove;
    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;
    boolean hasPersonMove=false;
    double averageMoves;
    int numPeopleMoving;
    int numberPeopleMovingForAverage;
    
    int[] people = new int[]{0, 0, 1, 1};
    //int[] people = new int[]{1, 0, 0, 1};
     //int[] people = new int[]{1, 0, 0, 1, 1, 1, 0, 0, 0};
    //int[] people = new int[]{0, 1, 0, 0, 1, 0, 1, 0, 1};
      //int[] people = new int[]{0, 1, 0,0, 1, 0, 0, 0, 0, 1};
      
          //int[] people = new int[]{0,1,1}; //PASS
         //int[] people = new int[]{1,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;
    
    public shiftPeople()
    {
        int farEnd;
	    int start=0;
	    
        System.out.println("Length of aisle is: " + length);
        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;
	    }
	    
	    sideToSide(start,farEnd);
	    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;
	        System.out.println("SH");
	    }
	    
	    sideToSide(start,farEnd);
    }
	
    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 void beginMove()
    {
        
        
        int moves=0;
        int counter=0;
        int [] original = new int [length];
        System.arraycopy(people, 0, original, 0, length);
        int k=0;
        int endLocation;
        
        int [] recordPersonMoved = new int[length];
        int numberScansRow=0;
        int currentNumberScansRow=0;
        boolean hasProcess=false;
        boolean hasProcess1=false;
        boolean hasSamePerson=false;
        int iValue=0;
        
        do
        {
            numberScansRow++;
            System.out.println("\n-----STARTING NEW ROW SCAN: " + numberScansRow + " --------------------------");
            
            for (int i=length-1;i>0;i--)
            {
                
                //System.out.println("VALUE I: " + i);
                
                if ((people[i-1]==0) && (people[i]==1))
                {
                    
                            
                    
                    hasPersonMove=true;
                    //System.out.println("WHAT IS: " + hasPersonPrevMove);
                    //remember that i-1 is moving to the left hand side
                    
                    
                    //this bit is a bit tricky, since we don't want it to enter in here if k=0
                    //i.e when there have been no people who have moved
                    //at same time we want it to provide meaningful information for the first person moving
                    //if (!hasPersonPrevMove && hasPersonMove && k!=0)
                    //So I have removed the check for k!=0
                    //but then this also means that I would need to change the condition in for statement to
                    //m<=k  This is not ideal and will cause ArrayIndexOutOfBoundsException
                    
                    if (!hasPersonPrevMove && hasPersonMove)
                    {
                        //System.out.println("TEST");
                        
                        //This resolves the issue
                        if (i==(length-1))
                        {
                            //System.out.println("CHANGE");
                            k=1;
                        }
                        
                        for (int m=0; m<k;m++)
                        {
                            //System.out.println("SSSSS");
                            if (hasSamePerson && m==0)
                            {
                                
                                System.out.println("SAME PERSON FINISHED MOVING AT POSITION: " + Math.abs((iValue-i)));
                                
                                for (int p=0; p<k;p++)
                                {
                                    System.out.println("\nCHECKING TO SEE IF PERSON AT UNIQUE ID: " + i + " has already been moved");
                                    System.out.println("VALUE IN STORED INDEX OF PERSON FINISHED: " + recordPersonMoved[m]);
                                    
                                    if(i==recordPersonMoved[p])
                                    {
                                        p=k-1;
                                        hasSamePerson=true;
                                        //iValue=i;
                                    }
                                }
                                if (hasSamePerson)
                                {
                                    System.out.println("-EVER2-------REGISTERING UNIQUE ID FOR MOVING PERSON AT: " + Math.abs((iValue-i)));
                                    //hasProcess=true;
                                    recordPersonMoved[k]= Math.abs((iValue-i));
                                    
                                    if (Math.abs((iValue-i))==0)
                                    {
                                        hasRegisteredMovementToZero=true;
                                    }
                                    k++;
                                }
                                
                                hasSamePerson=false;
                            }
                            
                            System.out.println("\nCHECKING TO SEE IF PERSON AT UNIQUE ID: " + i + " has already been moved");
                            System.out.println("VALUE IN STORED INDEX OF PERSON FINISHED: " + recordPersonMoved[m]);
                            
                            if (i==recordPersonMoved[m])
                            {
                                System.out.println("\nSame person continuing move at position: " + i);
                                System.out.println("\n1START POS:  =>" + Arrays.toString(people) + "   Position:" + 0 + " - " + "Position:" + (length-1));
                                //numPeopleMoving=numPeopleMoving-1;
                                m=k-1;
                                hasSamePerson=true;
                                iValue=i;
                            }
                            
                            else if (m==(k-1))
                            {
                                
                                hasProcess=false;
                                hasProcess1=false;
                                System.out.println("\n2START POS:  =>" + Arrays.toString(people) + "   Position:" + 0 + " - " + "Position:" + (length-1));
                                System.out.println("-----------------------------------------------------------New Person has commenced movement at position: " + i);
                                numPeopleMoving = numPeopleMoving + 1;
                                
                                currentNumberScansRow=numberScansRow;
                                //break;
                            }
                            //System.out.println("HERE1");
                            
                        }
                        //System.out.println("HERE2");
                        
                    }
                    //System.out.println("HERE3");
                    
                    //System.out.println("^^^^^^^^^^^^^^^^^^^^FOR SAME PERSON ALSO^^^^^^^^^^^^^");
                    
                    
                if (!hasProcess && !hasProcess1)
                {
                    System.out.println("k: " + k);
                    if (/*numPeopleMoving==0 && */k==0)
                    {
                        System.out.println("\n3START POS:  =>" + Arrays.toString(people) + "   Position:" + 0 + " - " + "Position:" + (length-1));
                        numPeopleMoving=numPeopleMoving+1;
                        
                        if (numberScansRow>currentNumberScansRow && currentNumberScansRow!=0)
                    {
                        System.out.println("PERSON FINISHED MOVING AT POSITION: " + 0);
                        hasProcess1=true;
                    }
                    else
                    {
                        System.out.println("1PERSON FINISHED MOVING AT POSITION: " + (i-1));
                        hasProcess1=true;
                    }
                }     
                        
                        
                        
                        
                        
                        
                        
                        
                    }
                    
                    /*
                    if (numberScansRow>currentNumberScansRow && currentNumberScansRow!=0)
                    {
                        System.out.println("\n4START POS:  =>" + Arrays.toString(people) + "   Position:" + 0 + " - " + "Position:" + (length-1));
                    }
                    */
                    
                    people[i]=0; 
                    people[i-1]=1;
                    moves++;
                  System.out.println("              " + Arrays.toString(people));
                    
                    
                    
                    hasPersonPrevMove=true;
                    
                }
                
                //System.out.println("REACH HERE!!!!!!!!!!!!!!!!!!!!!!!!!!!!");
                //System.out.println("FINISHED------------------ MOVEMENT:"+ Arrays.toString(people));
                
                if (hasSamePerson)
                {
                
                //System.out.println(Arrays.toString(people));
                hasProcess1=true;
                hasProcess=true;
                //hasSamePerson=false;
                
                }
                
                //System.out.println(hasPersonMove);
                //System.out.println("PEOPLE[i]: " + people[i]);
                if (hasPersonMove && people[i]!=0 || people[i-1]==0)
                {
                    
                    //we need a condition here since if a person was at location 1
                    //and moved to location 0, the  i for loop would have commenced again
                    //and hence lost track of the finish point
                    if (!hasProcess1)
                    {
                    if (numberScansRow>currentNumberScansRow && currentNumberScansRow!=0)
                    {
                        System.out.println("PERSON FINISHED MOVING AT POSITION: " + 0);
                        hasProcess1=true;
                    }
                    else
                    {
                        System.out.println("1PERSON FINISHED MOVING AT POSITION: " + i);
                        hasProcess1=true;
                        
                        //if (numPeopleMoving>0 && k!=numPeopleMoving)
                        //{
                         //   System.out.println("ADJUST numPeopleMoving: " + k);
                          //  numPeopleMoving=k;
                        //}
                    }
                    }
                    
                    hasPersonMove=false;
                    hasPersonPrevMove=false;
                    //we take the position of where person has finished moving
                    //since we know if he hits an obstruction of a person on his left,
                    //he will stop moving
                    //and when all people on his left have cleared his path and reached most left hand side
                    //person will start his movement again
                    
                    if (!hasProcess)
                    {
                        System.out.println(numberScansRow);
                        System.out.println(currentNumberScansRow);
                    if (numberScansRow>currentNumberScansRow && (currentNumberScansRow!=0 || k==0))  //&& /*currentNumberScansRow!=0*/)
                    {
                        System.out.println("------------REGISTERING UNIQUE ID FOR MOVING PERSON AT: " + 0);
                        
                        hasProcess=true;
                        recordPersonMoved[k]= 0;
                        hasRegisteredMovementToZero=true;
                    }
                    else
                    {
                        System.out.println("-EVER1-------REGISTERING UNIQUE ID FOR MOVING PERSON AT: " + i);
                        hasProcess=true;
                        recordPersonMoved[k]= i;
                        if (i==0)
                        {
                            
                            hasRegisteredMovementToZero=true;
                        }
                    }
                    }
                    
                    k++;
                    //numPeopleMoving=numPeopleMoving+1;
                    
                    //we need to keep a track of the position of i when a new position makes a move
                    //since when we perform check to see if person at unique ID has made a move
                    //we can not rely on i.
                    //we need to make reference of the last i of where it found a person
                   
                   
                }
                //if (hasProcess && hasProcess1)
                //{
                //break;
                //}
            }
            
            
            counter++;
            k=0;
        }while(counter<length-1);
        
           
            //System.out.println("INSIDE HERE");
            k=0;
            
            System.out.println(people[0]);
            System.out.println(people[1]);
            
            /*
            //We know that once we reach end and that there is a person, as long as there is no movement registered for person
            //at the end, we must include this person as part of the average
            if (people[0]==1 && !hasRegisteredMovementToZero)
            {
                numPeopleMoving=numPeopleMoving+1;
            }
            */
        
        
        System.out.println("Total number of moves left: " + moves);
        System.out.println("FINISHED MOVEMENT:"+ Arrays.toString(people));
        
        System.out.println(hasRegisteredMovementToZero);
        
        
        if (k!=0 && numPeopleMoving==0)
        {
            numPeopleMoving=k;
        }
        
        
        if (!hasRegisteredMovementToZero && people[0]==1 && /*(numPeopleMoving!=0 || k==0)*/ numPeopleMoving!=0)
        {
            System.out.println("First person on far side has had no movement, hence adding 1 to number people");
            numPeopleMoving=numPeopleMoving+1;
        }
        
        
        System.out.println("Number people moved: " + numPeopleMoving);
        
        //System.out.println("Number people moving for average: " + numberPeopleMovingForAverage);
        averageMoves = (double)moves/(double)numPeopleMoving;
        
        System.out.println("AVERAGE MOVE PER PERSON: " + averageMoves+"\n\n");
        
        System.arraycopy(original, 0, people, 0, length);
        moves=0;
        counter=0;
        numPeopleMoving=0;
        

/*

	do
        {
            for (int i=0;i<length-1;i++)
            {
                if ((people[i+1]==0) && (people[i]==1))
                {
                    people[i]=0;
                    people[i+1]=1;
                    moves++;
                    System.out.println(Arrays.toString(people));
                    hasPersonMove=true;
                }
                if (hasPersonMove)
                {
                    numPeopleMoving=numPeopleMoving+1;
                }
                hasPersonMove=false;
            }
            
            counter++;
            
        }while(counter<length-1);
        
        System.out.println("\nTotal number of moves right: " + moves);
        System.out.println("FINISHED MOVEMENT:"+Arrays.toString(people));
        averageMoves = moves/numberPeopleMovingForAverage;
        System.out.println("Number people moved: " + numPeopleMoving);
        System.out.println("AVERAGE MOVE PER PERSON: " + averageMoves+"\n\n
        */
    }
}