
- C++ Basics
- C++ Home
- C++ Overview
- C++ Environment Setup
- C++ Basic Syntax
- C++ Comments
- C++ Data Types
- C++ Variable Types
- C++ Variable Scope
- C++ Constants/Literals
- C++ Modifier Types
- C++ Storage Classes
- C++ Operators
- C++ Loop Types
- C++ Decision Making
- C++ Functions
- C++ Numbers
- C++ Arrays
- C++ Strings
- C++ Pointers
- C++ References
- C++ Date & Time
- C++ Basic Input/Output
- C++ Data Structures
- C++ Object Oriented
- C++ Classes & Objects
- C++ Inheritance
- C++ Overloading
- C++ Polymorphism
- C++ Abstraction
- C++ Encapsulation
- C++ Interfaces
Find k maximum elements of array in original order in C++
In this problem, we are given an array arr[] of n elements. Our task is to find k maximum elements of the array in original order.
We need to find k maximum elements of the array and then print then as they were indexed originally.
Let’s take an example to understand the problem,
Input: arr[] = {5, 1, 3, 6, 2}, k = 2
Output: 5, 6
Explanation:
The largest two elements of the array are 6 and 5. But 5 comes before 6 in the original array hence we have printed in that way.
Solution Approach
To solve the problem, and print k elements in the original order.
For this we will create a decArray that will store the elements of arr[] in descending order. Then we will traverse the original array and print k largest element in order using the decArray[].
Program to illustrate the working of our solution,
Example
#include <bits/stdc++.h> using namespace std; bool seachVal(int decArr[], int k, int ele){ for(int i = 0; i < k; i++){ if( decArr[i] == ele) return true; } return false; } void printKMaxEle(int arr[], int k, int n) { int decArr[n]; for(int i = 0; i < n ; i++){ decArr[i] = arr[i]; } sort(decArr, decArr + n, greater<int>()); for (int i = 0; i < n; ++i) if ( seachVal(decArr, k, arr[i]) ) cout<<arr[i]<<" "; } int main() { int arr[] = { 15, 1, 3, 6, 2, 34, 8, 9 }; int n = sizeof(arr) / sizeof(arr[0]); int k = 3; cout<<k<<" maximum elements of the array in their original order are \n"; printKMaxEle(arr, k, n); return 0; }
Output
3 maximum elements of the array in their original order are 15 34 9
- Related Articles
- JavaScript Program to Find k maximum elements of array in original order
- Print n smallest elements from given array in their original order
- Maximum value K such that array has at-least K elements that are >= K in C++
- Find original array from encrypted array (An array of sums of other elements) using C++.
- Maximum subarray sum by flipping signs of at most K array elements in C++
- Maximum possible middle element of the array after deleting exactly k elements in C++
- Maximum difference between the group of k-elements and rest of the array in C
- Find Maximum XOR value of a sub-array of size k in C++
- Count maximum elements of an array whose absolute difference does not exceed K in C++
- C# Program to order array elements in descending order
- Find a number that divides maximum array elements in C++
- Find prime number K in an array such that (A[i] % K) is maximum in C++
- Recursive Programs to find Minimum and Maximum elements of array in C++
- Find integers that divides maximum number of elements of the array in C++
- Maximum sum of increasing order elements from n arrays in C++

Advertisements