
Problem Statement
In the problem, we are provided with an array arr
, and our task is to determine the length of the longest subarray that qualifies as a "mountain". A subarray is defined as a mountain if it has at least three elements, and there is a peak element such that elements before the peak strictly increase and elements after the peak strictly decrease. Specifically, the array must have this behavior in sequential order: first increasing up to a single peak element, and then decreasing continuously from this peak. If no such mountain exists, the array should return a length of 0.
Examples
Example 1
Input:
arr = [2,1,4,7,3,2,5]
Output:
5
Explanation:
The largest mountain is [1,4,7,3,2] which has length 5.
Example 2
Input:
arr = [2,2,2]
Output:
0
Explanation:
There is no mountain.
Constraints
1 <= arr.length <= 104
0 <= arr[i] <= 104
Approach and Intuition
To tackle the problem of finding the longest mountain in the array, we can follow this strategy:
- Initialize a variable to keep track of the maximum length of the mountain found (
maxLength
). - Traverse through the array using an index
i
that will attempt to identify potential peaks. - For each potential peak at index
i
, we check if it's a valid peak:- Make sure
i
is not the first or the last element in the array (since the peak needs to have elements on both sides). - Confirm that
arr[i-1] < arr[i] > arr[i+1]
to validate that it's a peak.
- Make sure
- Once a peak is identified, expand from the peak in both directions:
- Move leftwards from
i - 1
and continue until the elements stop increasing. - Move rightwards from
i + 1
and continue until the elements stop decreasing.
- Move leftwards from
- Calculate the length of the mountain for the given peak by considering the number of elements counted while expanding in both directions.
- Compare and update the
maxLength
if the current mountain's length is greater than previously recorded lengths. - After completing the scan of the array, return
maxLength
as the length of the longest mountain.
Considerations based on constraints:
- If the array has less than 3 elements, it's impossible to form a mountain, so we return 0 immediately.
- Since each peak check involves expanding to both the left and right, the worst case computational complexity is O(n), where
n
is the number of elements in the array. This is efficient given the constraints provided.
Solutions
- Java
class Solution {
public int maxMountainLength(int[] arr) {
int arrayLength = arr.length;
int maxLen = 0, startPoint = 0;
while (startPoint < arrayLength) {
int peak = startPoint;
if (peak + 1 < arrayLength && arr[peak] < arr[peak + 1]) {
while (peak + 1 < arrayLength && arr[peak] < arr[peak + 1]) peak++;
if (peak + 1 < arrayLength && arr[peak] > arr[peak + 1]) {
while (peak + 1 < arrayLength && arr[peak] > arr[peak + 1]) peak++;
maxLen = Math.max(maxLen, peak - startPoint + 1);
}
}
startPoint = Math.max(peak, startPoint + 1);
}
return maxLen;
}
}
This Java solution is designed to find the longest mountain in an array. A mountain in an array consists of elements that first strictly increase until a peak, and then strictly decrease. The provided Java method, maxMountainLength
, takes an integer array arr
and returns the length of the longest mountain.
Follow these steps to understand the implementation:
- Initialize
arrayLength
to the length of the input arrayarr
. - Set
maxLen
to zero, which stores the maximum length of the mountain found so far, andstartPoint
to zero, which is used to scan through the array. - Use a while loop to traverse the array from
startPoint
until the end of the array. - Use an inner while loop to find the peak of the mountain by comparing the current element with the next. If the current element is less than the next, increment
peak
. - Once a peak is found, check if there's a descending sequence immediately following the peak by another condition checking if the current element is greater than the next.
- Use another while loop to traverse down the mountain once a descending sequence starts.
- Calculate the length of the mountain by subtracting the
startPoint
frompeak
and adding one because the array indices start from zero. UpdatemaxLen
if this mountain is longer than any previously found ones. - Set
startPoint
to the maximum ofpeak
andstartPoint + 1
to ensure the loop processes subsequent potential mountains and doesn't recheck the same mountain.
This approach ensures that each element contributing to a potential mountain's upward or downward slope is visited exactly once, making the algorithm efficient with a time complexity of O(N), where N is the number of elements in the array. The algorithm effectively identifies the longest sequence that represents a mountain as per the given criteria.
No comments yet.