Pop the back element from the vp until the first value is same as val and difference between indices is less than K
while ( len ( vp ) != 0 and vp [ - 1 ] [ 0 ] == val and ind - vp [ - 1 ] [ 1 ] + 1 <= K ) : NEW_LINE INDENT vp . pop ( ) NEW_LINE DEDENT