Java Program to Find k maximum elements of array in original order Last Updated : 07 Dec, 2022 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 (Integer.MIN_VALUE in Java) in the array. Also, the position of all k maximum elements is marked using an array so that with the help of that array we can print the elements in the order given in the original array. The time complexity of this method is O(n*k). Java // Java program to find k maximum elements // of array in original order import java.util.*; import java.util.Arrays; import java.util.Collections; class GFG { // Function to print k Maximum elements public static void printMax(int arr[], int k, int n) { int[] brr = new int[n]; Arrays.fill(brr, 0); int[] crr = new int[n]; // Copying the array arr // into crr so that it // can be used later for(int i=0;i<n;i++) { crr[i]=arr[i]; } // Iterating for K-times for(int i=0;i<k;i++) { // Finding the maximum element // along with its index int maxi=Integer.MIN_VALUE; int index=0; for(int j=0;j<n;j++) { if(maxi<arr[j]) { maxi=arr[j]; index=j; } } // Assigning 1 in order // to mark the position // of all k maximum numbers brr[index]=1; arr[index]=Integer.MIN_VALUE; } for(int i=0;i<n;i++) { // Printing the k maximum // elements array if(brr[i]==1) System.out.print(crr[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); } } 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. 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 Output50 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 Java Program to Find k maximum elements of array in original order kartik Follow Improve Article Tags : Java Searching Sorting Java Programs DSA Binary Search STL cpp-vector +4 More Practice Tags : Binary SearchJavaSearchingSortingSTL +1 More Similar Reads Java Program to Find maximum element of each row in a matrix Given a matrix, the task is to find the maximum element of each row. Examples: Input : [1, 2, 3] [1, 4, 9] [76, 34, 21] Output : 3 9 76 Input : [1, 2, 3, 21] [12, 1, 65, 9] [1, 56, 34, 2] Output : 21 65 56 Approach : Approach is very simple. The idea is to run the loop for no_of_rows. Check each ele 2 min read Java Program to Find Largest Element in an Array Finding the largest element in an array is a common programming task. There are multiple approaches to solve it. In this article, we will explore four practical approaches one by one to solve this in Java.Example Input/Output:Input: arr = { 1, 2, 3, 4, 5}Output: 5Input: arr = { 10, 3, 5, 7, 2, 12}Ou 4 min read Finding Maximum Element of Java ArrayList For finding the maximum element in the ArrayList, complete traversal of the ArrayList is required. There is an inbuilt function in the ArrayList class to find the maximum element in the ArrayList, i.e. Time Complexity is O(N), where N is the size of ArrayList, Let's discuss both the methods. Example 2 min read Finding Maximum Element of Java Vector Vector implements a dynamic array that means it can grow or shrink as required. Like an array, it contains components that can be accessed using an integer index. We know two ways for declaring array i.e. either with a fixed size of array or size enter as per the demand of the user according to whic 3 min read Java 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 = 3 Output: {_, _, 10, 11, 20, 40, 50, 50, ...} Extra space allowed is O(k). Recommended: Please solve it on "PRACTICE" first, before moving on 4 min read How to Find the Maximum Element in an Array? In Java, the array is a data structure that allows the users to store data of the same type in contiguous memory locations. To find the maximum element in an Array in Java, we can sort the array in ascending order using the Arrays.sort() method and then we can access the last element of the array wh 1 min read Java Program for 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 3 min read Finding the Minimum or Maximum Value in Java ArrayList The minimum value is the one with the smallest value and the maximum value is the one with the largest value. The main task here is to find the minimum and maximum value from the ArrayList. Consider an example of an ArrayList, and we need to find the largest and the smallest element. Example: Input 5 min read Java 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 Java Program To Find Next Greater Element Given an array, print the Next Greater Element (NGE) for every element. The Next greater Element for an element x is the first greater element on the right side of x in the array. Elements for which no greater element exist, consider the next greater element as -1. Examples: For an array, the righ 6 min read Like