Java Program to Find k maximum elements of array in original order
Last Updated :
07 Dec, 2022
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);
}
}
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
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!
Similar Reads
Javascript Program to Find k maximum elements of array in original order 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 appea
3 min read
Find k maximum elements of array in original order 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 app
11 min read
Maximum element in a sorted and rotated array Given a sorted array arr[] (may contain duplicates) of size n that is rotated at some unknown point, the task is to find the maximum element in it. Examples: Input: arr[] = {5, 6, 1, 2, 3, 4}Output: 6Explanation: 6 is the maximum element present in the array.Input: arr[] = {3, 2, 2, 2}Output: 3Expla
7 min read
Minimize maximum array element possible by at most K splits on the given array Given an array arr[] consisting of N positive integers and a positive integer K, the task is to minimize the maximum element present in the array by splitting at most K array elements into two numbers equal to their value. Examples: Input: arr[] = {2, 4, 8, 2}, K = 4Output: 2Explanation:Following se
9 min read
Largest element after K operations on Array Given array A[] (1 <= A[i] <= 108) of size N (2 <= N <= 103) and integer K (1 <= K <= 108), the task is to find the largest element of the array after performing the given operation at most K times, in one operation choose i from 1 to N - 1 such that A[i] <= A[i + 1] and then in
13 min read
Maximize the minimum value of Array by performing given operations at most K times Given array A[] of size N and integer K, the task for this problem is to maximize the minimum value of the array by performing given operations at most K times. In one operation choose any index and increase that array element by 1. Examples: Input: A[] = {3, 1, 2, 4, 6, 2, 5}, K = 8Output: 4Explana
10 min read