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 first, second and third minimum elements in an array in C++ program
In this article, we need to identify the three minimum elements in an array: minimum, second minimum, and third minimum. We will look at two different approaches to solve this problem and compare their time complexity.
Problem Statement
Given an array of n elements, we need to find the first, second, and third minimum elements in the array.
The first minimum is the minimum of the array's elements, the second is a minimum but larger than the first, and the third is a minimum but larger than the second.
Let us see the following example scenario to understand the problem better:
Scenario 1
Input: arr = [10, 8, 7, 4, 2] Output: 1st min = 2 2nd min = 4 3rd min = 7
Scenario 2
Input: arr = [4, 9, 1, 32, 12] Output: 1st min = 1 2nd min = 4 3rd min = 9
Find 1st, 2nd, and 3rd Minimum: Naive Approach
In this approach, we first sort the array and then print the first three elements:
Example
In the following C++ example, we print the first three minimum elements using the naive approach:
#include <bits/stdc++.h>
using namespace std;
void ThreeMinElement(int array[], int n) {
// Sorting the array
sort(array, array + n);
if (n < 3) {
cout << "Array has less than 3 elements.\n";
return;
}
cout << "1st min = " << array[0] << "\n";
cout << "2nd min = " << array[1] << "\n";
cout << "3rd min = " << array[2] << "\n";
}
int main() {
int array[] = {4, 9, 1, 32, 12};
int n = sizeof(array) / sizeof(array[0]);
ThreeMinElement(array, n);
return 0;
}
Following is the output of the code:
1st min = 1 2nd min = 4 3rd min = 9
Find 1st, 2nd, and 3rd Minimum: Linear Approach
We traverse the array only once, and at each step we update the three minimums, without sorting the array. Let's see the algorithm/pseudo code:
- Initialize:
first = INT_MAX,second = INT_MAX,third = INT_MAX -
Traverse each element:
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]- Print first, second, and third
Example
This is another example of the C++ to print the first, second, and third minimum elements in an array:
#include <bits/stdc++.h>
using namespace std;
void threeMinimumElement(int array[], int n) {
if (n < 3) {
cout << "Array must have at least 3 elements.\n";
return;
}
int first = INT_MAX, second = INT_MAX, third = INT_MAX;
for (int i = 0; i < n; i++) {
if (array[i] < first) {
third = second;
second = first;
first = array[i];
}
else if (array[i] < second) {
third = second;
second = array[i];
}
else if (array[i] < third) {
third = array[i];
}
}
cout << "First min = " << first << "\n";
cout << "Second min = " << second << "\n";
cout << "Third min = " << third << "\n";
}
int main() {
int array[] = {10, 8, 7, 4, 2};
int n = sizeof(array) / sizeof(array[0]);
threeMinimumElement(array, n);
return 0;
}
Following is the output of the code:
First min = 2 Second min = 4 Third min = 7