Pop the back element from the vp until the first value is same as val and difference between indices is less than K
while ( vp . Count != 0 && vp [ vp . Count - 1 ] . first == val && ind - vp [ vp . Count - 1 ] . second + 1 <= K ) vp . RemoveAt ( vp . Count - 1 ) ; }