Answer to Question #230572 in Java | JSP | JSF for kejriwal

Question #230572

In the country of WeirdLand, there is only one barber shop. In the shop there are 3

barbers, Alpha, Beta and Gamma, who are not always efficient as employees. All the customers are seated in a waiting area, each customer has a card which has a unique integral ID. For calling next person from the waiting area each barber has his own weird way:

Alpha: He will call either the person with least ID from the waiting area of the one with max ID. Min or Max is decided randomly by him

Beta: He will call the kth smallest ID, (Kth ID when all IDs are arranged in ascending order). K is chosen randomly(1 <= K <= Total persons)

Gamma: He will always choose the median ID. If there are ‘n’ people in the waiting area, the median will be defined as (n+1)/2 th ID when all of them are arranged in ascending order. (For both odd and even ‘n’ )

Your task is to design a data structure which should support the query of each of the three

barbers. The data structure should be highly efficient.


1
Expert's answer
2021-08-29T00:57:07-0400
int n;
int customers_id[n];
int i1=0;
int a_call_id1=customers_id[0];
int a_call_id2=customers_id[0];
int b_call_id1=customers_id[0];
int g_call_id=0;
Alpha{
    while(true):
        if(customers_id[i]<a_call_id1):
            a_call_id1=customers_id[i];
        if(customers_id[i]>a_call_id2):
            a_call_id2=customers_id[i];
}


Beta{
    while(true):
        if(customers_id[i]<b_call_id1):
            b_call_id1=customers_id[i];
}


Gamma{
    while(true):
            g_call_id1=median.(customers_id);
}

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog
APPROVED BY CLIENTS