-
Notifications
You must be signed in to change notification settings - Fork 0
/
Find Second Largest in array
73 lines (62 loc) · 1.89 KB
/
Find Second Largest in array
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
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
import java.lang.*;
import java.util.*;
public class Main {
public static void findLargestAndSecondLargestVal(int[] a){
int aLen = a.length;
if(validateError(a)){
System.out.print("NA");
}
else{
int greaterVal = a[0];
for(int j=0; j<aLen; j++){
if(greaterVal < a[j]){
greaterVal = a[j];
}
}
//System.out.println("Largest val: "+greaterVal);
getsecond(a, greaterVal);
}
}
public static boolean validateError(int[] ar){
int arLen = ar.length;
//System.out.println("Len: "+arLen);
if(arLen <= 0){
return true;
}
int count = 1;
for(int i=0; i<arLen-1; i++){
//System.out.println("check: "+ar[i]+ " - "+ ar[i+1]);
if(ar[i] == ar[i+1]){
count++;
}
}
// System.out.println("count==arLen: "+count+" "+arLen);
if(count == arLen){
return true;
}
return false;
}
public static void getsecond(int[] a2, int b){
int a2Len = a2.length;
int secondLargest = a2[0];
for(int k=1; k<a2Len; k++){
if(a2[k] != b && secondLargest < a2[k]){
secondLargest = a2[k];
}
}
System.out.println(secondLargest);
}
public static void main(String[] args) {
// YOUR CODE GOES HERE
// Please take input and print output to standard input/output (stdin/stdout)
// DO NOT USE ARGUMENTS FOR INPUTS
// E.g. 'Scanner' for input & 'System.out' for output
Scanner sc = new Scanner(System.in);
int arrLen = sc.nextInt();
int[] arr = new int[arrLen];
for(int i=0; i<arrLen; i++){
arr[i] = sc.nextInt();
}
findLargestAndSecondLargestVal(arr);
}
}