Sort the array in descending order
arr . sort ( ( b , a ) => a - b ) var temp = 0 ; for ( var i = K ; i < N ; i ++ ) { for ( var j = i + 1 ; j < N ; j ++ ) { if ( arr [ i ] > arr [ j ] ) { temp = arr [ i ] ; arr [ i ] = arr [ j ] ; arr [ j ] = temp ; } } }