Peak Index of Mountain Array Problem in JavaIt 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 StatementAn array is said to be a mountain array if it satisfies the following conditions:
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 ProblemThe problem can be solved using the following two approaches:
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:
ExampleSuppose 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
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 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 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. PeakIndex.java Output: The peak index of the mountain array is: 4 ComplexityThe 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. |
We provides tutorials and interview questions of all technology like java tutorial, android, java frameworks
G-13, 2nd Floor, Sec-3, Noida, UP, 201301, India