Skip to content
Related Articles

Related Articles

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++




// C++ program for the above approach
#include <bits/stdc++.h>
using namespace std;
 
// Function to find the peak
// index for the given array
int peakIndex(int arr[], int N)
{
     
    // Base Case
    if (N < 3)
        return -1;
 
    int i = 0;
 
    // Check for strictly
    // increasing array
    while (i + 1 < N)
    {
         
        // If the strictly increasing
        // condition is violated, then break
        if (arr[i + 1] < arr[i] ||
            arr[i] == arr[i + 1])
            break;
             
        i++;
    }
 
    if (i == 0 || i == N - 1)
        return -1;
 
    // Stores the value of i, which
    // is a potential peak index
    int ans = i;
 
    // Second traversal, for
    // strictly decreasing array
    while (i < N - 1)
    {
         
        // When the strictly
        // decreasing condition is
        // violated, then break
        if (arr[i] < arr[i + 1] ||
            arr[i] == arr[i + 1])
            break;
             
        i++;
    }
 
    // If i = N - 1, it means that
    // ans is the peak index
    if (i == N - 1)
        return ans;
 
    // Otherwise, peak index doesn't exist
    return -1;
}
 
// Driver Code
int main()
{
    int arr[] = { 0, 1, 0 };
    int N = sizeof(arr) / sizeof(arr[0]);
 
    cout << peakIndex(arr, N) << "\n";
 
    return 0;
}
 
// This code is contributed by Kingash

Java




// Java program for the above approach
 
import java.io.*;
import java.util.*;
 
class GFG {
 
    // Function to find the peak
    // index for the given array
    public static int peakIndex(int[] arr)
    {
        int N = arr.length;
 
        // Base Case
        if (arr.length < 3)
            return -1;
 
        int i = 0;
 
        // Check for strictly
        // increasing array
        while (i + 1 < N) {
 
            // If the strictly increasing
            // condition is violated, then break
            if (arr[i + 1] < arr[i]
                || arr[i] == arr[i + 1])
                break;
            i++;
        }
 
        if (i == 0 || i == N - 1)
            return -1;
 
        // Stores the value of i, which
        // is a potential peak index
        int ans = i;
 
        // Second traversal, for
        // strictly decreasing array
        while (i < N - 1) {
 
            // When the strictly
            // decreasing condition is
            // violated, then break
            if (arr[i] < arr[i + 1]
                || arr[i] == arr[i + 1])
                break;
            i++;
        }
 
        // If i = N - 1, it means that
        // ans is the peak index
        if (i == N - 1)
            return ans;
 
        // Otherwise, peak index doesn't exist
        return -1;
    }
 
    // Driver Code
    public static void main(String[] args)
    {
        int[] arr = { 0, 1, 0 };
        System.out.println(peakIndex(arr));
    }
}

Python3




# Python3 program for the above approach
 
# Function to find the peak
# index for the given array
def peakIndex(arr):
     
    N = len(arr)
 
    # Base Case
    if (len(arr) < 3):
        return -1
 
    i = 0
 
    # Check for strictly
    # increasing array
    while (i + 1 < N):
 
        # If the strictly increasing
        # condition is violated, then break
        if (arr[i + 1] < arr[i] or
            arr[i] == arr[i + 1]):
            break
         
        i += 1
 
    if (i == 0 or i == N - 1):
        return -1
 
    # Stores the value of i, which
    # is a potential peak index
    ans = i
 
    # Second traversal, for
    # strictly decreasing array
    while (i < N - 1):
 
        # When the strictly
        # decreasing condition is
        # violated, then break
        if (arr[i] < arr[i + 1] or
            arr[i] == arr[i + 1]):
            break
         
        i += 1
 
    # If i = N - 1, it means that
    # ans is the peak index
    if (i == N - 1):
        return ans
 
    # Otherwise, peak index doesn't exist
    return -1
 
# Driver Code
if __name__ == '__main__':
     
    arr = [0, 1, 0]
     
    print(peakIndex(arr))
 
# This code is contributed by mohit kumar 29

C#




// C# program for the above approach
using System;
using System.Collections.Generic;
 
class GFG{
     
// Function to find the peak
// index for the given array
public static int peakIndex(int[] arr)
{
    int N = arr.Length;
 
    // Base Case
    if (arr.Length < 3)
        return -1;
 
    int i = 0;
 
    // Check for strictly
    // increasing array
    while (i + 1 < N)
    {
         
        // If the strictly increasing
        // condition is violated, then break
        if (arr[i + 1] < arr[i] ||
            arr[i] == arr[i + 1])
            break;
             
        i++;
    }
 
    if (i == 0 || i == N - 1)
        return -1;
 
    // Stores the value of i, which
    // is a potential peak index
    int ans = i;
 
    // Second traversal, for
    // strictly decreasing array
    while (i < N - 1)
    {
         
        // When the strictly
        // decreasing condition is
        // violated, then break
        if (arr[i] < arr[i + 1] ||
            arr[i] == arr[i + 1])
            break;
             
        i++;
    }
 
    // If i = N - 1, it means that
    // ans is the peak index
    if (i == N - 1)
        return ans;
 
    // Otherwise, peak index doesn't exist
    return -1;
}
 
// Driver Code
static public void Main()
{
    int[] arr = { 0, 1, 0 };
     
    Console.WriteLine(peakIndex(arr));
}
}
 
// This code is contributed by splevel62

Javascript




<script>
 
// Javascript program for the above approach
 
    // Function to find the peak
    // index for the given array
    function peakIndex(arr)
    {
        var N = arr.length;
 
        // Base Case
        if (arr.length < 3)
            return -1;
 
        var i = 0;
 
        // Check for strictly
        // increasing array
        while (i + 1 < N) {
 
            // If the strictly increasing
            // condition is violated, then break
            if (arr[i + 1] < arr[i] || arr[i] == arr[i + 1])
                break;
            i++;
        }
 
        if (i == 0 || i == N - 1)
            return -1;
 
        // Stores the value of i, which
        // is a potential peak index
        var ans = i;
 
        // Second traversal, for
        // strictly decreasing array
        while (i < N - 1) {
 
            // When the strictly
            // decreasing condition is
            // violated, then break
            if (arr[i] < arr[i + 1] || arr[i] == arr[i + 1])
                break;
            i++;
        }
 
        // If i = N - 1, it means that
        // ans is the peak index
        if (i == N - 1)
            return ans;
 
        // Otherwise, peak index doesn't exist
        return -1;
    }
 
    // Driver Code
     
        var arr = [ 0, 1, 0 ];
        document.write(peakIndex(arr));
 
// This code contributed by Rajput-Ji
 
</script>
Output: 
1

 

Time Complexity: O(N)
Auxiliary Space: O(1)

Attention reader! Don’t stop learning now. Get hold of all the important mathematical concepts for competitive programming with the Essential Maths for CP Course at a student-friendly price. To complete your preparation from learning a language to DS Algo and many more,  please refer Complete Interview Preparation Course.

My Personal Notes arrow_drop_up
Recommended Articles
Page :