## Peak Index of Mountain Array Problem in JavaIt is a very interesting problem frequently asked in interviews of top IT companies like ## Problem StatementAn array is said to be a mountain array if it satisfies the following conditions: - The length of the given array is should be greater or equal to 3 i.e.
**LENGTH >=3**. - There must be only
**one peak**in the array or the largest element in the array. - The array must follows the condition:
**ARRAY[0] < ARRAY[1] < ARRAY[i-1] < ARRAY[ i] > ARRAY[ i+1 ] > ARRAY[..] > ARRAY[length-1]**
The task is to find the Suppose we have given the input [60, 20, 90, 110, 10]. The output will be 3. Because the largest element in the array is 110 whose index is 3. ## Solution to the ProblemThe problem can be solved using the following two approaches: - Using Binary Search
- Using Linear Search
Let's discuss the above approaches one by one. ## Using Binary SearchThe elements in the given array must be in either ascending or descending sorted order. There should not be duplicate elements. Using the binary search algorithm, we can find out the required element. In this approach, at every step reduce the search by half. The problem can be solved by using the following steps: - Find the middle element of the array
**arr**. - If the array is arranged in descending order (
**arr[mid]>arr[mid+1]**), it means the peak element will be on the**left**side of the middle element. Therefore, reduce the search between the**left half**and the**middle** - If the array is arranged in ascending order (
**arr[mid+1]<arr[mid]**), it means the peak element will be on the**right**side of the middle element. Therefore, reduce the search between the**right**element and the**middle+1** - Repeat step 2 and 3, recursively until the condition
**left<right**become false. - When the condition
**left >= right**becomes true, the peak element will be at the left index.
## ExampleSuppose the input array is The length of the array will be array length-1, i.e. 7-1 = Therefore, Let's find the mid of the array. Mid=low+(high-low)/2 Mid=0+(6-0)/2 = Now check if (array[mid]>=array[mid+1]) or not Therefore, 9 >= 8, the condition is Now, Again, find the mid of the array Mid=low+(high-low)/2 Mid = 0 + (3 - 0) / 2 = Now check if (array[mid]>=array[mid+1]) Therefore, 2 >= 7, the condition is Now, Again, find the mid of the array Mid=low+(high-low)/2 Mid = 2 + (3 - 2) / 2 = Now check if (array[mid]>=array[mid+1]) Therefore, 7 >= 9, the condition is Now, Again, find the mid of the array Mid=low+(high-low)/2 Mid = 3 + (3 - 3) / 2 = Now check if (array[mid]>=array[mid+1]) Therefore, 2 >= 7, the condition is Repeat the above process. At last we get low=3, High=3, Mid=3; And here when it comes in loop while (low < high), means 3< 3 and the condition becomes false. Exit from the loop and return low i.e. 3. Hence, the peak index become 3. ## Algorithm- Set low = 0.
- Set high to the length of array -1.
- Declare a variable mid.
- Set mid = low + (high - low) / 2.
- While low < high:
- If array[ mid ] > = array [ mid + 1].
- then high = mid.
- Else
- then low = mid + 1.
- Return low.
- If array[ mid ] > = array [ mid + 1].
Let's implement the above algorithm in the Java program. ## Java Program to Find the Peak Index of Mountain ArrayIn this approach, we are going to use a
The peak index of the mountain array is: 3 ## Using Linear SearchIn this approach, we iterate through the given input array A. At each iteration, if the current element is greater than the previous element and the current element is smaller than the next element then the current element is the peak element. Let's implement the above approach in a Java program.
The peak index of the mountain array is: 4 ## ComplexityThe time complexity of the above solution is |

For Videos Join Our Youtube Channel: Join Now

- Send your Feedback to [email protected]