
Problem Statement
The problem is to determine whether a given array of integers called arr
can be classified as a "mountain array." A mountain array possesses certain characteristics which create a visual representation resembling a mountain. Specifically:
- The array must have at least three elements to form a peak and slopes.
- It should increase to a highest point (peak) and then strictly decrease. This implies an element
i
such that before thisi
, elements are in ascending order, and afteri
, elements are in descending order. - The peak, which is neither the first nor the last element in the array, signifies a shift from climbing up to climbing down.
Examples
Example 1
Input:
arr = [2,1]
Output:
false
Example 2
Input:
arr = [3,5,5]
Output:
false
Example 3
Input:
arr = [0,3,2,1]
Output:
true
Constraints
1 <= arr.length <= 104
0 <= arr[i] <= 104
Approach and Intuition
When looking to validate whether an array is a mountain array, we are essentially checking for a single peak with increasing values leading up to the peak and decreasing values thereafter. Here’s the method to approach this:
Initial Checks:
- Verify if the array length is less than 3. If it is, return
false
immediately since we can't form a mountain.
- Verify if the array length is less than 3. If it is, return
Find Ascending Order:
- Traverse through the array from the beginning, and continue moving forward until you find the first element which is not larger than the next one. This point (if exists) should potentially be the peak.
- If during this traversal, either the peak ends up being the first element or no peak can be found, return
false
.
Find Descending Order:
- Continue from the peak found in the previous step. The elements should now start to decrease.
- If any element is found that does not adhere to this descending order, return
false
.
Check for True Peak:
- If the array was strictly increasing or strictly decreasing without a true peak, verify the position of the stopping point from the first traversal. If you stopped at the last element, then you don't have a decline phase — return
false
. - Conversely, if the first traversal stopped at the first element, indicating there was no increase, return
false
.
- If the array was strictly increasing or strictly decreasing without a true peak, verify the position of the stopping point from the first traversal. If you stopped at the last element, then you don't have a decline phase — return
Final Validation:
- If all the conditions meet (an increase phase, a peak, followed by a decrease phase), the array is a mountain array. Return
true
.
- If all the conditions meet (an increase phase, a peak, followed by a decrease phase), the array is a mountain array. Return
Examples:
- For
arr = [2,1]
, it fails the initial check of having at least 3 elements. Hence, it returnsfalse
. - In the case of
arr = [3,5,5]
, even though it starts off correctly by increasing, it doesn't form a peak because of the repetition of5
. There is no strict increase followed by a strict decrease. - For
arr = [0,3,2,1]
, we observe a classic mountain array. It strictly increases from0
to3
and then strictly decreases from3
to1
.
By reasoning through these steps using the outlined approach, you can determine if an array represents a mountain structure, fulfilling the conditions necessary for valid mountain-like progression.
Solutions
- Java
class Solution {
public boolean checkMountain(int[] arr) {
int len = arr.length;
int idx = 0;
// Ascend to peak
while (idx + 1 < len && arr[idx] < arr[idx + 1])
idx++;
// A valid peak cannot be at the extremes
if (idx == 0 || idx == len - 1)
return false;
// Descend from peak
while (idx + 1 < len && arr[idx] > arr[idx + 1])
idx++;
return idx == len - 1;
}
}
The given Java solution checks if an array represents a "mountain" sequence, which means the elements of the array first increase to a peak and then decrease from that peak. The method checkMountain
in the Solution
class implements this check through the following steps:
- Initialize variables
len
to store the length of the array andidx
to track the current index. - Increment
idx
while consecutive elements are ascending, i.e., the current element is less than the next element. - Verify that the peak is not at the first or last index of the array since a valid mountain must peak somewhere in the middle.
- Continue incrementing
idx
while elements are descending, ensuring the current element is greater than the next. - Confirm that
idx
has reached the last element of the array, indicating a proper rise and fall akin to a mountain.
The method returns true
if the given array fits the criteria of a mountain array; otherwise, it returns false
. This approach ensures efficient checking with a single pass through the array, utilizing a while loop to traverse elements according to their ascent and descent.
No comments yet.