text: Find k maximum elements of array in original order | C ++ program to 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: #include <bits/stdc++.h> NEW_LINE using namespace std ; void printMax ( int arr [ ] , int k , int n ) { vector < int > brr ( arr , arr + n ) ; sort ( brr . begin ( ) , brr . end ( ) , greater < int > ( ) ) ; for ( int i = 0 ; i < n ; ++ i ) if ( binary_search ( brr . begin ( ) , brr . begin ( ) + k , arr [ i ] , greater < int > ( ) ) ) cout << arr [ i ] << " ▁ " ; } int main ( ) { int arr [ ] = { 50 , 8 , 45 , 12 , 25 , 40 , 84 } ; int n = sizeof ( arr ) / sizeof ( arr [ 0 ] ) ; int k = 3 ; printMax ( arr , k , n ) ; return 0 ; }