text: Find k maximum elements of array in original order | Function to print m Maximum elements ; vector to store the copy of the original array ; Sorting the vector in descending order . Please refer below link for details www . geeksforgeeks . org / sort - c - stl / https : ; Traversing through original array and printing all those elements that are in first k of sorted vector . goo . gl / 44 Rwgt Please refer https : for details of binary_search ( ) ; Driver code
code: function printMax ( arr , k , n ) { var brr = arr . slice ( ) ; brr . sort ( ( a , b ) => b - a ) ; for ( var i = 0 ; i < n ; ++ i ) if ( brr . indexOf ( arr [ i ] ) < k ) document . write ( arr [ i ] + " " ) ; } var arr = [ 50 , 8 , 45 , 12 , 25 , 40 , 84 ] ; var n = arr . length ; var k = 3 ; printMax ( arr , k , n ) ;