The interviews and hiring challenges have started evolving recently. The companies no longer ask the same old question, but they follow certain trends in their questions by mixing them with real-life objects and puzzles.
So, in today's tech blog we are going to solve a renowned question called Valid Mountain Array. This question has been asked in Google and Amazon and is still a favorite question of hiring recruiters. So, let's go!
Valid Mountain Array Problem Statement
Here is the problem statement for the Valid Mountain Array: "You are given an array of integers. You need to check whether the given array is a Mountain Array or not."
What is a Mountain Array?
We will come to the solution eventually, but what if you don't know what a Mountain Array is? Well, then you won't be able to present anything to your interviewer! To save you from such embarrassment, read the whole blog carefully!
A Mountain Array is an array of elements that gradually increase in their first half and later, start decreasing. Think of it as a Mountain! You start climbing its top which is high, you reach the peak and later, descend it in which the height of the mountain keeps on decreasing.
Look at the given image:
Input: [5, 2, 1, 4]
Explanation: This is not a Mountain Array. Why? Because here the first element is 5 and after that 2 comes, so the array never actually increased. It keeps on decreasing till its 3rd element and then, again increases. Carefully see the following image:
Input: [1, 2, 6, 5, 3]
Explanation: This is a valid Mountain Array. Here, firstly the elements are increased from 1 to 2 to 6. Now, 6 is the highest point of this array, from which the array is descending for the rest of its elements. Observe the given illustration:
NOTE: The largest element of a Mountain Array is called the Peak Element.
How to solve Valid Mountain Array?
Now that you have grasped the concept of Mountain Array, let's solve this question using 2 techniques.
Method 01) Traversing from Left to Right
It is the most basic idea to solve this question. You just start moving from the leftmost integer or we can say, starting off the array. As you traverse, you will keep checking whether the next element is greater than the previous or not.
Now, when you reach a point where you encounter the peak element. You now check whether the next element is less than the previous element or not.
If the above conditions are satisfied, this is a Valid Mountain Array. Else, return False.
Method 02) Traversing from Opposite Ends
Here, we initialize 2 variables to move from the opposite ends of the array. Now, using a loop we reach the peak element of the array. Similarly, using the loop we reach the peak element from the right side of the array.
Now, in a Mountain Array, you can have only one Peak Element. So, if the peak encountered by the left variable and the right variable is the same, it is a Mountain Array. Otherwise, it is not!
Code Implementation in C++
Here is the C++ code to solve the Valid Mountain Array problem:
Code Implementation in Python
Here is the Python code:
Code Implementation in Java
Here is the Java code:
|Traversing from Left to Right
|Traversing from Opposite Ends
This question has come in interviews with tech giants like Google and Amazon. After reading it, we are sure you will be able to solve the Valid Mountain Array problem in the future. So, keep practicing and for more such tech blogs, FavTutor is always there to support you.