using
System;
using
System.Collections.Generic;
using
System.Linq;
class
GFG
{
public
static
int
maxDiffK(
int
[] arr,
int
K)
{
int
N = arr.Length;
Dictionary<
int
,
int
> tm =
new
Dictionary<
int
,
int
>();
for
(
int
i = 0; i < K; i++)
{
if
(!tm.ContainsKey(arr[i]))
{
tm[arr[i]] = 1;
}
else
{
tm[arr[i]] += 1;
}
}
int
[] keys = tm.Keys.ToArray();
Array.Sort(keys);
int
maxDiff = Math.Abs(keys.First() - keys.Last());
for
(
int
i = K; i < N; i++)
{
if
(!tm.ContainsKey(arr[i]))
{
tm[arr[i]] = 1;
}
else
{
tm[arr[i]] += 1;
}
int
freq = tm[arr[i - K]];
if
(freq == 1)
tm.Remove(arr[i - K]);
else
tm[arr[i - K]] = freq - 1;
keys = tm.Keys.ToArray();
Array.Sort(keys);
maxDiff = Math.Max(maxDiff, Math.Abs(keys.First() - keys.Last()));
}
return
maxDiff;
}
public
static
void
Main()
{
int
[] arr = { 2, 3, -1, -5, 4, 0 };
int
K = 3;
Console.Write(maxDiffK(arr, K));
}
}