Find the 2nd smallest element in the array

Problem statement:- Program to Find the 2nd smallest element in the array.

Data requirement:-

   Input Data:- arr,size

  Output Data:-sec_min

  Additional Data:- i, j, min

Program in C

Here is the source code of the C Program to Find the 2nd smallest element in the array.

Code:

#include<stdio.h>
#include<limits.h>
int main()
{
    printf("Enter the size of the array:");
    int size;
    scanf("%d",&size);
    int arr[size],i=0,min=INT_MAX,sec_min=INT_MAX,j=0;
    printf("Enter the Element of the array:\n");
     while(j<size)
    {
        scanf("%d",&arr[j]);
        j++;
    }
    while(i<size)
    {
        if(arr[i]<=min)
        {
            sec_min=min;
            min=arr[i];

        }
        else if(arr[i]<=sec_min)
            sec_min=arr[i];

        i++;
    }
    printf("The 2nd smallest element of array: %d",sec_min);
}

Input/Output:
Enter the size of the array:6
Enter the Element of the array:
5
9
2
4
6
7
The smallest element of array: 4

Program in C++

Here is the source code of the C++ Program to find the second smallest number in an array.

Code:

#include<iostream>
#include<climits>
using namespace std;
int main()
{
    cout<<"Enter the size of the array:";
    int size;
    cin>>size;
    int arr[size],i=0,min=INT_MAX,sec_min=INT_MAX,j=0;
     cout<<"Enter the Element of the array:\n";
     while(j<size)
    {
        cin>>arr[j];
        j++;
    }
    while(i<size)
    {
        if(arr[i]<=min)
        {
            sec_min=min;
            min=arr[i];

        }
        else if(arr[i]<=sec_min)
            sec_min=arr[i];

        i++;
    }
    cout<<"The 2nd smallest element of array: "<<sec_min;
}

Input/Output:
Enter the size of the array:4
Enter the Element of the array:
5
2
9
4
The 2nd smallest element of array: 4

Program in Java

Here is the source code of the Java Program to Find the 2nd smallest element in the array.

Code:

import java.util.Scanner;
public class p9 {

public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
System.out.println("Enter the size of the array:");
    int size;
    size=sc.nextInt();
    int arr[ ]=new int[size];
int i=0,min=Integer.MAX_VALUE,sec_min=Integer.MAX_VALUE,j=0;
System.out.println("Enter the Element of the array:");
     while(j<size)
    {
        arr[j]=sc.nextInt();
        j++;
    }
    while(i<size)
    {
    if(arr[i]<=min)
         {
             sec_min=min;
             min=arr[i];
         }
         else if(arr[i]<=sec_min)
             sec_min=arr[i];
         i++;
    }
    System.out.println("The 2nd smallest element of array: "+sec_min);
    sc.close();
}
}

Input/Output:
Enter the size of the array:
3
Enter the Element of the array:
9
5
2
The 2nd smallest element of array: 5

Program in Python

Here is the source code of the Python Program to find the second smallest number in an array.

Code:

import sys
arr=[]
size = int(input("Enter the size of the array: "))
print("Enter the Element of the array:")
for i in range(0,size):
    num = int(input())
    arr.append(num)
min=sys.maxsize
sec_min=sys.maxsize
for j in range(0,size):
    if (arr[j] <= min):
        sec_min=min
        min = arr[j]
    elif(arr[i] <= sec_min):
        sec_min = arr[j]
print("The 2nd smallest element of array: ",sec_min)

Input/Output:
Enter the size of the array: 4
Enter the Element of the array:
89
5
24
77
The 2nd smallest element of an array:  24

Post a Comment

2 Comments

  1. This comment has been removed by the author.

    ReplyDelete
  2. I am 100 % sure this logic failed under (111 , 121 , 111 , 111 , 112) this test cases..
    So we need to change this logic i.e,
    else if(arr[i] < sec_min & arr[i] > min )
    {
    sec_min = arr[i] ;
    }

    ReplyDelete

Please do not Enter any spam link in the comment box