-
Notifications
You must be signed in to change notification settings - Fork 11
/
SecondLargeAndSmall.java
33 lines (31 loc) · 1.03 KB
/
SecondLargeAndSmall.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
package Problems_on_Arrays;
import java.util.Arrays;
import java.util.Scanner;
public class SecondLargeAndSmall {
public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
System.out.println("enter the number of elements:");
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.println("Second Largest : "+arr[arr.length-2]);
System.out.println("Second Smallest : "+arr[1]);
//without defined method Sort
for (int i=0;i<n;i++){
for (int j=0;j<n;j++){
if (arr[i]<arr[j]){
int temp=arr[i];
arr[i]=arr[j];
arr[j]=temp;
}
}
}
System.out.println("without defined method Sort");
System.out.println("Second Largest : "+arr[arr.length-2]);
System.out.println("Second Smallest : "+arr[1]);
sc.close();
}
}