Answer to Question #229847 in Java | JSP | JSF for Rohan

Question #229847

Dumledore’s dining table can be thought of as an infinite row of seats with Dumbledore at one end. Each of Dumbledore’s guests represents a character of English alphabet (a to z).Dumbledore wants his guests to be seated such that the resulting string formed from the alphabets should be lexicographically smallest (See examples for reference).● Seats are allotted in sequential order, a guest who arrives later must be further from Dumbledore than a guest who arrives earlier.● An incoming guest must be assigned a seat, as soon as he arrives. He will take a seat.● Once a guest takes their seat, you can’t ask them to move to some other seat. Even if the seat in front of him is empty, they will stay at their occupied seat only.● But you can make a guest disappear using your spells, Dumbledore has allowed you to make at most ‘k’ of his guests disappear. He can handle ‘k’ guests not in the party but you can’t remove more than ‘k’ guests.


1
Expert's answer
2021-08-26T02:27:11-0400
import java.util.*;
class Main{
static Vector<Integer> restore(int arr[], int N)
{
    Vector<Integer> r = new Vector<>();
 
    HashMap<Integer,
            Integer> x = new HashMap<Integer,
                                      Integer>();
    for (int i = 0; i < N; i++)
    {
 
        if (x.containsKey(arr[i]) &&
            x.get(arr[i]) == 0)
        {
 
            r.add(arr[i]);
 
            if(x.containsKey(arr[i]))
            {
                x.put(arr[i], x.get(arr[i]) + 1);
            }
            else
            {
                x.put(arr[i], 1);
            }
        }
        else
            x.put(arr[i], 0);
    }
 
    return r;
}
 
static void printResult(Vector<Integer> r)
{
    for (int i = 0; i < r.size(); i++)
        System.out.print(r.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