Answer to Question #230746 in Java | JSP | JSF for joe

Question #230746

D is hosting a lunch and R is incharge of the sitting plan D’s dining table is an infinite row of seats with D at one end. Each of D’s guests represents a alphabet (a to z). D want guests to be seated such that the resulting string formed from the alphabets is smallest He didn’t tell total number of guests.

The rules:-

● Seats are allotted in sequential order, a guest who arrives later must be further from D

● An incoming guest must be assigned a seat, as soon as he arrives

● Once a guest take seat, you can’t ask them to move to some other seat.Even if

the seat in front of him is empty.

● But you can make a guest disappear, D has allowed to make at most k of his guests disappear but you can’t remove more than k guests.

● No seat should remain empty b/w any 2 guests or b/w guest and D, a series of empty seats would be in the end. You are R, design a code using an efficient data structure that will follow to keep track of which guest to assign which seat.


1
Expert's answer
2021-08-30T01:35:51-0400
import java.util.*;
class Main{
static Vector<Integer> restore(int arr[], int N)
{
    Vector<Integer> result = new Vector<>();
 
    HashMap<Integer,
            Integer> m = new HashMap<Integer,
                                      Integer>();
    for (int i = 0; i < N; i++)
    {
 
        if (m.containsKey(arr[i]) &&
            m.get(arr[i]) == 0)
        {
 
            result.add(arr[i]);
 
            if(m.containsKey(arr[i]))
            {
                m.put(arr[i], m.get(arr[i]) + 1);
            }
            else
            {
                m.put(arr[i], 1);
            }
        }
        else
            m.put(arr[i], 0);
    }
 
    return result;
}
 
static void printResult(Vector<Integer> result)
{
ult    for (int i = 0; i < result.size(); i++)
        System.out.print(result.get(i) + " ");
}
 
public static void main(String[] args)
{
    int arr[] = { 2, 13, 1, 4, 13, 24, 2, 2 };
 
    int N = arr.length;
 
    printResult(restore(arr, N));
}
}

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