Hope the following Java solution with some explanatory comments will be helpful:
class Solution {
public boolean validMountainArray(int[] arr) {
boolean left = false, right = false; // Two boolean vars to track the left and right half of the array
int i = 0; // A var to traverse the array
int L = arr.length; // A var to store the length of the array
// The walking-up part
while(i < L-1 && arr[i] <= arr[i+1]){ // First traverse the array till the ith element is less than or equal the (i+1)th element
if(arr[i] == arr[i+1]) // If the ith and the (i+1)th elements are equal
return false; // Then, no mountain can be formed. So, return 'false'
left = true; // Else, set the value of 'left' to 'true'
i++; // And keep moving to the next index
}
// The walking-down part
while(i < L-1){ // Then, traverse the rest of the array till the 2nd last index
if(arr[i] <= arr[i+1]) // If any element is found that is less than or equal to it's next element
return false; // Then, no mountain can be formed. So, return 'false'
right = true; // Else, set the value of 'right' to 'true'
i++; // And, keep moving to the next index
}
return left && right; // Finally, return the logical and of 'left' and 'right' as both have to be 'true'
}
}