-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsecond_greatest.java
More file actions
32 lines (32 loc) · 922 Bytes
/
second_greatest.java
File metadata and controls
32 lines (32 loc) · 922 Bytes
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
import java.util.Scanner;
public class second_greatest {
public static void main(String args[]) {
Scanner sc=new Scanner(System.in);
System.out.println("Enter number of elements in the array");
int n=sc.nextInt();
int a[]=new int[n];
int b[]=new int[n];
for(int i=0;i<n;i++){
System.out.println("Enter value at index "+i);
a[i]=sc.nextInt();
b[i]=i;
}
for(int i=0;i<n;i++){
for(int j=0;j<n-i-1;j++){
if(a[j]>a[j+1]){
int temp=a[j];
a[j]=a[j+1];
a[j+1]=temp;
int temp=b[j];
b[j]=b[j+1];
b[j+1]=temp;
}
}
}
for(int i=0;i<n;i++){
if(b[i]==1){
System.out.println(a[i]);
}
}
}
}