
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Find the largest three elements in an array in C++
An array is a group of similar data elements stored at contiguous memory locations. It is one of the fundamental data structures in programming and used to store and manage multiple values of the same type.
Here, we are given an array arr[] containing N unsorted elements, and our task is to find the three largest elements from this array.
To understand the problem better. let's see the following example scenarios:
Scenario 1
Input: arr[] = {7, 3, 9, 12, 1} Output: 12, 9, 7
Scenario 2
Input: arr[] = {15, 22, 6, 3, 11, 8} Output: 22, 15, 11
We will solve the given problem of finding the largest three elements from an array using two approaches:
Finding Largest Three Elements Using Linear Search
Linear Search, also known as sequential search, is one of the simplest searching algorithms, which traverses the array and checks each element one by one to find the desired element.
Here, we will traverse the array from start to end and compare each element with the current top three maximum values, max, max2, and max3. And at every step, we will keep comparing and updating these three values of variables.
Algorithm
Here is the following algorithm we will follow to find the largest three elements of a given array using linear search in C++.
- First, create three variables holding elements, max, max2, and max3, and set these values to arr[0].
- Then loop with index i from 1 to n-1 and for each element
- if (arr[i] > max), then store max3 = max2, max2 = max and max = arr[i].
- else if (arr[i] > max2) then store max3 = max2, max2 = arr[i].
- else if (arr[i] > max3) then store max3 = arr[i].
- At the end of the loop, we will print all three values.
Example
In this example, we are finding the largest three elements from the array using the linear search approach:
#include <iostream> using namespace std; void findThreeLargestElements(int arr[], int arr_size) { int max, max2, max3; max3 = max = max2 = arr[0]; for (int i = 0; i < arr_size; i++) { if (arr[i] > max) { max3 = max2; max2 = max; max = arr[i]; } else if (arr[i] > max2) { max3 = max2; max2 = arr[i]; } else if (arr[i] > max3) max3 = arr[i]; } cout << endl << "Three largest elements of the array are " << max << ", " << max2 << ", " << max3; } int main() { int arr[] = {15, 2, 7, 86, 0, 21, 50}; int n = sizeof(arr) / sizeof(arr[0]); cout << "The array is : "; for (int i = 0; i < n; i++) cout << arr[i] << " "; findThreeLargestElements(arr, n); return 0; }
Output
The array is : 15 2 7 86 0 21 50 Three largest elements of the array are 86, 50, 21
Finding Largest Three Elements Using Sorting
Sorting is a process of arranging the elements of an array in ascending or descending order. This can be done manually using algorithms (like bubble sort, selection sort, etc.) or by the built-in sort() function, which is provided in the C++ Standard Library (
Algorithm
Here is the following algorithm we will follow to find the largest three elements of a given array using sort and select in C++.
- First, sort the array using a sorting technique.
- Now print first three elements of an array : arr[0] , arr[1] , arr[2]
Example
In this example, we are finding the largest three elements from the array using the sorting approach:
#include <bits/stdc++.h> using namespace std; void findThreeLargestElements(int arr[], int n) { sort(arr, arr + n, std::greater < > ()); int j = 0; cout << "\nThree largest elements are "; for (int i = 0; i < n; i++) { if (arr[i] != arr[i + 1]) { cout << arr[i] << " "; j++; } if (j == 3) { break; } } } int main() { int arr[] = {15, 2, 7, 86, 0, 21, 50, 53, 50}; int n = sizeof(arr) / sizeof(arr[0]); cout << "The array is : "; for (int i = 0; i < n; i++) cout << arr[i] << "\t"; findThreeLargestElements(arr, n); return 0; }
Output
The array is : 15 2 7 86 0 21 50 53 50 Three largest elements are 86 53 50