Last active
September 18, 2020 03:11
-
-
Save MukulLatiyan/b98fe71d84e1d99a256231a32a94a4b2 to your computer and use it in GitHub Desktop.
Second Largest(GeeksforGeeks)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//Java Code for printing the second largest Element present in the array(Geeksforgeeks-array School problem) | |
import java.util.*; | |
import java.lang.*; | |
import java.io.*; | |
class GFG { | |
public static void SecondMinimum(int[] a,int n) | |
{ | |
int first,second; | |
first=second=Integer.MIN_VALUE; | |
for(int i=0;i<n;i++){ | |
if(a[i]>first) | |
{ | |
second=first; | |
first=a[i]; | |
}else if(a[i]>second && a[i]!=first) | |
{ | |
second=a[i]; | |
} | |
} | |
if (second == Integer.MIN_VALUE) | |
System.out.print("-1"); | |
else | |
System.out.println(second); | |
} | |
public static void main (String[] args) { | |
Scanner in=new Scanner(System.in); | |
int t,n; | |
t=in.nextInt(); | |
while(t-->0) | |
{ | |
n=in.nextInt(); | |
int a[]=new int[n]; | |
for(int i=0;i<n;i++) | |
{ | |
a[i]=in.nextInt(); | |
} | |
SecondMinimum(a,n); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment