Write Program to find second smallest element in the array.

09 June 2022

Write Program to find second smallest element in the array.


Given an integer array of size N. Write Program to find second smallest element in the array.

Sample input 1:

4

1 2 3 4

Sample output 1:

2

Sample input 2:

4

5 5 5 5

Sample output 1:

Not found

C Program

#include <stdio.h>

#include <limits.h>

int SecondSmallest(int arr[], int n)

{

    int min, s_min;

    if (n<2)

    {

        printf("Not found");

        return 0;

    }

    min = s_min = INT_MAX;

    for (int i = 0; i < n ; i ++)

    {

        if (arr[i] < min)

        {

            s_min = min;

            min = arr[i];

        }

        else if (arr[i] < s_min && arr[i] != min)

            s_min = arr[i];

    }

    if (s_min == INT_MAX)

        printf("Not found\n");

    else

        printf("%d", s_min);

}

 

int main()

{

      int n;          scanf("%d",&n);

      int arr[n];

      for(int i = 0 ; i<n ; i++)

      {

            scanf("%d",&arr[i]);

      }

      SecondSmallest(arr,n);

      return 0;

}

C++ Program

#include <bits/stdc++.h>

 using namespace std;

 

void SecondSmallest(int arr[], int n)

{

    int min, s_min;

    if (n<2)

    {

        cout<<"Not found";

    }

    else

    {

        min = s_min = INT_MAX;

        for (int i = 0; i < n ; i ++)

        {

            if (arr[i] < min)

            {

                s_min = min;

                min = arr[i];

            }

            else if (arr[i] < s_min && arr[i] != min)

                s_min = arr[i];

        }

        if (s_min == INT_MAX)

            cout<<"Not found";

        else

            cout<<s_min;

    }

}

 

int main()

{

      int n;          scanf("%d",&n);

      int arr[n];

      for(int i = 0 ; i<n ; i++)

      {

            scanf("%d",&arr[i]);

      }

      SecondSmallest(arr,n);

      return 0;

}

JAVA Program

import java.util.*;

import java.lang.*;

import java.io.*;

class Main

{

      static void SecondSmallest(int arr[], int n)

    {

        int min, s_min;

        if (n<2)

        {

            System.out.print("Not found");

        }

        else

        {

            min = s_min = Integer.MAX_VALUE;

            for (int i = 0; i < n ; i ++)

            {

                if (arr[i] < min)

                {

                    s_min = min;

                    min = arr[i];

                }

                else if (arr[i] < s_min && arr[i] != min)

                    s_min = arr[i];

            }

            if (s_min == Integer.MAX_VALUE)

                System.out.print("Not found");

            else

                System.out.print(s_min);

        }

    }

 

      public static void main(String[] args) throws java.lang.Exception

      {

            Scanner sc = new Scanner(System.in);

            int n = sc.nextInt();

            int arr[] = new int[n];

            for(int i = 0 ; i<n ; i++)

            {

                        arr[i] = sc.nextInt();

            }

            SecondSmallest(arr,n);

      }

}

 

PYTHON Program

def SecondSmallest(arr,n):

    if n<2 :

        print("Not found")

    else:

        f_min = 2147483647

        s_min = 2147483647

        for i in range(0,n):

            if arr[i] < f_min:

                s_min = f_min

                f_min = arr[i]

            elif arr[i] < s_min and arr[i] != f_min:

                s_min = arr[i]

        if s_min == 2147483647 :

            print("Not found")

        else:

            print(s_min)

n = int(input())

arr = list(map(int,input().split(' ')))

SecondSmallest(arr,n)


If you are from 2023 batch student, Join our Telegram group for placement preparation and coming placement drive updates : https://t.me/talentbattle2023
Related Articles

Ask Us Anything !