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 repalce it with minus infinite (INT_MIN in C) in the array. The time complexity of this method is O(n*k).

**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++

`// CPP 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 ` ` ` `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; ` `} ` |

*chevron_right*

*filter_none*

## Java

`// Java program to find k maximum ` `// elements of array in original order ` `import` `java.util.Arrays; ` `import` `java.util.Collections; ` ` ` `public` `class` `GfG { ` ` ` ` ` `// Function to print m Maximum elements ` ` ` `public` `static` `void` `printMax(` `int` `arr[], ` `int` `k, ` `int` `n) ` ` ` `{ ` ` ` `// Array to store the copy ` ` ` `// of the original array ` ` ` `Integer[] brr = ` `new` `Integer[n]; ` ` ` ` ` `for` `(` `int` `i = ` `0` `; i < n; i++) ` ` ` `brr[i] = arr[i]; ` ` ` ` ` `// Sorting the array in ` ` ` `// descending order ` ` ` `Arrays.sort(brr, Collections.reverseOrder()); ` ` ` ` ` `// Traversing through original array and ` ` ` `// printing all those elements that are ` ` ` `// in first k of sorted array. ` ` ` `// Please refer https://goo.gl/uj5RCD ` ` ` `// for details of Arrays.binarySearch() ` ` ` `for` `(` `int` `i = ` `0` `; i < n; ++i) ` ` ` `if` `(Arrays.binarySearch(brr, arr[i], ` ` ` `Collections.reverseOrder()) >= ` `0` ` ` `&& Arrays.binarySearch(brr, arr[i], ` ` ` `Collections.reverseOrder()) < k) ` ` ` ` ` `System.out.print(arr[i]+ ` `" "` `); ` ` ` `} ` ` ` ` ` `// Driver code ` ` ` `public` `static` `void` `main(String args[]) ` ` ` `{ ` ` ` `int` `arr[] = { ` `50` `, ` `8` `, ` `45` `, ` `12` `, ` `25` `, ` `40` `, ` `84` `}; ` ` ` `int` `n = arr.length; ` ` ` `int` `k = ` `3` `; ` ` ` `printMax(arr, k, n); ` ` ` `} ` `} ` ` ` `// This code is contributed by Swetank Modi ` |

*chevron_right*

*filter_none*

Output :

50 45 84

Time Complexity : O(n Log n) for sorting.

Auxiliary Space : O(n)

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the **DSA Self Paced Course** at a student-friendly price and become industry ready.

## Recommended Posts:

- Print n smallest elements from given array in their original order
- Generate original array from an array that store the counts of greater elements on right
- Generate original array from difference between every two consecutive elements
- Count of array elements whose order of deletion precedes order of insertion
- Rearrange an Array such that Sum of same-indexed subsets differ from their Sum in the original Array
- Find original numbers from gcd() every pair
- Build original array from the given sub-sequences
- Sort elements of an array in increasing order of absolute difference of adjacent elements
- Sum of elements in 1st array such that number of elements less than or equal to them in 2nd array is maximum
- Print elements of an array according to the order defined by another array | set 2
- Tag Sort (To get both sorted and original)
- Count of ordered triplets (R, G, B) in a given original string
- Restore original String from given Encrypted String by the given operations
- Maximum sum of increasing order elements from n arrays
- Maximum sum by picking elements from two arrays in order
- Sort all even numbers in ascending order and then sort all odd numbers in descending order
- Sorting rows of matrix in ascending order followed by columns in descending order
- Sort first k values in ascending order and remaining n-k values in descending order
- Sorting rows of matrix in descending order followed by columns in ascending order
- Print nodes of a Binary Search Tree in Top Level Order and Reversed Bottom Level Order alternately

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.