/*
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
{
    int count=0;
    int distanceLeft=0;
    int distanceRight=0;
    int temp;
    int pos[]=new int[3];
    int[] people = new int[]{0, 0, 0, 0, 0, 0, 0, 0, 1};
    int length=people.length;
    
    public shiftPeople()
    {
        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("whether to move people left or right");

        System.out.println("\n****Calculating proximity of two people from left hand side****");
        
        for (int i=0;i<length;i++)
        {
            System.out.println("this is value of person " + i + " :" + people[i]);
            
            if (count==2)
            {
                break;
            }
            
            if (people[i]==1)
            {
                count++;
                pos[count]=i;
            }
        }
        
        if (count==2)
        {
            distanceLeft = pos[2]-pos[1]-1;
            System.out.println("Distance is:" + distanceLeft + "\n");
        }
        
        else
        {
            System.out.println("One person in the aisle");
        }
        
        count=0;
        System.out.println("\n****Calculating proximity of two people from right hand side****");
        
        for (int i=length-1;i>-1;i--)
        {
            System.out.println("this is value of person " + i + " :" + people[i]);
            
            if (count==2)
            {
                break;
            }
            
            if (people[i]==1)
            {
                count++;
                pos[count]=i;
            }
        }
        
        if (count==2)
        {
            distanceLeft = pos[2]-pos[1]-1;
            System.out.println("Distance is:" + distanceRight + "\n");
        }
        
        else
        {
            System.out.println("One person in the aisle");
        }
    }
    
    public void leftOrRight()
    {
        if (distanceLeft>distanceRight)
        {
            System.out.println("\n***VERDICT: " + "People to move right\n");
        }
        
        else if (distanceRight>distanceLeft)
        {
            System.out.println("\n***VERDICT: " + "People to move left\n");
        }
        
        else
        {
            System.out.println("\n***VERDICT: " + "no difference moving left or right\n");
        }
    }

    public void beginMove()
    {
        int moves=0;
        int counter=0;
        int [] original = new int [length];
        System.arraycopy(people, 0, original, 0, length);
        
        do
        {
            for (int i=length-1;i>0;i--)
            {
                if ((people[i-1]==0) && (people[i]==1))
                {
                    people[i]=0; people[i-1]=1;
                    moves++;
                    System.out.println(Arrays.toString(people));
                }
            }
            counter++;
            
        }while(counter<length-1);
        
        System.out.println("Total number of moves left: " + moves);
        System.out.println("\n"+Arrays.toString(people)+"\n\n");
        System.arraycopy(original, 0, people, 0, length);
        moves=0;
        counter=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));
                }
            }
            counter++;
            
        }while(counter<length-1);
        
        System.out.println("Total number of moves right: " + moves);
        System.out.println("\n"+Arrays.toString(people)+"\n\n");
    }
}