Javatpoint Logo
Javatpoint Logo

Peak Index of Mountain Array Problem in Java

It is a very interesting problem frequently asked in interviews of top IT companies like Google, Amazon, TCS, Accenture, Adobe, Apple, Infosys, etc. By solving the problem, one wants to check the logical ability, critical thinking, and problem-solving skill of the interviewee. So, in this section, we are going to find the peak index of mountain array in Java with different approaches and logic. Also, we will create Java programs for the same.

Problem Statement

An array is said to be a mountain array if it satisfies the following conditions:

  1. The length of the given array is should be greater or equal to 3 i.e. LENGTH >=3.
  2. There must be only one peak in the array or the largest element in the array.
  3. 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 peak index of the mountain array.

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 Problem

The 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 Search

The 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:

  1. Find the middle element of the array arr.
  2. 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
  3. 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
  4. Repeat step 2 and 3, recursively until the condition left<right become false.
  5. When the condition left >= right becomes true, the peak element will be at the left index.

Example

Suppose the input array is {4, 2, 7, 9, 8, 3, 1}.

The length of the array will be array length-1, i.e. 7-1 = 6.

Therefore, High=6 and Low=0

Let's find the mid of the array.

Mid=low+(high-low)/2

Mid=0+(6-0)/2 = 3

Now check if (array[mid]>=array[mid+1]) or not

Therefore, 9 >= 8, the condition is true. Set high=Mid

Now, low=0, Mid=3, High=3

Again, find the mid of the array Mid=low+(high-low)/2

Mid = 0 + (3 - 0) / 2 = 1

Now check if (array[mid]>=array[mid+1])

Therefore, 2 >= 7, the condition is false. Set low=Mid+1

Now, low=1+1=2, Mid=1, High=3

Again, find the mid of the array Mid=low+(high-low)/2

Mid = 2 + (3 - 2) / 2 = 2

Now check if (array[mid]>=array[mid+1])

Therefore, 7 >= 9, the condition is false. Set low=Mid+1

Now, low=2+1=3, Mid=2, High=3

Again, find the mid of the array Mid=low+(high-low)/2

Mid = 3 + (3 - 3) / 2 = 1

Now check if (array[mid]>=array[mid+1])

Therefore, 2 >= 7, the condition is false. Set low=Mid+1

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

  1. Set low = 0.
  2. Set high to the length of array -1.
  3. Declare a variable mid.
  4. Set mid = low + (high - low) / 2.
  5. While low < high:
    1. If array[ mid ] > = array [ mid + 1].
      1. then high = mid.
    2. Else
      1. then low = mid + 1.
    3. Return low.

Let's implement the above algorithm in the Java program.

Java Program to Find the Peak Index of Mountain Array

In this approach, we are going to use a binary search. We have defined a function named findPeakIndex() in which we have passed the input array and the length of an array. We have declared a variable named mid and initialized it to 0 and a variable named high which is equal to high-1. Inside a while loop, we have defined a condition low < high. The condition will execute until it returns false. Entering in a loop we set mid=low+(high-low) / 2.

GetPeakIndex.java

Output:

The peak index of the mountain array is: 3

Using Linear Search

In 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.

PeakIndex.java

Output:

The peak index of the mountain array is: 4

Complexity

The time complexity of the above solution is O(log n) where n is the length of the array. The space complexity is O(1) because we have not used any extra space for calculation.







Youtube For Videos Join Our Youtube Channel: Join Now

Feedback


Help Others, Please Share

facebook twitter pinterest

Learn Latest Tutorials


Preparation


Trending Technologies


B.Tech / MCA