C++ Program to Find k maximum elements of array in original order Last Updated : 17 Aug, 2023 Comments Improve Suggest changes Like Article Like Report Given an array arr[] and an integer k, we need to print k maximum elements of given array. The elements should printed in the order of the input.Note : k is always less than or equal to n. Examples: Input : arr[] = {10 50 30 60 15} k = 2 Output : 50 60 The top 2 elements are printed as per their appearance in original array. Input : arr[] = {50 8 45 12 25 40 84} k = 3 Output : 50 45 84 Method 1: We search for the maximum element k times in the given array. Each time we find one maximum element, we print it and replace it with minus infinite (INT_MIN in C) in the array. The time complexity of this method is O(n*k). C++ // C++ program to find k Maximum elements #include <bits/stdc++.h> using namespace std; // Function to print k Maximum elements void printMax(int a[], int n, int k) { int b[n],c[n]; // Coping the array a // into c and initialising // elements in array b as 0. for(int i=0;i<n;i++) { c[i]=a[i]; b[i]=0; } // Iterating for K-times // to find k-maximum for(int i=0;i<k;i++) { // finding the maximum element // and its index int maxi=INT_MIN; int index; for(int j=0;j<n;j++) { if(a[j]>maxi) { maxi=a[j]; index=j; } } // Assigning 1 in order // to mark the position // of all k maximum numbers b[index]=1; a[index]=INT_MIN; } for(int i=0;i<n;i++) { // Printing the k maximum // elements of the array if(b[i]==1) cout<<c[i]<<" "; } } // Driver code int main() { int a[] = { 50, 8, 45, 12, 25, 40, 84 }; int n = sizeof(a) / sizeof(a[0]); int k = 3; printMax(a, n, k); return 0; } // This code is contributed by Aman kumar. Output50 45 84 Time Complexity: O(n*k)Auxiliary Space: O(n) Method 2: In this method, we store the original array in a new array and will sort the new array in descending order. After sorting, we iterate the original array from 0 to n and print all those elements that appear in first k elements of new array. For searching, we can do Binary Search. C++ // C++ program to find k maximum elements // of array in original order #include <bits/stdc++.h> using namespace std; // Function to print m Maximum elements void printMax(int arr[], int k, int n) { // vector to store the copy of the // original array vector<int> brr(arr, arr + n); // Sorting the vector in descending // order. Please refer below link for // details // https://www.geeksforgeeks.org/sort-c-stl/ sort(brr.begin(), brr.end(), greater<int>()); // Traversing through original array and // printing all those elements that are // in first k of sorted vector. // Please refer https://goo.gl/44Rwgt // for details of binary_search() for (int i = 0; i < n; ++i) if (binary_search(brr.begin(), brr.begin() + k, arr[i], greater<int>())) cout << arr[i] << " "; } // Driver code 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; } Output: 50 45 84 Time Complexity: O(n Log n) for sorting. Auxiliary Space: O(n) Please refer complete article on Find k maximum elements of array in original order for more details! Comment More infoAdvertise with us Next Article C++ Program to Find k maximum elements of array in original order kartik Follow Improve Article Tags : Searching Sorting C++ Programs C++ DSA Binary Search STL cpp-vector +4 More Practice Tags : CPPBinary SearchSearchingSortingSTL +1 More Similar Reads C++ Program to Find Largest Element in an Array In this article, we will learn to write a C++ program to find the largest element in the given array arr of size N. The element that is greater than all other elements is the largest element in the array. Recommended PracticeHelp a Thief!!!Try It! One of the most simplest and basic approaches to fin 2 min read How to Find the Maximum Element of an Array using STL in C++? Given an array of n elements, the task is to find the maximum element using STL in C++.ExamplesInput: arr[] = {11, 13, 21, 45, 8}Output: 45Explanation: 45 is the largest element of the array.Input: arr[] = {1, 9, 2, 5, 7}Output: 9Explanation: 9 is the largest element of the array.STL provides the fo 3 min read C++ Program to Find the Second Largest Element in an Array In C++, an array is a data structure that is used to store multiple values of similar data types in a contiguous memory location. In this article, we will learn how to find the second largest element in an array in C++. Examples: Input: arr[] = {34, 5, 16, 14, 56, 7, 56} Output: 34 Explanation: The 3 min read C++ Program to Find the K'th largest element in a stream Given an infinite stream of integers, find the k'th largest element at any point of time.Example: Input:stream[] = {10, 20, 11, 70, 50, 40, 100, 5, ...}k = 3Output: {_, _, 10, 11, 20, 40, 50, 50, ...} Extra space allowed is O(k). Recommended: Please solve it on "PRACTICE" first, before moving on to 7 min read Maximize the rightmost element of an array in k operations in Linear Time Given an array arr[ ] of size N and an integer p, the task is to find maximum possible value of rightmost element of array arr[ ] performing at most k operations.In one operation decrease arr[i] by p and increase arr[i+1] by p. Examples: Input: N = 4, p = 2, k = 5, arr = {3, 8, 1, 4}Output: 8Explana 9 min read C++ Program to Maximize elements using another array Given two arrays with size n, maximize the first array by using the elements from the second array such that the new array formed contains n greatest but unique elements of both the arrays giving the second array priority (All elements of second array appear before first array). The order of appeara 4 min read Program to find largest element in an array using Dynamic Memory Allocation Given an array arr[] consisting of N integers, the task is to find the largest element in the given array using Dynamic Memory Allocation. Examples: Input: arr[] = {4, 5, 6, 7} Output: 7Explanation:The largest element present in the given array is 7. Input: arr[] = {8, 9, 10, 12} Output: 12Explanati 5 min read C++ Program for Third largest element in an array of distinct elements Given an array of n integers, find the third largest element. All the elements in the array are distinct integers. Example :  Input: arr[] = {1, 14, 2, 16, 10, 20} Output: The third Largest element is 14 Explanation: Largest element is 20, second largest element is 16 and third largest element is 1 5 min read Minimize the maximum element of N subarrays of size K Given an array arr[] and two integers N and K, the task is to choose non-overlapping N subarrays of size K such that the maximum element of all subarrays is minimum.Note: If it is not possible to choose N such subarrays then return -1. Examples: Input: arr[] = {1, 10, 3, 10, 2}, N = 3, K = 1 Output: 8 min read Given an array and two integers l and r, find the kth largest element in the range [l, r] Given an unsorted array arr[] of n integers and an integer k, the task is to find the kth largest element in the given index range [l, r]Examples: Input: arr[] = {5, 3, 2, 4, 1}, k = 4, l = 1, r = 5 Output: 4 4 will be the 4th element when arr[0...4] is sorted.Input: arr[] = {1, 4, 2, 3, 5, 7, 6}, k 11 min read Like