Check if Array is a Mountain
Given an array of integers, determine if it represents a 'mountain'. A 'mountain' array is defined as one that:
- Has at least 3 elements.
- Has a peak element where elements increase up to the peak, then decrease.
You must check if the input array follows this pattern and return true if it is a mountain array and false otherwise.
[ 2, 1, 4, 7, 3, 2 ]
Explanation. The array increases to element 7, then decreases, forming a mountain shape.
[ 5, 5, 5, 5 ]
Explanation. The array doesn't increase and decrease, it remains constant, hence it's not a mountain.
[ 0, 3, 2, 1 ]
Explanation. The array strictly increases to 3 and then decreases, forming a valid mountain.
[ 1, 2, 3, 4, 5 ]
Explanation. The array only increases and does not decrease, hence it is not a mountain.
[ 10, 6, 5, 4, 3, 2, 1 ]
Explanation. The array only decreases and never increases to a peak, hence it is not a mountain.
Follow-up: How would your solution change if you needed to find the index of the peak element in the mountain array?
The input array will contain at least 3 integers, and all elements will be non-negative integers.
- Views
- 2