
- C++ Basics
- C++ Home
- C++ Overview
- C++ Environment Setup
- C++ Basic Syntax
- C++ Comments
- C++ Data Types
- C++ Variable Types
- C++ Variable Scope
- C++ Constants/Literals
- C++ Modifier Types
- C++ Storage Classes
- C++ Operators
- C++ Loop Types
- C++ Decision Making
- C++ Functions
- C++ Numbers
- C++ Arrays
- C++ Strings
- C++ Pointers
- C++ References
- C++ Date & Time
- C++ Basic Input/Output
- C++ Data Structures
- C++ Object Oriented
- C++ Classes & Objects
- C++ Inheritance
- C++ Overloading
- C++ Polymorphism
- C++ Abstraction
- C++ Encapsulation
- C++ Interfaces
Find the first, second and third minimum elements in an array in C++
Suppose we have an array of n elements. We have to find the first, second and the third minimum elements in the array. First minimum is the minimum of the array, second min is minimum but larger than the first one, and similarly the third min is minimum but greater than second min.
Scan through each element, then check the element, and relate the condition for first, second and third min elements conditions to solve this problem.
Example
#include<iostream> using namespace std; int getThreeMins(int arr[], int n) { int first = INT_MAX, sec = INT_MAX, third = INT_MAX; for (int i = 0; i < n; i++) { if (arr[i] < first) { third = sec; sec = first; first = arr[i]; } else if (arr[i] < sec) { third = sec; sec = arr[i]; } else if (arr[i] < third) third = arr[i]; } cout << "First min = " << first << endl; cout << "Second min = " << sec << endl; cout << "Third min = " << third << endl; } int main() { int array[] = {4, 9, 18, 32, 12}; int n = sizeof(array) / sizeof(array[0]); getThreeMins(array, n); }
Output
First min = 4 Second min = 9 Third min = 12
- Related Articles
- Find the first, second and third minimum elements in an array in C++ program
- Find the smallest and second smallest elements in an array in C++
- Product of maximum in first array and minimum in second in C
- Find elements which are present in first array and not in second in C++
- Elements present in first array and not in second using STL in C++
- Sort the second array according to the elements of the first array in JavaScript
- If 225 students appear in an examination. Among them 4:5 ratio of students passed in first and second division. If 25 students passed in third division and 29 students failed then find 1. Number of students passed in first and second division.2. Find the ratio of students passed in first, second and third division.
- Count elements present in first array but not in second in C++
- Point of View: First, Second & Third Person
- Find difference between first and second largest array element in Java
- Find the sum of first 51 terms of an A.P. whose second and third terms are 14 and 18 respectively.
- Find the sum of first 51 terms of an AP whose second and third terms are 14 and 18 respectively.
- Difference between first and the second array in JavaScript
- Set the first array elements raised to powers from second array element-wise in Numpy
- Return the bases when first array elements are raised to powers from second array in Python

Advertisements