C++ Program to Find the Second Largest Element in an Array Last Updated : 30 Apr, 2024 Comments Improve Suggest changes Like Article Like Report In C++, an array is a data structure that is used to store multiple values of similar data types in a contiguous memory location. In this article, we will learn how to find the second largest element in an array in C++. Examples: Input: arr[] = {34, 5, 16, 14, 56, 7, 56} Output: 34 Explanation: The largest element is 56 and the second largest element is 34.Find the Second Largest Element in an Array in C++To find the second largest element in an array in C++, we can initialize two variables, first and second, to the minimum possible value for the data type. Then, we can traverse the array and update the first and second as we find elements larger than them. ApproachInitialize two variables, first and second, to the minimum possible value to keep track of the largest and second largest elements in the array.Start traversing the whole array using a loop.For each element in the array, check if it is greater than the current largest element (first).If it is greater, then update the second largest element (second) to be the current largest element, and update the largest element to be the current element.If the current element is not greater than the largest element but is greater than the second largest element, then update the second largest element to be the current element.C++ Program to Find the Second Largest Element in an Array C++ // C++ Program to illustrate how to find the second largest // element in an array #include <climits> #include <iostream> using namespace std; int main() { // Initialize an array int array[] = { 1, 2, 3, 4, 5 }; int n = sizeof(array) / sizeof(array[0]); // Initialize first and second to the minimum possible // value int first = INT_MIN, second = INT_MIN; // Traverse the array for (int i = 0; i < n; i++) { // If current element is greater than first if (array[i] > first) { second = first; first = array[i]; } // If current element is in between first and second else if (array[i] > second && array[i] < first) { second = array[i]; } } // Print the second largest element cout << "Second Largest Element in the Array: " << second << endl; return 0; } OutputSecond Largest Element in the Array: 4 Time Complexity: O(N), where N is the size of the array.Auxiliary space: O(1) Comment More infoAdvertise with us Next Article C++ Program to Find the Second Largest Element in an Array R rohan_paul Follow Improve Article Tags : C++ Programs C++ cpp-array CPP Examples Practice Tags : CPP Similar Reads C++ Program to Find Largest Element in an Array In this article, we will learn to write a C++ program to find the largest element in the given array arr of size N. The element that is greater than all other elements is the largest element in the array. Recommended PracticeHelp a Thief!!!Try It! One of the most simplest and basic approaches to fin 2 min read C++ Program to Find the K'th largest element in a stream Given an infinite stream of integers, find the k'th largest element at any point of time.Example: Input:stream[] = {10, 20, 11, 70, 50, 40, 100, 5, ...}k = 3Output: {_, _, 10, 11, 20, 40, 50, 50, ...} Extra space allowed is O(k). Recommended: Please solve it on "PRACTICE" first, before moving on to 7 min read C++ Program to Sort the Elements of an Array in Ascending Order Here, we will see how to sort the elements of an array in ascending order using a C++ program. Below are the examples: Input: 3 4 5 8 1 10Output: 1 3 4 5 8 10 Input: 11 34 6 20 40 3Output: 3 6 11 20 34 40 There are 2 ways to sort an array in ascending order in C++: Brute-force Approach Using Bubble 4 min read How to Find the Second Smallest Element in an Array in C++? In C++, arrays are data structures that store the collection of data elements of the same type in contiguous memory locations. In this article, we will learn how to find the second smallest element in an array in C++. Example:Input:myArray = {10, 5, 8, 2, 7, 3, 15};Output:The second smallest element 3 min read C++ Program to Find the Minimum and Maximum Element of an Array Given an array, write functions to find the minimum and maximum elements in it. Example: C++ // C++ program to find minimum (or maximum) element // in an array. #include <bits/stdc++.h> using namespace std; int getMin(int arr[], int n) { int res = arr[0]; for (int i = 1; i < n; i++) res = 3 min read Program to find largest element in an array using Dynamic Memory Allocation Given an array arr[] consisting of N integers, the task is to find the largest element in the given array using Dynamic Memory Allocation. Examples: Input: arr[] = {4, 5, 6, 7} Output: 7Explanation:The largest element present in the given array is 7. Input: arr[] = {8, 9, 10, 12} Output: 12Explanati 5 min read How to Find the Maximum Element of an Array using STL in C++? Given an array of n elements, the task is to find the maximum element using STL in C++.ExamplesInput: arr[] = {11, 13, 21, 45, 8}Output: 45Explanation: 45 is the largest element of the array.Input: arr[] = {1, 9, 2, 5, 7}Output: 9Explanation: 9 is the largest element of the array.STL provides the fo 3 min read C++ Program for Third largest element in an array of distinct elements Given an array of n integers, find the third largest element. All the elements in the array are distinct integers. Example :  Input: arr[] = {1, 14, 2, 16, 10, 20} Output: The third Largest element is 14 Explanation: Largest element is 20, second largest element is 16 and third largest element is 1 5 min read How to Find Largest Number in an Array in C++? In C++, arrays are used to store the collection of similar elements to be stored in adjacent memory locations. They can store data of any type such as int, char, float, etc. In this article, we will learn how to find the largest number in an array in C++. For Example,Input: myVector = {1, 3, 10, 7, 2 min read How to Find the Maximum Element in a List in C++? In C++, a list is a sequence container provided by the STL library that represents a doubly linked list and allows us to store data in non-contiguous memory locations efficiently. In this article, we will learn how to find the maximum element in a list in C++. Example: Input: myList = {30, 20, 10, 5 2 min read Like