// C# code for the above approach
using System;
using System.Collections.Generic;
namespace GFG
{
class Program
{
// Function to find the values
// strictly greater than the element
// and present in the array
static void operations(
int n, long[] A, long[] B)
{
// Treeset to store the
// values of the array A
SortedSet<long> tree
= new SortedSet<long>();
// HashMap to store the frequencies
// of the values in array A
Dictionary<long, int> freqMap
= new Dictionary<long, int>();
// Iterating through the array
// and add values in the treeset
for (int j = 0; j < n; j++)
{
long x = A[j];
tree.Add(x);
// Updating the frequencies
if (freqMap.ContainsKey(x))
{
freqMap[x] = freqMap[x] + 1;
}
else
{
freqMap[x] = 1;
}
}
// Finding the strictly greater value
// in the array A[] using "GetViewBetween()"
// function and also reducing the
// frequency of that value because it
// has to be used only once
for (int j = 0; j < n; j++)
{
long x = B[j];
// If the higher value exists
if (tree.GetViewBetween(x, long.MaxValue).Count > 0)
{
Console.Write(tree.GetViewBetween(x, long.MaxValue).Min + " ");
// If the frequency value is 1
// then remove it from treeset
// because it has been used
// and its frequency becomes 0
if (freqMap[tree.GetViewBetween(x, long.MaxValue).Min] == 1)
{
tree.Remove(tree.GetViewBetween(x, long.MaxValue).Min);
}
// Else, reducing the frequency
// by 1
else
{
freqMap[tree.GetViewBetween(x, long.MaxValue).Min] =
freqMap[tree.GetViewBetween(x, long.MaxValue).Min] - 1;
}
}
// If the value is not present
// then print null
else
{
Console.Write("null ");
}
}
}
static void Main(string[] args)
{
int n = 12;
long[] A = new long[] { 9, 5, 100, 4,
89, 2, 0, 2,
89, 77, 77, 77 };
long[] B = new long[] { 0, 18, 60, 34,
50, 29, 4, 20,
48, 77, 2, 8 };
operations(n, A, B);
}
}
}
// This code is contributed by Potta Lokesh