Search code examples
javaiojava.util.scannerdata-manipulation

How to solve a two elevator scenario in java program


Below is the requirement scenario - This is a hackerearth problem.

There are 7 floors in a block and only 2 lifts. Initially Lift A is at the ground floor and Lift B at the top floor. Whenever someone calls the lift from N th floor, the lift closest to that floor comes to pick him up. If both the lifts are at equidistant from the N th floor, them the lift from the lower floor comes up.

I am trying to write java code for the above scenario but couldn't get the expected result

I have initialized integers for the position of both lifts and an integer for floor calling. I used if condition with operators to find the position and printed which lift will reach the user. What can be the right programming procedure for the problem statement. Kindly help. TIA.

import java.util.*;

class TestClass {
    public static void main(String args[] ) throws Exception {

        Scanner s = new Scanner(System.in);
        int count=s.nextInt();
        int[] fc=new int[count];
        int posA=0;
        int posB=7;


        for(int i=0;i<count;i++){
            fc[i]=s.nextInt();

            if((fc[i]-posA)<(posB-fc[i])){
                posA=fc[i];
                System.out.println("A");
            }

            if((fc[i]-posA)==(posB-fc[i])){
                if(posA<posB){
                    posA=fc[i];
                    System.out.println("A");
                }
                else if(posA>posB){
                    posB=fc[i];
                    System.out.println("B");
                }
            }
            else{
                posB=fc[i];
                System.out.println("B");
            }            
        }
    }
}

My input, current & expected output are mentioned below.

Input - 10 0 6 4 1 1 2 4 0 3 1

Current output - A B B B A B B B A B

Expected output - A B B A A A B A B A


Solution

  • You just missed, that a distance should be ALWAYS a positive integer.

    Use Math.abs() when calculating distances.

    Plus there is an else missing after the first if block. Plus the if(posA>posB) is too much because it filters out posA==posB.

    My code:

    package de.test.lang.stackexchange;
    
    public class Lifts {
    
        public static void main(String args[]) throws Exception {
            int[] fc = new int[]{0, 6, 4, 1, 1, 2, 4, 0, 3, 1};
            int posA = 0;
            int posB = 7;
    
            for (int reqPos: fc) {
                final int distA = Math.abs(reqPos - posA);
                final int distB = Math.abs(reqPos - posB);
    
                if (distA < distB) {
                    posA = reqPos;
                    System.out.println("A");
                } else if (distA == distB) {
                    if (posA < posB) {
                        posA = reqPos;
                        System.out.println("A");
                    } else {
                        posB = reqPos;
                        System.out.println("B");
                    }
                } else {
                    posB = reqPos;
                    System.out.println("B");
                }
            }
        }
    }
    

    Output: A B B A A A B A B A

    (BTW: nice homework...)