From aa327722f039583e8a7d78e989d6567b71a9e033 Mon Sep 17 00:00:00 2001 From: Rahul Dabgotra Date: Tue, 20 Oct 2020 06:01:21 +0530 Subject: [PATCH] Create largest-three-elements.cpp C++ program to find the largest three elements in an array. --- largest-three-elements.cpp | 44 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 44 insertions(+) create mode 100644 largest-three-elements.cpp diff --git a/largest-three-elements.cpp b/largest-three-elements.cpp new file mode 100644 index 0000000..6876f6c --- /dev/null +++ b/largest-three-elements.cpp @@ -0,0 +1,44 @@ +//C++ program to find the largest three elements in an array. +#include +using namespace std; + +void three_largest(int arr[], int arr_size) + { + int i, first, second, third; + + if (arr_size < 3) + { + cout << "Invalid Input"; + } + + third = first = second = INT_MIN; + for (i = 0; i < arr_size ; i ++) + { + if (arr[i] > first) + { + third = second; + second = first; + first = arr[i]; + } + else if (arr[i] > second) + { + third = second; + second = arr[i]; + } + + else if (arr[i] > third) + third = arr[i]; + } + + cout << "\nThree largest elements are: " <