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 . isEmpty ( ) && vp . get ( vp . size ( ) - 1 ) . first == val && ind - vp . get ( vp . size ( ) - 1 ) . second + 1 <= K ) vp . remove ( vp . size ( ) - 1 ) ; }