Find the peak index of a given array
Last Updated :
03 May, 2021
Given an array arr[] consisting of N(> 2) integers, the task is to find the peak index of the array. If the array doesn’t contain any peak index, then print -1.
The peak index, say idx, of the given array arr[] consisting of N integers is defined as:
- 0 < idx < N – 1
- arr[0] < arr[1] < arr[2] < …. < arr[idx] < …. < arr[N – 2] < arr[N – 1]
Examples:
Input: arr[] = {0, 1, 0}
Output: 1
Explanation: In the given array at index 1, arr[0] < arr[1] and arr[1] > arr[2]. Since index 1 satisfies the given condition, therefore 1 is a peak index of the array.
Input: arr[] = {3, 5, 5, 4, 3, 2, 1}
Output: -1
Naive Approach: The simplest approach is to traverse the array and check for each index, say idx, over the range [1, N – 2] whether the index idx can be the peak index of the array or not. This can be done by checking if all elements to the left and right of this index idx must be in strictly increasing and strictly decreasing order. After checking for all the indices, if there exists any such index, then print that index. Otherwise, print “-1”.
Time Complexity: O(N2)
Auxiliary Space: O(1)
Efficient Approach: The above approach can also be optimized based on the fact that the peak index will exist only if the array contains a strictly increasing prefix followed by a strictly decreasing suffix.
Follow the steps below to solve the problem:
- Initialize two variables, say ans, to store the index of the peak element of the array.
- Traverse the given array over the range of indices [1, N – 2], using a variable, say i. If the value of arr[i] is greater than or equal to arr[i + 1], then update ans as i and break out of the loop.
- If the value of ans is 0 or (N – 1), then print “-1”, as there exists no such peak index for the given array.
- Now, traverse the given array over the range [ans, N – 2] using the variable i and if the value of arr[i] is less than or equal to arr[i + 1], then break out of the loop.
- After completing the above steps, if the value of i is (N – 1) then print the value of ans as the resultant peak index. Otherwise, print “-1” as there exists no such peak index for the given array.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
int peakIndex( int arr[], int N)
{
if (N < 3)
return -1;
int i = 0;
while (i + 1 < N)
{
if (arr[i + 1] < arr[i] ||
arr[i] == arr[i + 1])
break ;
i++;
}
if (i == 0 || i == N - 1)
return -1;
int ans = i;
while (i < N - 1)
{
if (arr[i] < arr[i + 1] ||
arr[i] == arr[i + 1])
break ;
i++;
}
if (i == N - 1)
return ans;
return -1;
}
int main()
{
int arr[] = { 0, 1, 0 };
int N = sizeof (arr) / sizeof (arr[0]);
cout << peakIndex(arr, N) << "\n" ;
return 0;
}
|
Java
import java.io.*;
import java.util.*;
class GFG {
public static int peakIndex( int [] arr)
{
int N = arr.length;
if (arr.length < 3 )
return - 1 ;
int i = 0 ;
while (i + 1 < N) {
if (arr[i + 1 ] < arr[i]
|| arr[i] == arr[i + 1 ])
break ;
i++;
}
if (i == 0 || i == N - 1 )
return - 1 ;
int ans = i;
while (i < N - 1 ) {
if (arr[i] < arr[i + 1 ]
|| arr[i] == arr[i + 1 ])
break ;
i++;
}
if (i == N - 1 )
return ans;
return - 1 ;
}
public static void main(String[] args)
{
int [] arr = { 0 , 1 , 0 };
System.out.println(peakIndex(arr));
}
}
|
Python3
def peakIndex(arr):
N = len (arr)
if ( len (arr) < 3 ):
return - 1
i = 0
while (i + 1 < N):
if (arr[i + 1 ] < arr[i] or
arr[i] = = arr[i + 1 ]):
break
i + = 1
if (i = = 0 or i = = N - 1 ):
return - 1
ans = i
while (i < N - 1 ):
if (arr[i] < arr[i + 1 ] or
arr[i] = = arr[i + 1 ]):
break
i + = 1
if (i = = N - 1 ):
return ans
return - 1
if __name__ = = '__main__' :
arr = [ 0 , 1 , 0 ]
print (peakIndex(arr))
|
C#
using System;
using System.Collections.Generic;
class GFG{
public static int peakIndex( int [] arr)
{
int N = arr.Length;
if (arr.Length < 3)
return -1;
int i = 0;
while (i + 1 < N)
{
if (arr[i + 1] < arr[i] ||
arr[i] == arr[i + 1])
break ;
i++;
}
if (i == 0 || i == N - 1)
return -1;
int ans = i;
while (i < N - 1)
{
if (arr[i] < arr[i + 1] ||
arr[i] == arr[i + 1])
break ;
i++;
}
if (i == N - 1)
return ans;
return -1;
}
static public void Main()
{
int [] arr = { 0, 1, 0 };
Console.WriteLine(peakIndex(arr));
}
}
|
Javascript
<script>
function peakIndex(arr)
{
var N = arr.length;
if (arr.length < 3)
return -1;
var i = 0;
while (i + 1 < N) {
if (arr[i + 1] < arr[i] || arr[i] == arr[i + 1])
break ;
i++;
}
if (i == 0 || i == N - 1)
return -1;
var ans = i;
while (i < N - 1) {
if (arr[i] < arr[i + 1] || arr[i] == arr[i + 1])
break ;
i++;
}
if (i == N - 1)
return ans;
return -1;
}
var arr = [ 0, 1, 0 ];
document.write(peakIndex(arr));
</script>
|
Time Complexity: O(N)
Auxiliary Space: O(1)
Share your thoughts in the comments
Please Login to comment...