Update the smallest elements row - wise
for ( int i = 0 ; i < N ; i ++ ) { for ( int j = 0 ; j < M - K + 1 ; j ++ ) { int min = int . MaxValue ; for ( int k = j ; k < j + K ; k ++ ) { min = Math . Min ( min , nums [ i , k ] ) ; } nums [ i , j ] = min ; } }