Find the first, second and third minimum elements in an array
Last Updated :
09 Mar, 2023
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).
C++
// CPP program to find the first, second
// and third minimum element in an array
#include<bits/stdc++.h>
using namespace std;
int Print3Smallest(int array[], int n)
{
// Sorting the array
sort(array,array+n);
cout << "First min = " << array[0] << "\n";
cout << "Second min = " << array[1] << "\n";
cout << "Third min = " << array[2] << "\n";
}
// Driver code
int main()
{
int array[] = {4, 9, 1, 32, 12};
int n = sizeof(array) / sizeof(array[0]);
Print3Smallest(array, n);
return 0;
}
// Code submitted by Pushpesh Raj
Java
// Java program to find the first, second
// and third minimum element in an array
import java.util.Arrays;
public class Main {
static void Print3Smallest(int array[], int n) {
// Sorting the array
Arrays.sort(array);
System.out.println("First min = " + array[0]);
System.out.println("Second min = " + array[1]);
System.out.println("Third min = " + array[2]);
}
// 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 bhardwajji
Python3
# Python program to find the first, second
# and third minimum element in an array
def print_3_smallest(array):
# Sorting the array
array.sort()
print("First min =", array[0])
print("Second min =", array[1])
print("Third min =", array[2])
# Driver code
if __name__ == '__main__':
array = [4, 9, 1, 32, 12]
n = len(array)
print_3_smallest(array)
C#
// C# program to find the first, second
// and third minimum element in an array
using System;
using System.Linq;
class Program
{
static void Print3Smallest(int[] array, int n)
{
// Sorting the array
Array.Sort(array);
Console.WriteLine("First min = " + array[0]);
Console.WriteLine("Second min = " + array[1]);
Console.WriteLine("Third min = " + array[2]);
}
// Driver code
static void Main()
{
int[] array = {4, 9, 1, 32, 12};
int n = array.Length;
Print3Smallest(array, n);
}
}
JavaScript
// JavaScript program to find the first,
// second and third minimum element in an
// array.
function Print3Smallest(array,n){
// Sorting the array
array.sort((a, b) => a - b);
console.log('First min = ' + array[0]);
console.log('Second min = ' + array[1]);
console.log('Third min = ' + array[2]);
}
// Driver code
let array = [4, 9, 1, 32, 12];
Print3Smallest(array,array.length);
OutputFirst min = 1
Second min = 4
Third min = 9
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
Implementation:
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 update 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
?>
JavaScript
<script>
// Javascript program to find the first, second
// and third minimum element in an array
let MAX = 100000
function Print3Smallest( array, n)
{
let firstmin = MAX, secmin = MAX, thirdmin = MAX;
for (let 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];
}
document.write("First min = " + firstmin + "</br>");
document.write("Second min = " + secmin + "</br>");
document.write("Third min = " + thirdmin + "</br>");
}
// Driver program
let array = [4, 9, 1, 32, 12];
let n = array.length;
Print3Smallest(array, n);
</script>
OutputFirst min = 1
Second min = 4
Third min = 9
Time Complexity: O(n)
Auxiliary Space: O(1)
Similar Reads
Find the smallest and second smallest elements in an array Given an array arr[] of size N, find the smallest and second smallest element in an array.Examples:Input: arr[] = {12, 13, 1, 10, 34, 1}Output: 1 10Explanation: The smallest element is 1 and second smallest element is 10.Input: arr[] = {111, 13, 25, 9, 34, 1}Output: 1 9Explanation: The smallest elem
12 min read
Find the minimum and maximum sum of N-1 elements of the array Given an unsorted array A of size N, the task is to find the minimum and maximum values that can be calculated by adding exactly N-1 elements. Examples: Input: a[] = {13, 5, 11, 9, 7} Output: 32 40 Explanation: Minimum sum is 5 + 7 + 9 + 11 = 32 and maximum sum is 7 + 9 + 11 + 13 = 40.Input: a[] = {
10 min read
Program to find the minimum (or maximum) element of an array Given an array, write functions to find the minimum and maximum elements in it. Examples:Input: arr[] = [1, 423, 6, 46, 34, 23, 13, 53, 4]Output: Minimum element of array: 1 Maximum element of array: 423Input: arr[] = [2, 4, 6, 7, 9, 8, 3, 11]Output: Minimum element of array: 2 Maximum element of ar
14 min read
Recursive Programs to find Minimum and Maximum elements of array Given an array of integers arr, the task is to find the minimum and maximum element of that array using recursion. Examples : Input: arr = {1, 4, 3, -5, -4, 8, 6}; Output: min = -5, max = 8 Input: arr = {1, 4, 45, 6, 10, -8}; Output: min = -8, max = 45 Recursive approach to find the Minimum element
7 min read
Sum of all minimum occurring elements in an Array Given an array of integers containing duplicate elements. The task is to find the sum of all least occurring elements in the given array. That is the sum of all such elements whose frequency is minimum in the array.Examples: Input : arr[] = {1, 1, 2, 2, 3, 3, 3, 3} Output : 2 The least occurring ele
5 min read
Queries for the minimum element in an array excluding the given index range Given an array arr[] of N integers and Q queries where each query consists of an index range [L, R]. For each query, the task is to find the minimum element in the array excluding the elements from the given index range. Examples: Input: arr[] = {3, 2, 1, 4, 5}, Q[][] = {{1, 2}, {2, 3}} Output: 3 2
15+ min read
Find minimum possible size of array with given rules for removing elements Given an array of numbers and a constant k, minimize size of array with following rules for removing elements. Exactly three elements can be removed at one go.The removed three elements must be adjacent in array, i.e., arr[i], arr[i+1], arr[i+2]. And the second element must be k greater than first a
11 min read
Find K-th smallest element in an array for multiple queries Given an array arr[] of size N and an array Q[][] consisting of M queries that needs to be processed on the given array. It is known that these queries can be of the following two types: Type 1: If Q = 1, then add an element in the array {type, element_to_add}.Type 2: If Q = 2, then print the K-th s
9 min read
Remove minimum elements from array so that max <= 2 * min Given an array arr, the task is to remove minimum number of elements such that after their removal, max(arr) <= 2 * min(arr). Examples: Input: arr[] = {4, 5, 3, 8, 3} Output: 1 Remove 8 from the array. Input: arr[] = {1, 2, 3, 4} Output: 1 Remove 1 from the array. Approach: Let us fix each value
6 min read
Minimum in an array which is first decreasing then increasing Given an array of N integers where array elements form a strictly decreasing and increasing sequence. The task is to find the smallest number in such an array. Constraints: N >= 3 Examples: Input: a[] = {2, 1, 2, 3, 4}Output: 1Input: a[] = {8, 5, 4, 3, 4, 10}Output: 3 A naive approach is to linea
12 min read