Find k maximum elements of array in original order
Last Updated :
29 Mar, 2024
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. 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).
Below is the implementation of the above approach:
C++
// C++ program to find k maximum elements
// of array in original order
#include <bits/stdc++.h>
using namespace std;
// Function to print k Maximum elements
void printMax(int arr[], int k, int n)
{
int brr[n]={0},crr[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=INT_MIN;
int index;
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]=INT_MIN;
}
for(int i=0;i<n;i++)
{
// Printing the k maximum
// elements array
if(brr[i]==1)
cout<<crr[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;
}
// This code is contributed by Pushpesh Raj.
Java
// JAVA program to find k maximum elements
// of array in original order
import java.util.*;
class GFG {
// Function to print k Maximum elements
public static void printMax(int arr[], int k, int n)
{
int brr[] = new int[n];
int crr[] = new int[n];
for (int i = 0; i < n; i++)
brr[i] = 0;
// 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[] = new int[] { 50, 8, 45, 12, 25, 40, 84 };
int n = arr.length;
int k = 3;
printMax(arr, k, n);
}
}
// This code is contributed by Taranpreet
Python3
# Function to print k Maximum elements
def printMax(arr, k, n):
brr = [0 for _ in range(n)]
crr = [0 for _ in range(n)]
# Copying the array arr
# into crr so that it
# can be used later
for i in range(0, n):
crr[i] = arr[i]
# Iterating for K-times
for i in range(0, k):
# Finding the maximum element
# along with its index
maxi = -99999
index = 0
for j in range(0, n):
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] = -99999
for i in range(0, n):
# Printing the k maximum
# elements array
if brr[i] == 1:
print(crr[i], end='')
print(" ", end='')
if __name__ == "__main__":
arr = [50, 8, 45, 12, 25, 40, 84]
n = len(arr)
k = 3
printMax(arr, k, n)
# This code is contributed by Aarti_Rathi
C#
// C# program to find k maximum
// elements of array in original order
using System;
using System.Linq;
class GFG {
// Function to print m Maximum elements
public static void printMax(int[] arr, int k, int n)
{
// Copying the array arr
// into crr so that it
// can be used later
int[] brr = new int[n];
int[] crr = new int[n];
for (int i = 0; i < n; i++) {
brr[i] = 0;
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 = Int32.MinValue;
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] = Int32.MinValue;
}
for (int i = 0; i < n; i++) {
// Printing the k maximum
// elements array
if (brr[i] == 1)
Console.Write(crr[i] + " ");
}
}
// Driver code
public static void Main()
{
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 Aarti_Rathi
JavaScript
// Function to print k Maximum elements
function printMax(arr, k, n)
{
var brr = Array(n).fill(0);
var crr = Array(n).fill(0);
for (var i =0; i < n; i++)
{
brr[i] = 0;
}
// Copying the array arr
// into crr so that it
// can be used later
for (var i=0; i < n; i++)
{
crr[i] = arr[i];
}
// Iterating for K-times
for (var i=0; i < k; i++)
{
// Finding the maximum element
// along with its index
var maxi = -Number.MAX_VALUE;
var index = 0;
for (var 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] = -Number.MAX_VALUE;
}
for (var i=0; i < n; i++)
{
// Printing the k maximum
// elements array
if (brr[i] == 1)
{
console.log(crr[i] + " ");
}
}
}
// Driver code
var arr = [50, 8, 45, 12, 25, 40, 84];
var n = arr.length;
var k = 3;
printMax(arr, k, n);
// This code is contributed by Aarti_Rathi
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;
}
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
Python3
# Python3 program to find k maximum elements
# of array in original order
# Function to print m Maximum elements
def printMax(arr, k, n):
# vector to store the copy of the
# original array
brr = arr.copy()
# Sorting the vector in descending
# order. Please refer below link for
# details
brr.sort(reverse = True)
# Traversing through original array and
# print all those elements that are
# in first k of sorted vector.
for i in range(n):
if (arr[i] in brr[0:k]):
print(arr[i], end = " ")
# Driver code
arr = [ 50, 8, 45, 12, 25, 40, 84 ]
n = len(arr)
k = 3
printMax(arr, k, n)
# This code is contributed by SHUBHAMSINGH10
C#
// C# program to find k maximum
// elements of array in original order
using System;
using System.Linq;
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
int[] brr = new int[n];
for(int i = 0; i < n; i++)
brr[i] = arr[i];
// Sorting the array in
// descending order
Array.Sort(brr);
Array.Reverse(brr);
int[] crr = new int[k];
for(int i = 0; i < k; i++)
{
crr[i] = brr[i];
}
// 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 Array.BinarySearch()
for(int i = 0; i < n; ++i)
{
if (crr.Contains(arr[i]))
{
Console.Write(arr[i] + " ");
}
}
}
// Driver code
public static void Main()
{
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 Shubhamsingh10
JavaScript
<script>
// JavaScript program to find k maximum elements
// of array in original order
// Function to print m Maximum elements
function printMax(arr, k, n)
{
// vector to store the copy of the
// original array
var brr = arr.slice();
// Sorting the vector in descending
// order. Please refer below link for
// details
// https://www.geeksforgeeks.org/sort-c-stl/
brr.sort((a, b) => b - a);
// 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 (var i = 0; i < n; ++i)
if (brr.indexOf(arr[i]) < k)
document.write(arr[i] +" ");
}
// Driver code
var arr = [ 50, 8, 45, 12, 25, 40, 84 ];
var n = arr.length;
var k = 3;
printMax(arr, k, n);
// This code is contributed by ShubhamSingh10
</script>
Time Complexity: O(n Log n) for sorting.
Auxiliary Space: O(n)
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
Maximum sum of increasing order elements from n arrays Given n arrays of size m each. Find the maximum sum obtained by selecting a number from each array such that the elements selected from the i-th array are more than the element selected from (i-1)-th array. If maximum sum cannot be obtained then return 0.Examples: Input : arr[][] = {{1, 7, 3, 4}, {4
13 min read
Maximum in array which is at-least twice of other elements Given an array of integers of length n. Our task is to return the index of the max element if the it is at least twice as much as every other number in the array. If the max element does not satisfy the condition return -1. Examples: Input : arr = {3, 6, 1, 0} Output : 1 Here, 6 is the largest integ
7 min read
Find the maximum possible value of last element of the Array Given a non-negative array arr of size N and an integer M representing the number of moves such that in one move, the value of any one element in the array decreases by one, and the value of its adjacent element on the right increases by one. The task is to find the maximum possible value of the las
7 min read
Find the element having maximum premutiples in the array Given an array arr[], the task is to find the element which has the maximum number of pre-multiples present in the set. For any index i, pre-multiple is the number that is multiple of i and is present before the ith index of the array. Also, print the count of maximum multiples of that element in th
8 min read
Find k largest elements in an array Given an array arr[] and an integer k, the task is to find k largest elements in the given array. Elements in the output array should be in decreasing order.Examples:Input: [1, 23, 12, 9, 30, 2, 50], k = 3Output: [50, 30, 23]Input: [11, 5, 12, 9, 44, 17, 2], k = 2Output: [44, 17]Table of Content[Nai
15+ min read