using
System;
using
System.Collections.Generic;
class
GFG
{
static
int
maxSubarray(
int
[] arr,
int
n,
int
k)
{
SortedDictionary<
int
,
int
> Map =
new
SortedDictionary<
int
,
int
>();
int
l = 0;
for
(
int
i = 0; i < n; i++)
{
l++;
if
(!Map.ContainsKey(arr[i]))
{
Map.Add(arr[i], 1);
}
else
{
Map[arr[i]]++;
}
if
(l == k)
{
var
itMax = getLast(Map);
var
itMin = getFirst(Map);
Console.WriteLine(itMin.Key +
" "
+ itMax.Key);
Map[arr[i - k + 1]]--;
if
(Map[arr[i - k + 1]] == 0)
{
Map.Remove(arr[i - k + 1]);
}
l--;
}
}
return
0;
}
static
KeyValuePair<
int
,
int
> getFirst(SortedDictionary<
int
,
int
> lhm)
{
int
count = 1;
foreach
(KeyValuePair<
int
,
int
> it
in
lhm)
{
if
(count == 1)
{
return
it;
}
count++;
}
return
default
(KeyValuePair<
int
,
int
>);
}
static
KeyValuePair<
int
,
int
> getLast(SortedDictionary<
int
,
int
> lhm)
{
int
count = 1;
foreach
(KeyValuePair<
int
,
int
> it
in
lhm)
{
if
(count == lhm.Count)
{
return
it;
}
count++;
}
return
default
(KeyValuePair<
int
,
int
>);
}
public
static
void
Main(
string
[] args)
{
int
[] arr = { 5, 4, 3, 2, 1, 6, 3, 5, 4, 2, 1 };
int
k = 3;
int
n = arr.Length;
maxSubarray(arr, n, k);
}
}