Skip to main content

Posts

Showing posts from March, 2021

Valid Mountain Array ( Google )

  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 : - Java 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; } } Python Solution def validMountain(arr): i = 0