package org.wesome.dsalgo;
public class SecondLargest {
static int findSecondLargest(int[] arr) {
if (arr.length < 2) {
System.out.println(" array size is less then required");
return -1;
}
int size = arr.length, firstLargest = Integer.MIN_VALUE, secondLargest = Integer.MIN_VALUE;
/* Find the firstLargest element */
for (int indx = 0; indx < size; indx++) {
firstLargest = Math.max(firstLargest, arr[indx]);
}
/* Find the secondLargest element */
for (int indx = 0; indx < size; indx++) {
if (arr[indx] != firstLargest)
secondLargest = Math.max(secondLargest, arr[indx]);
}
if (Integer.MIN_VALUE == secondLargest) {
System.out.println("The first Largest is " + firstLargest + " There is no second Largest element");
return -1;
} else {
System.out.println("The first Largest is " + firstLargest + " and the second Largest is " + secondLargest);
return secondLargest;
}
}
}