Here cand and maxi arrays are in non - increasing order beforehand . Now , j is the index of the next cand element and i is the index of next maxi element . Traverse through maxi array . If cand [ j ] > maxi [ i ] insert cand [ j ] at the ith position in the maxi array and remove the minimum element of the maxi array i . e . the last element and increase j by 1 i . e . take the next element from cand .
int k = maxi . size ( ) ; int j = 0 ; for ( int i = 0 ; i < k ; i ++ ) { if ( cand [ j ] > maxi [ i ] ) { maxi . insert ( maxi . begin ( ) + i , cand [ j ] ) ; maxi . erase ( maxi . begin ( ) + k ) ; j += 1 ; } } }