There are 7 floors in BH3 and only 2 lifts. Initially, Lift A is on the ground floor, and Lift B at the top floor. Whenever someone calls the lift from the Nth floor, the lift closest to that floor comes to pick him up. If both the lifts are at equidistant from the Nth floor, then the lift from the lower floor comes up.
Example:
Input: n = 3
Output: A
Approach
C++
#include <bits/stdc++.h>using namespace std;int a, b;void liftQueires(int n){int x = abs(n - a);int y = abs(b - n);if (x <= y){cout << "A\n";a = n;}else{cout << "B\n";b = n;}}int main(){a = 0, b = 7;int n = 3;liftQueires(n);return 0;}
No comments:
Post a Comment