Second Smallest in java

Second Smallest in java

Find the 2nd smallest element in an unsorted array.

Input

Input consists of the first line containing integer N followed by N natural numbers for the array.

Constraints:

1 <= n <= 10000
1 <= value of natural number <= 10000000

Output

In a new line, print the 2nd smallest element in the array.

Example

Input:

6
3 2 1 5 6 4

Output:

2

Input:

6
3 7 1 4 5 6

Output:

3

Solution of Second Smallest in java:–

import java.util.*;
import java.lang.*;
import java.io.*;

public class Main
{
	public static void main (String[] args) throws java.lang.Exception
	{
		//your code here
      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();
        }
      Arrays.sort(arr);
      System.out.print(arr[1]);
	}
}

Add a Comment

Your email address will not be published. Required fields are marked *