Problem :-
Given an array A of integers , return true if and only if it is a valid mountain Array , otherwise return false .
☝ Sample Input :- [ 0 , 2 , 3 , 4 , 5 , 2 , 1 ]
Sample Output :- true
✌ Sample Input :- [ 0 , 2 , 3 , 4 , 5 ]
Sample Output :- false
Explanation :-
Solution : -
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 | public class Mountain { public static void main(String[] args) { int[] arr = { 0, 2, 3, 4, 5, 2, 1 } ; System.out.println(validMountain(arr)) ; } public static boolean validMountain(int[] arr) { int i = 0, n = arr.length-1; while(i<n && arr[i]<arr[i+1]) i++; if(i==0 || i==n) return false; while(i<n && arr[i]>arr[i+1]) i++; return i==n; } } |
def validMountain(arr): i = 0 n = len(arr)-1 while i<n and arr[i]<arr[i+1]: i+=1 if i==0 or i==n: return False while i<n and arr[i]>arr[i+1]: i+=1 return i==n
arr = [ 0, 2, 3, 4, 5 ]
print(validMountain(arr))
Thank you for reading this post.💓
If there is any problem with the solution. Write your query below 👇.