Python Program to Find k maximum elements of array in original order
Last Updated :
28 May, 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 (-1*sys.maxsize for largest minimum value in Python) 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).
Python3
# Python program to find k maximum elements
# of array in original order
# import the module
import sys
# Function to print k Maximum elements
def printMax(arr, k, n):
brr=[0]*n
crr=[0]*n
# Coping the array arr
# into crr so that it
# can be used later
for i in range(n):
crr[i]=arr[i]
# Iterating for K-times
for i in range(k):
# Finding the maximum element
# along with its index
maxi=-1*sys.maxsize
for j in range(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]=-1*sys.maxsize
for i in range(n):
# Printing the k maximum
# elements array
if(brr[i]==1):
print(crr[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 Pushpesh Raj.
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.
Python3
# Python3 program to find k maximum elements
# of array in original order
# Function to print 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
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
Python 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 Method 1: The idea is to run the loop for no_of_rows. Check each element inside the row and fi
5 min read
Python program to find N largest elements from a list Given a list of integers, the task is to find N largest elements assuming size of list is greater than or equal o N. Examples : Input : [4, 5, 1, 2, 9] N = 2 Output : [9, 5] Input : [81, 52, 45, 10, 3, 2, 96] N = 3 Output : [81, 96, 52] A simple solution traverse the given list N times. In every tra
5 min read
Python | Positions of maximum element in list Sometimes, while working with Python lists, we can have a problem in which we intend to find the position of maximum element of list. This task is easy and discussed many times. But sometimes, we can have multiple maximum elements and hence multiple maximum positions. Let's discuss a shorthand to ac
3 min read
Python - Maximum and Minimum K elements in Tuple Sometimes, while dealing with tuples, we can have problem in which we need to extract only extreme K elements, i.e maximum and minimum K elements in Tuple. This problem can have applications across domains such as web development and Data Science. Let's discuss certain ways in which this problem can
8 min read
Python3 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
4 min read