Second largest element
Problem Statement:
Given an array of integers, find the second largest element in the array.
Steps to Solve the Problem:
Initialization:
- Initialize two variables:
firstLargestandsecondLargestto the smallest possible integer value (Integer.MIN_VALUE).
- Initialize two variables:
Iterate Through the Array:
For each element in the array, compare it with
firstLargestandsecondLargestand update these variables accordingly:If the current element is greater than
firstLargest, updatesecondLargesttofirstLargestand then updatefirstLargestto the current element.If the current element is greater than
secondLargestbut not equal tofirstLargest, updatesecondLargestto the current element.
Return the Result:
- After the loop, check if
secondLargestis stillInteger.MIN_VALUE. If so, returnnull, meaning there is no second largest element. Otherwise, returnsecondLargest.
- After the loop, check if
Dry Run:
Let's perform a dry run with the array [12, 35, 1, 10, 34, 1].
Initial State:
firstLargest = Integer.MIN_VALUE(-2147483648)secondLargest = Integer.MIN_VALUE(-2147483648)
Array Iteration:
First Element: 12
Compare 12 with
firstLargest(-2147483648):- 12 >
firstLargest, so updatesecondLargesttofirstLargest(-2147483648) andfirstLargestto 12.
- 12 >
State:
firstLargest = 12secondLargest = -2147483648
Second Element: 35
Compare 35 with
firstLargest(12):- 35 >
firstLargest, so updatesecondLargesttofirstLargest(12) andfirstLargestto 35.
- 35 >
State:
firstLargest = 35secondLargest = 12
Third Element: 1
Compare 1 with
firstLargest(35):1 is not greater than
firstLargest, so check if it's greater thansecondLargest(12):1 is not greater than
secondLargest, so no changes.
State:
firstLargest = 35secondLargest = 12
Fourth Element: 10
Compare 10 with
firstLargest(35):10 is not greater than
firstLargest, so check if it's greater thansecondLargest(12):10 is not greater than
secondLargest, so no changes.
State:
firstLargest = 35secondLargest = 12
Fifth Element: 34
Compare 34 with
firstLargest(35):34 is not greater than
firstLargest, so check if it's greater thansecondLargest(12):34 >
secondLargest, so updatesecondLargestto 34.
State:
firstLargest = 35secondLargest = 34
Sixth Element: 1
Compare 1 with
firstLargest(35):1 is not greater than
firstLargest, so check if it's greater thansecondLargest(34):1 is not greater than
secondLargest, so no changes.
Final State:
firstLargest = 35secondLargest = 34
Final Result:
- The second largest element in the array is 34.
Java Code Recap:
javaCopy codepublic class SecondLargestElement {
public static Integer findSecondLargest(int[] arr) {
if (arr.length < 2) {
return null; // Not enough elements
}
int firstLargest = Integer.MIN_VALUE;
int secondLargest = Integer.MIN_VALUE;
for (int i = 0; i < arr.length; i++) {
if (arr[i] > firstLargest) {
secondLargest = firstLargest;
firstLargest = arr[i];
} else if (arr[i] > secondLargest && arr[i] != firstLargest) {
secondLargest = arr[i];
}
}
return secondLargest == Integer.MIN_VALUE ? null : secondLargest;
}
public static void main(String[] args) {
int[] arr = {12, 35, 1, 10, 34, 1};
Integer result = findSecondLargest(arr);
if (result != null) {
System.out.println("The second largest element is: " + result);
} else {
System.out.println("There is no second largest element.");
}
}
}
This dry run shows how the algorithm processes each element in the array and correctly identifies the second largest element.