Find the first, second and third minimum elements in an array in O(n).
Examples:
Input : 9 4 12 6
Output : First min = 4
Second min = 6
Third min = 9
Input : 4 9 1 32 12
Output : First min = 1
Second min = 4
Third min = 9
First approach : First we can use normal method that is sort the array and then print first, second and third element of the array. Time complexity of this solution is O(n Log n).
Second approach : Time complexity of this solution is O(n).
Algorithm-
First take an element
then if array[index] < Firstelement
Thirdelement = Secondelement
Secondelement = Firstelement
Firstelement = array[index]
else if array[index] < Secondelement
Thirdelement = Secondelement
Secondelement = array[index]
else if array[index] < Thirdelement
Thirdelement = array[index]
then print all the element
C++
// CPP program to find the first, second // and third minimum element in an array #include<bits/stdc++.h> #define MAX 100000 using namespace std; int Print3Smallest(int array[], int n) { int firstmin = MAX, secmin = MAX, thirdmin = MAX; for (int i = 0; i < n; i++) { /* Check if current element is less than firstmin, then update first, second and third */ if (array[i] < firstmin) { thirdmin = secmin; secmin = firstmin; firstmin = array[i]; } /* Check if current element is less than secmin then update second and third */ else if (array[i] < secmin) { thirdmin = secmin; secmin = array[i]; } /* Check if current element is less than then update third */ else if (array[i] < thirdmin) thirdmin = array[i]; } cout << "First min = " << firstmin << "\n"; cout << "Second min = " << secmin << "\n"; cout << "Third min = " << thirdmin << "\n"; } // Driver code int main() { int array[] = {4, 9, 1, 32, 12}; int n = sizeof(array) / sizeof(array[0]); Print3Smallest(array, n); return 0; } |
Java
// Java program to find the first, second // and third minimum element in an array import java.util.*; public class GFG { static void Print3Smallest(int array[], int n) { int firstmin = Integer.MAX_VALUE; int secmin = Integer.MAX_VALUE; int thirdmin = Integer.MAX_VALUE; for (int i = 0; i < n; i++) { /* Check if current element is less than firstmin, then update first, second and third */ if (array[i] < firstmin) { thirdmin = secmin; secmin = firstmin; firstmin = array[i]; } /* Check if current element is less than secmin then update second and third */ else if (array[i] < secmin) { thirdmin = secmin; secmin = array[i]; } /* Check if current element is less than then update third */ else if (array[i] < thirdmin) thirdmin = array[i]; } System.out.println("First min = " + firstmin ); System.out.println("Second min = " + secmin ); System.out.println("Third min = " + thirdmin ); } // Driver code public static void main(String[] args) { int array[] = {4, 9, 1, 32, 12}; int n = array.length; Print3Smallest(array, n); } } // This code is contributed by // Sam007 |
Python3
# A Python program to find the first, # second and third minimum element # in an array MAX = 100000 def Print3Smallest(arr, n): firstmin = MAX secmin = MAX thirdmin = MAX for i in range(0, n): # Check if current element # is less than firstmin, # then update first,second # and third if arr[i] < firstmin: thirdmin = secmin secmin = firstmin firstmin = arr[i] # Check if current element is # less than secmin then update # second and third elif arr[i] < secmin: thirdmin = secmin secmin = arr[i] # Check if current element is # less than,then upadte third elif arr[i] < thirdmin: thirdmin = arr[i] print("First min = ", firstmin) print("Second min = ", secmin) print("Third min = ", thirdmin) # driver program arr = [4, 9, 1, 32, 12] n = len(arr) Print3Smallest(arr, n) # This code is contributed by Shrikant13. |
C#
// C# program to find the first, second // and third minimum element in an array using System; class GFG { static void Print3Smallest(int []array, int n) { int firstmin = int.MaxValue; int secmin = int.MaxValue; int thirdmin = int.MaxValue; for (int i = 0; i < n; i++) { /* Check if current element is less than firstmin, then update first, second and third */ if (array[i] < firstmin) { thirdmin = secmin; secmin = firstmin; firstmin = array[i]; } /* Check if current element is less than secmin then update second and third */ else if (array[i] < secmin) { thirdmin = secmin; secmin = array[i]; } /* Check if current element is less than then update third */ else if (array[i] < thirdmin) thirdmin = array[i]; } Console.WriteLine("First min = " + firstmin ); Console.WriteLine("Second min = " + secmin ); Console.WriteLine("Third min = " + thirdmin ); } // Driver code static void Main() { int []array = new int[]{4, 9, 1, 32, 12}; int n = array.Length; Print3Smallest(array, n); } } // This code is contributed by Sam007 |
PHP
<?php // php program to find the first, second // and third minimum element in an array function Print3Smallest($array, $n) { $MAX = 100000; $firstmin = $MAX; $secmin = $MAX; $thirdmin = $MAX; for ($i = 0; $i < $n; $i++) { /* Check if current element is less than firstmin, then update first, second and third */ if ($array[$i] < $firstmin) { $thirdmin = $secmin; $secmin = $firstmin; $firstmin = $array[$i]; } /* Check if current element is less than secmin then update second and third */ else if ($array[$i] < $secmin) { $thirdmin = $secmin; $secmin = $array[$i]; } /* Check if current element is less than then update third */ else if ($array[$i] < $thirdmin) $thirdmin = $array[$i]; } echo "First min = ".$firstmin."\n"; echo "Second min = ".$secmin."\n"; echo "Third min = ".$thirdmin."\n"; } // Driver code $array= array(4, 9, 1, 32, 12); $n = sizeof($array) / sizeof($array[0]); Print3Smallest($array, $n); // This code is contributed by mits ?> |
Output:
First min = 1 Second min = 4 Third min = 9
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.
Recommended Posts:
- Find minimum value to assign all array elements so that array product becomes greater
- Find the minimum and maximum sum of N-1 elements of the array
- Find minimum changes required in an array for it to contain k distinct elements
- Recursive Programs to find Minimum and Maximum elements of array
- Find the minimum number of elements that should be removed to make an array good
- Find the minimum number of operations required to make all array elements equal
- Flip minimum signs of array elements to get minimum sum of positive elements possible
- Merge first two minimum elements of the array until all the elements are greater than K
- Find original array from encrypted array (An array of sums of other elements)
- Find elements larger than half of the elements in an array
- Find minimum difference between any two elements | Set 2
- Find minimum elements after considering all possible transformations
- Find minimum difference between any two elements
- Find minimum sum such that one of every three consecutive elements is taken
- Minimum value among AND of elements of every subset of an array
- Remove minimum elements from the array such that 2*min becomes more than max
- Sum of all minimum occurring elements in an Array
- Remove minimum elements from array so that max <= 2 * min
- Find all elements in array which have at-least two greater elements
- Find Kth element in an array containing odd elements first and then even elements
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.

