C++ Program for Third largest element in an array of distinct elements
Last Updated :
03 Jan, 2022
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 14
Input: arr[] = {19, -10, 20, 14, 2, 16, 10}
Output: The third Largest element is 16
Explanation: Largest element is 20, second largest element is 19
and third largest element is 16
Â
Naive Approach: The task is to first find the largest element, followed by the second-largest element and then excluding them both find the third-largest element. The basic idea is to iterate the array twice and mark the maximum and second maximum element and then excluding them both find the third maximum element, i.e the maximum element excluding the maximum and second maximum.
Â
- Algorithm:Â
- First, iterate through the array and find maximum.
- Store this as first maximum along with its index.
- Now traverse the whole array finding the second max, excluding the maximum element.
- Finally traverse the array the third time and find the third largest element i.e., excluding the maximum and second maximum.
Â
C++
// C++ program to find third Largest
// element in an array of distinct elements
#include <bits/stdc++.h>
void thirdLargest(int arr[], int arr_size)
{
/* There should be atleast three elements */
if (arr_size < 3)
{
printf(" Invalid Input ");
return;
}
// Find first largest element
int first = arr[0];
for (int i = 1; i < arr_size ; i++)
if (arr[i] > first)
first = arr[i];
// Find second largest element
int second = INT_MIN;
for (int i = 0; i < arr_size ; i++)
if (arr[i] > second && arr[i] < first)
second = arr[i];
// Find third largest element
int third = INT_MIN;
for (int i = 0; i < arr_size ; i++)
if (arr[i] > third && arr[i] < second)
third = arr[i];
printf("The third Largest element is %d
", third);
}
/* Driver program to test above function */
int main()
{
int arr[] = {12, 13, 1, 10, 34, 16};
int n = sizeof(arr)/sizeof(arr[0]);
thirdLargest(arr, n);
return 0;
}
The third Largest element is 13
- Complexity Analysis:Â
- Time Complexity: O(n).Â
As the array is iterated thrice and is done in a constant time - Space complexity: O(1).Â
No extra space is needed as the indices can be stored in constant space.
Efficient Approach: The problem deals with finding the third largest element in the array in a single traversal. The problem can be cracked by taking help of a similar problem- finding the second maximum element. So the idea is to traverse the array from start to end and to keep track of the three largest elements up to that index (stored in variables). So after traversing the whole array, the variables would have stored the indices (or value) of the three largest elements of the array.
Â
- Algorithm:Â
- Create three variables, first, second, third, to store indices of three largest elements of the array. (Initially all of them are initialized to a minimum value).
- Move along the input array from start to the end.
- For every index check if the element is larger than first or not. Update the value of first, if the element is larger, and assign the value of first to second and second to third. So the largest element gets updated and the elements previously stored as largest becomes second largest, and the second largest element becomes third largest.
- Else if the element is larger than the second, then update the value of second,and the second largest element becomes third largest.
- If the previous two conditions fail, but the element is larger than the third, then update the third.
- Print the value of third after traversing the array from start to end
Â
C++
// C++ program to find third
// Largest element in an array
#include <bits/stdc++.h>
void thirdLargest(int arr[], int arr_size)
{
/* There should be atleast three elements */
if (arr_size < 3)
{
printf(" Invalid Input ");
return;
}
// Initialize first, second and third Largest element
int first = arr[0], second = INT_MIN, third = INT_MIN;
// Traverse array elements to find the third Largest
for (int i = 1; i < arr_size ; i ++)
{
/* If current element is greater than first,
then update first, second and third */
if (arr[i] > first)
{
third = second;
second = first;
first = arr[i];
}
/* If arr[i] is in between first and second */
else if (arr[i] > second)
{
third = second;
second = arr[i];
}
/* If arr[i] is in between second and third */
else if (arr[i] > third)
third = arr[i];
}
printf("The third Largest element is %d
", third);
}
/* Driver program to test above function */
int main()
{
int arr[] = {12, 13, 1, 10, 34, 16};
int n = sizeof(arr)/sizeof(arr[0]);
thirdLargest(arr, n);
return 0;
}
The third Largest element is 13
- Complexity Analysis:
- Time Complexity: O(n).Â
As the array is iterated once and is done in a constant time - Space complexity: O(1).Â
No extra space is needed as the indices can be stored in constant space.
Please refer complete article on
Third largest element in an array of distinct elements for more details!
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 Second Largest Element in an Array 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
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
Maximize frequency of an element by at most one increment or decrement of all array elements Given an array arr[] of size N, the task is to find the maximum frequency of any array element by incrementing or decrementing each array element by 1 at most once. Examples: Input: arr[] = { 3, 1, 4, 1, 5, 9, 2 } Output: 4 Explanation: Decrementing the value of arr[0] by 1 modifies arr[] to { 2, 1,
8 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
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
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
C++ Program to Maximize elements using another array Given two arrays with size n, maximize the first array by using the elements from the second array such that the new array formed contains n greatest but unique elements of both the arrays giving the second array priority (All elements of second array appear before first array). The order of appeara
4 min read
C++ Program to Find k maximum elements of array in original order Given an array arr[] and an integer k, we need to print k maximum elements of given array. The elements should printed in the order of the input.Note : k is always less than or equal to n. Examples: Input : arr[] = {10 50 30 60 15} k = 2 Output : 50 60 The top 2 elements are printed as per their app
3 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