a program in java that asks the user for the number of seats
a program in java that asks the user for the number of seats and then prints out diagrams (one diagram per line) in this format each time a new person occupies a seat. In the above example, you would print a total of 11 diagrams (one per line), the empty diagram and then 10 additional diagrams.
_ _ _ _ _ _ _ _ _ _
_ _ _ _ _ X _ _ _ _
The next person will be in the middle of the empty area at the left (left has more seats).
 _ _ X _ _ X _ _ _ _
The next person will be in the section to the right:
 _ _ X _ _ X _ _ X _
_ X X _ _ X _ _ X _
 _ X X _ X X _ _ X _
 _ X X _ X X _ X X _
 X X X _ X X _ X X _
 X X X X X X _ X X _
 X X X X X X X X X _
 X X X X X X X X X X
Solution
below is the code. i will add further comments.
 
/* package codechef; // don\'t place package name! */
import java.util.*;
 import java.lang.*;
 import java.io.*;
/* Name of the class has to be \"Main\" only if the class is public. */
 class Codechef1
 {
    public static void main (String[] args) throws java.lang.Exception
    {
    Scanner sc=new Scanner(System.in);
    System.out.println(\"Enter the number of seats :\");
 int n=sc.nextInt();
 //String line=\"_ _ _ _ _ _ _ _ _ _\";
 StringBuilder line = new StringBuilder(\"_\");
 for(int i=0;i<n-1;i++)
 {
 line.append(\" _\");
 }
   
 for(int i=1;i<=n;i++)
 {
 int x=getPosition(0,n-1,i,line.toString());
 line.setCharAt((x*2), \'X\');
 System.out.println(line);
 }
 // System.out.println(line);
    }
   
    public static int getPosition(int low,int high,int i,String line)
    {
    if(i==0)
    {
    return -1;
    }
    if(low==high)
    {
    return low;
    }
    int mid=((low+high)/2);
    if((high-low)%2!=0)
    {
        mid=mid+1;
    }
    int x=-1;
    if(line.charAt(mid*2)!=\'X\')
    {
    return mid;
    }
    else
    {
        int leftMid=(low+mid-1)/2;
        if((mid-1-low)%2!=0)
        {
            leftMid++;
        }
        int rightMid=(mid+1+high)/2;
        if((high-(mid+1))%2!=0)
        {
            rightMid++;
        }
   
        if(line.charAt(leftMid*2)!=\'X\' && line.charAt(rightMid*2)!=\'X\')
    {
            if(high-mid<=mid-low)
            {
                x=getPosition(low,mid-1,i--,line);
            }
            else //high-mid>mid-low
            {
                x=getPosition(mid+1,high,i--,line);
            }
       
      
    }
        else if(line.charAt(leftMid*2)!=\'X\')
        {
            x=getPosition(low,mid-1,i--,line);
        }
        else if(line.charAt(rightMid*2)!=\'X\')
        {
            x=getPosition(mid+1,high,i--,line);
        }
        else
        {
            x=getPosition(low,mid-1,i--,line);
        }
    }
   
    return x;
    }
 }


