JavaScript Program to Find k maximum elements of array in original order

We will be using the JavaScript array sort method and slicing technique to find the k maximum elements of an array in their original order. Firstly, we sort the array in descending order, and then slice it from the beginning to the kth index to obtain the k maximum elements. By preserving the original order of the elements, the significance and context of the data remains intact, making it easier for us to analyze and interpret the results.

Approach

The approach to find k maximum elements of an array in original order can be described as follows ?

  • Create a copy of the original array to avoid modifying it.

  • Sort the copy in descending order to identify the k largest values.

  • Extract the first k elements from the sorted array.

  • Loop through the original array and collect elements that match the k maximum values.

  • Maintain the original order by processing elements sequentially.

  • Return the result array containing k maximum elements in their original positions.

Example

Here is a complete working example in JavaScript to find the k maximum elements of an array in their original order ?

function findKMaxElements(arr, k) {
   
   // Create a copy of the original array
   let sortedArr = [...arr];
   
   // Sort the copy of the array in descending order
   sortedArr.sort((a, b) => b - a);
   
   // Slice the first k elements of the sorted array
   let kMaxElements = sortedArr.slice(0, k);
   
   // Create a result array to store the k maximum elements in their original order
   let result = [];
   
   // Loop through the original array
   for (let i = 0; i 

Original array: [ 3, 2, 1, 4, 5 ]
K maximum elements in original order: [ 3, 4, 5 ]

Multiple Test Cases

// Test with different arrays and k values
let test1 = [10, 5, 8, 3, 12, 7];
let test2 = [1, 9, 3, 8, 5, 2, 7];
let test3 = [15, 10, 5, 20, 25];

console.log("Test 1 - Array:", test1, "K=4");
console.log("Result:", findKMaxElements(test1, 4));

console.log("\nTest 2 - Array:", test2, "K=3");
console.log("Result:", findKMaxElements(test2, 3));

console.log("\nTest 3 - Array:", test3, "K=2");
console.log("Result:", findKMaxElements(test3, 2));
Test 1 - Array: [ 10, 5, 8, 3, 12, 7 ] K=4
Result: [ 10, 8, 12, 7 ]

Test 2 - Array: [ 1, 9, 3, 8, 5, 2, 7 ] K=3
Result: [ 9, 8, 7 ]

Test 3 - Array: [ 15, 10, 5, 20, 25 ] K=2
Result: [ 20, 25 ]

How It Works

The algorithm works in several key steps:

  • Copy and Sort: We create a copy of the original array and sort it in descending order to identify the k largest values.

  • Extract Maximum Values: The slice(0, k) method extracts the first k elements from the sorted array.

  • Preserve Original Order: We iterate through the original array sequentially, checking if each element exists in our k maximum values.

  • Track Used Elements: Using splice() ensures we don't select duplicate elements when the array contains repeated values.

Time and Space Complexity

Aspect Complexity Explanation
Time Complexity O(n log n) Due to the sorting operation
Space Complexity O(n + k) For the copied array and result storage

Conclusion

This approach efficiently finds k maximum elements while preserving their original order using array sorting and filtering. The algorithm handles duplicates correctly and maintains the sequential appearance of elements from the input array.

Updated on: 2026-03-15T23:19:01+05:30

223 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements