A Simple Solution is to sort the array in increasing order. The first two elements in the sorted array would be the two smallest elements. In this approach, if the smallest element is present more than one time then we will have to use a loop for printing the unique smallest and second smallest elements.
Below is the implementation of the above approach:
C++
//C++ simple approach to print smallest
//and second smallest element.
#include<bits/stdc++.h>
usingnamespacestd;
intmain() {
intarr[]={111, 13, 25, 9, 34, 1};
intn=sizeof(arr)/sizeof(arr[0]);
//sorting the array using
//in-built sort function
sort(arr,arr+n);
//printing the desired element
cout<<"smallest element is "<<arr[0]<<endl;
cout<<"second smallest element is "<<arr[1];
return0;
}
//this code is contributed by Machhaliya Muhammad
Java
/*package whatever //do not write package name here */
importjava.io.*;
importjava.util.*;
classGFG {
//Java simple approach to print smallest
//and second smallest element.
// Driver Code
publicstaticvoidmain(String args[])
{
intarr[]={111, 13, 25, 9, 34, 1};
intn=arr.length;
// sorting the array using
// in-built sort function
Arrays.sort(arr);
// printing the desired element
System.out.println("smallest element is "+arr[0]);
System.out.println("second smallest element is "+arr[1]);
}
}
// This code is contributed by shinjanpatra
Python3
# Python3 simple approach to print smallest
# and second smallest element.
# driver code
arr =[111, 13, 25, 9, 34, 1]
n =len(arr)
# sorting the array using
# in-built sort function
arr.sort()
# printing the desired element
print("smallest element is "+str(arr[0]))
print("second smallest element is "+str(arr[1]))
# This code is contributed by shinjanpatra
C#
// C# simple approach to print smallest
// and second smallest element.
usingSystem;
publicclassGFG
{
// Driver Code
staticpublicvoidMain()
{
int[] arr = {111, 13, 25, 9, 34, 1};
intn = arr.Length;
// sorting the array using
// in-built sort function
Array.Sort(arr);
// printing the desired element
Console.WriteLine("smallest element is "+arr[0]);
Console.WriteLine("second smallest element is "+arr[1]);
}
}
// This code is contributed by kothavvsaakash
Javascript
<script>
// JavaScript simple approach to print smallest
// and second smallest element.
// driver code
let arr = [111, 13, 25, 9, 34, 1];
let n = arr.length;
// sorting the array using
// in-built sort function
arr.sort();
// printing the desired element
document.write("smallest element is "+arr[0],"</br>");
document.write("second smallest element is "+arr[1],"</br>");
// This code is contributed by shinjanpatra
</script>
Output
smallest element is 1
second smallest element is 9
Time complexity: O(N*logN) Auxiliary space: O(1)
An Efficient Approach: ABetter Solutionis to scan the array twice. In the first traversal find the minimum element. Let this element be x. In the second traversal, find the smallest element greater than x.
Using this method, we can overcome the problem of Method 1 which occurs when the smallest element is present in an array more than one time. The above solution requires two traversals of the input array.
C++
// C++ program to find smallest and
// second smallest element in array
#include <bits/stdc++.h>
usingnamespacestd;
intmain()
{
intarr[] = {12, 13, 1, 10, 34, 1};
intn = sizeof(arr) / sizeof(arr[0]);
intsmallest = INT_MAX;
// traversing the array to find
// smallest element.
for(inti = 0; i < n; i++)
{
if(arr[i] < smallest)
{
smallest = arr[i];
}
}
cout << "smallest element is: "<< smallest << endl;
intsecond_smallest = INT_MAX;
// traversing the array to find second smallest element
for(inti = 0; i < n; i++)
{
if(arr[i] < second_smallest && arr[i] > smallest)
{
second_smallest = arr[i];
}
}
cout << "second smallest element is: "<< second_smallest << endl;
return0;
}
// This code is contributed by Machhaliya Muhamma
Java
// Java program to find smallest and
// second smallest element in array
importjava.io.*;
classGFG {
publicstaticvoidmain(String args[])
{
intarr[] = { 12, 13, 1, 10, 34, 1};
intn = arr.length;
intsmallest = Integer.MAX_VALUE;
// traversing the array to find
// smallest element.
for(inti = 0; i < n; i++) {
if(arr[i] < smallest) {
smallest = arr[i];
}
}
System.out.println("smallest element is: "
+ smallest);
intsecond_smallest = Integer.MAX_VALUE;
// traversing the array to find second smallest
// element
for(inti = 0; i < n; i++) {
if(arr[i] < second_smallest
&& arr[i] > smallest) {
second_smallest = arr[i];
}
}
System.out.println("second smallest element is: "
+ second_smallest);
}
}
// This code is contributed by Lovely Jain
Python
# python program to find smallest and second smallest element in array
# import the module
importsys
arr =[12, 13, 1, 10, 34, 1]
n =len(arr)
smallest =sys.maxint
# traversing the array to find smallest element.
fori inrange(n):
if(arr[i] < smallest):
smallest =arr[i]
print('smallest element is: '+str(smallest))
second_smallest =sys.maxint
# traversing the array to find second smallest element
print('second smallest element is: '+str(second_smallest))
# This code is contributed by lokeshmvs21.
C#
// C# program to find smallest and
// second smallest element in array
usingSystem;
publicclassGFG
{
staticpublicvoidMain ()
{
int[] arr = { 12, 13, 1, 10, 34, 1 };
intn = arr.Length;
intsmallest = Int32.MaxValue;
// traversing the array to find
// smallest element.
for(inti = 0; i < n; i++)
{
if(arr[i] < smallest)
{
smallest = arr[i];
}
}
Console.WriteLine("smallest element is: "+ smallest);
intsecond_smallest = Int32.MaxValue;
// traversing the array to find second smallest
// element
for(inti = 0; i < n; i++)
{
if(arr[i] < second_smallest && arr[i] > smallest)
{
second_smallest = arr[i];
}
}
Console.WriteLine("second smallest element is: "+ second_smallest);
}
}
// This code is contributed by kothavvsaakash
Javascript
<script>
// Javascript program to find smallest and
// second smallest elements
functionsolution( arr, arr_size)
{
let first = Number.MAX_VALUE,
second = Number.MAX_VALUE;
/* There should be atleast two elements */
if(arr_size < 2)
{
document.write(" Invalid Input ");
return;
}
/* find the smallest element */
for(let i = 0; i < arr_size ; i ++)
{
if(arr[i] < first){
first = arr[i];
}
}
/* find the second smallest element */
for(let i = 0; i < arr_size ; i ++){
if(arr[i] < second && arr[i] > first){
second = arr[i];
}
}
if(second == Number.MAX_VALUE )
document.write("There is no second smallest element\n");
else
document.write("The smallest element is "+ first + " and second "+
"Smallest element is "+ second +'\n');
}
// Driver program
let arr = [ 12, 13, 1, 10, 34, 1 ];
let n = arr.length;
solution(arr, n);
</script>
Output
smallest element is: 1
second smallest element is: 10
Time complexity: O(N) Auxiliary space: O(1)
An Efficient Solution can find the minimum two elements in one traversal. Below is the complete algorithm. Algorithm:
1) Initialize both first and second smallest as INT_MAX
first = second = INT_MAX
2) Loop through all the elements.
a) If the current element is smaller than first, then update first
and second.
b) Else if the current element is smaller than second then update
second
Below is the implementation of the above approach:
C++
// C++ program to find smallest and
// second smallest elements
#include <bits/stdc++.h>
usingnamespacestd; /* For INT_MAX */
voidprint2Smallest(intarr[], intarr_size)
{
inti, first, second;
/* There should be atleast two elements */
if(arr_size < 2)
{
cout<<" Invalid Input ";
return;
}
first = second = INT_MAX;
for(i = 0; i < arr_size ; i ++)
{
/* If current element is smaller than first
then update both first and second */
if(arr[i] < first)
{
second = first;
first = arr[i];
}
/* If arr[i] is in between first and second
then update second */
elseif(arr[i] < second && arr[i] != first)
second = arr[i];
}
if(second == INT_MAX)
cout << "There is no second smallest element\n";
else
cout << "The smallest element is "<< first << " and second "
"Smallest element is "<< second << endl;
}
/* Driver code */
intmain()
{
intarr[] = {12, 13, 1, 10, 34, 1};
intn = sizeof(arr)/sizeof(arr[0]);
print2Smallest(arr, n);
return0;
}
// This is code is contributed by rathbhupendra
C
// C program to find smallest and second smallest elements
#include <stdio.h>
#include <limits.h> /* For INT_MAX */
voidprint2Smallest(intarr[], intarr_size)
{
inti, first, second;
/* There should be atleast two elements */
if(arr_size < 2)
{
printf(" Invalid Input ");
return;
}
first = second = INT_MAX;
for(i = 0; i < arr_size ; i ++)
{
/* If current element is smaller than first
then update both first and second */
if(arr[i] < first)
{
second = first;
first = arr[i];
}
/* If arr[i] is in between first and second
then update second */
elseif(arr[i] < second && arr[i] != first)
second = arr[i];
}
if(second == INT_MAX)
printf("There is no second smallest element\n");
else
printf("The smallest element is %d and second "
"Smallest element is %d\n", first, second);
}
/* Driver program to test above function */
intmain()
{
intarr[] = {12, 13, 1, 10, 34, 1};
intn = sizeof(arr)/sizeof(arr[0]);
print2Smallest(arr, n);
return0;
}
Java
// Java program to find smallest and second smallest elements
importjava.io.*;
classSecondSmallest
{
/* Function to print first smallest and second smallest
elements */
staticvoidprint2Smallest(intarr[])
{
intfirst, second, arr_size = arr.length;
/* There should be atleast two elements */
if(arr_size < 2)
{
System.out.println(" Invalid Input ");
return;
}
first = second = Integer.MAX_VALUE;
for(inti = 0; i < arr_size ; i ++)
{
/* If current element is smaller than first
then update both first and second */
if(arr[i] < first)
{
second = first;
first = arr[i];
}
/* If arr[i] is in between first and second
then update second */
elseif(arr[i] < second && arr[i] != first)
second = arr[i];
}
if(second == Integer.MAX_VALUE)
System.out.println("There is no second"+
"smallest element");
else
System.out.println("The smallest element is "+
first + " and second Smallest"+
" element is "+ second);
}
/* Driver program to test above functions */
publicstaticvoidmain (String[] args)
{
intarr[] = {12, 13, 1, 10, 34, 1};
print2Smallest(arr);
}
}
/*This code is contributed by Devesh Agrawal*/
Python3
# Python program to find smallest and second smallest elements
importmath
defprint2Smallest(arr):
# There should be atleast two elements
arr_size =len(arr)
ifarr_size < 2:
print("Invalid Input")
return
first =second =math.inf
fori inrange(0, arr_size):
# If current element is smaller than first then
# update both first and second
ifarr[i] < first:
second =first
first =arr[i]
# If arr[i] is in between first and second then
# update second
elif(arr[i] < second andarr[i] !=first):
second =arr[i];
if(second ==math.inf):
print("No second smallest element")
else:
print('The smallest element is',first,'and', \
' second smallest element is',second)
# Driver function to test above function
arr =[12, 13, 1, 10, 34, 1]
print2Smallest(arr)
# This code is contributed by Devesh Agrawal
C#
// C# program to find smallest
// and second smallest elements
usingSystem;
classGFG
{
/* Function to print first smallest
and second smallest elements */
staticvoidprint2Smallest(int[]arr)
{
intfirst, second, arr_size = arr.Length;
/* There should be atleast two elements */
if(arr_size < 2)
{
Console.Write(" Invalid Input ");
return;
}
first = second = int.MaxValue;
for(inti = 0; i < arr_size ; i ++)
{
/* If current element is smaller than first
then update both first and second */
if(arr[i] < first)
{
second = first;
first = arr[i];
}
/* If arr[i] is in between first and second
then update second */
elseif(arr[i] < second && arr[i] != first)
second = arr[i];
}
if(second == int.MaxValue)
Console.Write("There is no second"+
"smallest element");
else
Console.Write("The smallest element is "+
first + " and second Smallest"+
" element is "+ second);
}
/* Driver program to test above functions */
publicstaticvoidMain()
{
int[]arr = {12, 13, 1, 10, 34, 1};
print2Smallest(arr);
}
}
// This code is contributed by Sam007
PHP
<?php
// PHP program to find smallest and
// second smallest elements
functionprint2Smallest($arr, $arr_size)
{
$INT_MAX= 2147483647;
/* There should be atleast
two elements */
if($arr_size< 2)
{
echo(" Invalid Input ");
return;
}
$first= $second= $INT_MAX;
for($i= 0; $i< $arr_size; $i++)
{
/* If current element is
smaller than first then
update both first and
second */
if($arr[$i] < $first)
{
$second= $first;
$first= $arr[$i];
}
/* If arr[i] is in between
first and second then
update second */
elseif($arr[$i] < $second&&
$arr[$i] != $first)
$second= $arr[$i];
}
if($second== $INT_MAX)
echo("There is no second smallest element\n");
else
echo"The smallest element is ",$first
," and second Smallest element is "
, $second;
}
// Driver Code
$arr= array(12, 13, 1, 10, 34, 1);
$n= count($arr);
print2Smallest($arr, $n)
// This code is contributed by Smitha
?>
Javascript
<script>
// Javascript program to find smallest and
// second smallest elements
functionprint2Smallest( arr, arr_size)
{
let i, first, second;
/* There should be atleast two elements */
if(arr_size < 2)
{
document.write(" Invalid Input ");
return;
}
first=Number.MAX_VALUE ;
second=Number.MAX_VALUE ;
for(i = 0; i < arr_size ; i ++)
{
/* If current element is smaller than first
then update both first and second */
if(arr[i] < first)
{
second = first;
first = arr[i];
}
/* If arr[i] is in between first and second
then update second */
elseif(arr[i] < second && arr[i] != first)
second = arr[i];
}
if(second == Number.MAX_VALUE )
document.write("There is no second smallest element\n");
else
document.write("The smallest element is "+ first + " and second "+
"Smallest element is "+ second +'\n');
}
// Driver program
let arr = [ 12, 13, 1, 10, 34, 1 ];
let n = arr.length;
print2Smallest(arr, n);
</script>
Output
The smallest element is 1 and second Smallest element is 10
The same approach can be used to find the largest and second-largest elements in an array.
Time Complexity: O(n) Auxiliary Space: O(1)
Approach: A log(N) approach using Priority_queue data structure. You can read about Priority Queue in more detail here.
We use cookies to ensure you have the best browsing experience on our website. By using our site, you
acknowledge that you have read and understood our
Cookie Policy &
Privacy Policy
Please Login to comment...