Given a huge list of Investors with id, name, net worth, etc. Write an efficient code for printing top k investors in the list.
For example, if given list is - [Investor A:(networth:20L),Investor B:(networth:25L),Investor C:(networth:10L), Investor D:(networth:50L), Investor E:(networth:30L)] and you are asked for the largest 3 investors i.e., k = 3 then your program should display Investors D, E and B data.
my code -
#include <bits/stdc++.h>
using namespace std;
void kLargest(int arr[], int n, int k)
{
sort(arr, arr + n, greater<int>());
for (int i = 0; i < k; i++)
cout << arr[i] << " ";
}
int main()
{
int arr[] = { 20, 25, 10, 50, 30};
int n = sizeof(arr) / sizeof(arr[0]);
int k = 3;
kLargest(arr, n, k);
}
Aucun commentaire:
Enregistrer un commentaire