to store max Index encountred so far
int index = -1 ; for ( int j = 0 ; j < N ; ++ j ) { if ( S [ j ] == ch ) count ++ ; if ( count == K ) index = j ; } answer . push_back ( { ch , index } ) ; } int flag = 0 ;