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.
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));
}
}
Comments
Leave a comment