Javascript Program to Find k maximum elements of array in original order
Last Updated :
13 Sep, 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 (Number.MIN_SAFE_INTEGER in Javascript) 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).
JavaScript
// JavaScript program to find k maximum elements
// of array in original order
// Function to print k Maximum elements
function printMax(arr, k, n) {
let brr = Array(n).fill(0);
let crr = Array(n)
// Copying the array arr
// into crr so that it
// can be used later
for (let i = 0; i < n; i++) {
crr[i] = arr[i];
}
// Iterating for K-times
for (let i = 0; i < k; i++) {
// Finding the maximum element
// along with its index
let maxi = Number.MIN_SAFE_INTEGER;
let index;
for (let 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.MIN_SAFE_INTEGER;
}
for (let i = 0; i < n; i++) {
// Printing the k maximum
// elements array
if (brr[i] == 1)
console.log(crr[i] + " ");
}
}
// Driver code
let arr = [50, 8, 45, 12, 25, 40, 84];
let n = arr.length;
let k = 3;
printMax(arr, k, n);
// This code is contributed by Pushpesh raj
Complexity Analysis:
- 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.
JavaScript
// 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
let 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 (let i = 0; i < n; ++i)
if (brr.indexOf(arr[i]) < k)
console.log(arr[i] + " ");
}
// Driver code
let arr = [50, 8, 45, 12, 25, 40, 84];
let n = arr.length;
let k = 3;
printMax(arr, k, n);
// This code is contributed by ShubhamSingh10
Complexity Analysis:
- 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
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
JavaScript Program to Find kth Largest/Smallest Element in an Array JavaScript allows us to find kth largest/smallest element in an array. We are given an array containing some elements, we have to find kth smallest/largest element from the array where k is a number greater than zero and less than equal to the total number of elements present in the array. There are
5 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
Maximize first array element by performing given operations at most K times Given an array arr[] of size N an integer K, the task is to find the maximize the first element of the array by performing the following operations at most K times: Choose a pair of indices i and j (0 ? i, j ? N-1) such that |i ? j| = 1 and arri > 0.Set arri = arri ? 1 and arrj = arrj + 1 on thos
7 min read
Sum of Array maximums after K operations by reducing max element to its half Given an array arr[] of N integers and an integer K, the task is to find the sum of maximum of the array possible wherein each operation the current maximum of the array is replaced with its half. Example: Input: arr[] = {2, 4, 6, 8, 10}, K = 5Output: 33Explanation: In 1st operation, the maximum of
6 min read