fork download
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3.  
  4. void kLargest(int arr[], int n, int k)
  5. {
  6.  
  7. sort(arr, arr + n, greater<int>());
  8.  
  9.  
  10. for (int i = 0; i < k; i++)
  11. cout << arr[i] << " ";
  12. }
  13.  
  14.  
  15. int main()
  16. {
  17. int arr[] = { 1, 23, 12, 9, 30, 2, 50 };
  18. int n = sizeof(arr) / sizeof(arr[0]);
  19. int k = 3;
  20. kLargest(arr, n, k);
  21. }
  22.  
  23.  
Success #stdin #stdout 0.01s 5440KB
stdin
Standard input is empty
stdout
50 30 23