From 2d47d63597c0cb7820187e8428b3819424567f52 Mon Sep 17 00:00:00 2001 From: ma-hii <88180820+ma-hii@users.noreply.github.com> Date: Thu, 20 Oct 2022 12:18:23 +0530 Subject: [PATCH] Create 011_Largest_element_in_Array Find K Largest element in array --- 002_ARRAY/011_Largest_element_in_Array | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) create mode 100644 002_ARRAY/011_Largest_element_in_Array diff --git a/002_ARRAY/011_Largest_element_in_Array b/002_ARRAY/011_Largest_element_in_Array new file mode 100644 index 0000000..e92187c --- /dev/null +++ b/002_ARRAY/011_Largest_element_in_Array @@ -0,0 +1,20 @@ +#include +using namespace std; + void kLargest(int nums[], int n, int k) +{ + sort(nums, nums+n, greater()); + cout << "\nLargest " << k << " Elements: "; + for (int i = 0; i < k; i++) + cout << nums[i] << " "; +} + +int main() +{ + int nums[] = {4, 5, 9, 12, 9, 22, 45, 7}; + int n = sizeof(nums)/sizeof(nums[0]); + cout << "Original array: "; + for (int i=0; i < n; i++) + cout << nums[i] <<" "; + int k = 4; + kLargest(nums, n, k); +}