You are given a sorted and rotated array as below:
For example:
int[] arr1={7,5,6,1,4,2}; Second largest element in the array : 6
You can sort the array and then return the second last element in the array but it will be done in o(nlogn) time,
When you run the above program, you will get the below output::
Algorithm:
- Initialize highest and secondHighest with minimum possible value.
- Iterate over array.
- If current element is greater than highest
- Assign secondHighest = highest
- Assign highest = currentElement
- Else if current element is greater than secondHighest
- Assign secondHighest =current element
Java Program to find second largest number in array:
Create the main java class named
FindSecondLargestMain.java
:
public class FindSecondLargestMain { public static void main(String args[]) { int[] arr1={7,5,6,1,4,2}; int secondHighest=findSecondLargestNumberInTheArray(arr1); System.out.println("Second largest element in the array : "+ secondHighest); } public static int findSecondLargestNumberInTheArray(int array[]) { // Initialize these to the smallest value possible int highest = Integer.MIN_VALUE; int secondHighest = Integer.MIN_VALUE; // Loop over the array for (int i = 0; i < array.length; i++) { // If current element is greater than highest if (array[i] > highest) { // assign second highest element to highest element secondHighest = highest; // highest element to current element highest = array[i]; } else if (array[i] > secondHighest && array[i]!=highest) // Just replace the second highest secondHighest = array[i]; } // After exiting the loop, secondHighest now represents the second // largest value in the array return secondHighest; } }
Second largest element in the array : 6
Don't miss the next article!
Be the first to be notified when a new article or Kubernetes experiment is published.
Share This