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: " <