using
System;
using
System.Collections.Generic;
public
class
GFG
{
public
static
bool
IsPossible(List<
int
> nums,
int
k)
{
PriorityQueue<Pair<
int
,
int
>> pq =
new
PriorityQueue<Pair<
int
,
int
>>(
new
PairComparer());
int
i = 0;
while
(i < nums.Count)
{
if
(pq.Count == 0)
{
pq.Enqueue(
new
Pair<
int
,
int
>(nums[i], 1));
i++;
}
else
{
if
(nums[i] == pq.Peek().Key)
{
pq.Enqueue(
new
Pair<
int
,
int
>(nums[i], 1));
i++;
}
else
if
(nums[i] == pq.Peek().Key + 1)
{
Pair<
int
,
int
> temp = pq.Dequeue();
pq.Enqueue(
new
Pair<
int
,
int
>(nums[i], temp.Value + 1));
i++;
}
else
{
if
(pq.Peek().Value < k)
{
return
false
;
}
pq.Dequeue();
}
}
}
while
(pq.Count > 0)
{
if
(pq.Peek().Value < k)
{
return
false
;
}
pq.Dequeue();
}
return
true
;
}
public
static
void
Main(
string
[] args)
{
List<
int
> vec =
new
List<
int
> { 1, 2, 3, 3, 4, 5 };
int
k = 3;
if
(IsPossible(vec, k))
{
Console.WriteLine(
"True"
);
}
else
{
Console.WriteLine(
"False"
);
}
}
}
public
class
Pair<K, V>
{
public
K Key {
get
; }
public
V Value {
get
; }
public
Pair(K key, V value)
{
Key = key;
Value = value;
}
}
public
class
PairComparer : IComparer<Pair<
int
,
int
>>
{
public
int
Compare(Pair<
int
,
int
> a, Pair<
int
,
int
> b)
{
if
(a.Key == b.Key)
{
return
a.Value - b.Value;
}
else
{
return
a.Key - b.Key;
}
}
}
public
class
PriorityQueue<T>
{
private
List<T> data;
private
IComparer<T> comparer;
public
PriorityQueue(IComparer<T> comparer)
{
this
.data =
new
List<T>();
this
.comparer = comparer;
}
public
int
Count
{
get
{
return
data.Count; }
}
public
T Peek()
{
return
data[0];
}
public
void
Enqueue(T item)
{
data.Add(item);
int
childIndex = data.Count - 1;
while
(childIndex > 0)
{
int
parentIndex = (childIndex - 1) / 2;
if
(comparer.Compare(data[childIndex], data[parentIndex]) >= 0)
break
;
T tmp = data[childIndex];
data[childIndex] = data[parentIndex];
data[parentIndex] = tmp;
childIndex = parentIndex;
}
}
public
T Dequeue()
{
int
lastIndex = data.Count - 1;
T frontItem = data[0];
data[0] = data[lastIndex];
data.RemoveAt(lastIndex);
lastIndex--;
int
parentIndex = 0;
while
(
true
)
{
int
leftChild = parentIndex * 2 + 1;
if
(leftChild > lastIndex)
break
;
int
rightChild = leftChild + 1;
if
(rightChild <= lastIndex && comparer.Compare(data[rightChild], data[leftChild]) < 0)
leftChild = rightChild;
if
(comparer.Compare(data[parentIndex], data[leftChild]) <= 0)
break
;
T tmp = data[parentIndex];
data[parentIndex] = data[leftChild];
data[leftChild] = tmp;
parentIndex = leftChild;
}
return
frontItem;
}
}