Skip to content
Related Articles

Related Articles

Find indices of all local maxima and local minima in an Array

View Discussion
Improve Article
Save Article
  • Difficulty Level : Easy
  • Last Updated : 08 Mar, 2022
View Discussion
Improve Article
Save Article

Given an array arr[] of integers. The task is to find the indices of all local minima and local maxima in the given array.
Examples:

Input: arr = [100, 180, 260, 310, 40, 535, 695]
Output:
Points of local minima: 0 4 
Points of local maxima: 3 6
Explanation:
Given array can be break as below sub-arrays:
1. first sub array 
[100, 180, 260, 310] 
index of local minima = 0 
index of local maxima = 3
2. second sub array 
[40, 535, 695] 
index of local minima = 4 
index of local maxima = 6

Input: arr = [23, 13, 25, 29, 33, 19, 34, 45, 65, 67]
Output:
Points of local minima: 1 5
Points of local maxima: 0 4 9 
 

 

Approach: The idea is to iterate over the given array arr[] and check if each element of the array is smallest or greatest among their adjacent element. If it is smallest then it is local minima and if it is greatest then it is local maxima. Below are the steps:

  1. Create two arrays max[] and min[] to store all the local maxima and local minima.
  2. Traverse the given array and append the index of the array into the array max[] and min[] according to the below conditions:
    • If arr[i – 1] > arr[i] < arr[i + 1] then append that index to min[].
    • If arr[i – 1] < arr[i] > arr[i + 1] then append that index to max[].
  3. Check for the local maxima and minima conditions for the first and last elements separately.
  4. Print the indexes stored in min[] and max[].

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 all the local maxima
// and minima in the given array arr[]
void findLocalMaximaMinima(int n, int arr[])
{
     
    // Empty vector to store points of
    // local maxima and minima
    vector<int> mx, mn;
 
    // Checking whether the first point is
    // local maxima or minima or none
    if (arr[0] > arr[1])
        mx.push_back(0);
 
    else if (arr[0] < arr[1])
        mn.push_back(0);
 
    // Iterating over all points to check
    // local maxima and local minima
    for(int i = 1; i < n - 1; i++)
    {
         
    // Condition for local minima
    if ((arr[i - 1] > arr[i]) and
        (arr[i] < arr[i + 1]))
        mn.push_back(i);
         
    // Condition for local maxima
    else if ((arr[i - 1] < arr[i]) and
                (arr[i] > arr[i + 1]))
        mx.push_back(i);
    }
 
    // Checking whether the last point is
    // local maxima or minima or none
    if (arr[n - 1] > arr[n - 2])
        mx.push_back(n - 1);
 
    else if (arr[n - 1] < arr[n - 2])
        mn.push_back(n - 1);
 
    // Print all the local maxima and
    // local minima indexes stored
    if (mx.size() > 0)
    {
        cout << "Points of Local maxima are : ";
        for(int a : mx)
        cout << a << " ";
        cout << endl;
    }
    else
        cout << "There are no points of "
            << "Local Maxima \n";
 
    if (mn.size() > 0)
    {
        cout << "Points of Local minima are : ";
        for(int a : mn)
        cout << a << " ";
        cout << endl;
    }
    else
        cout << "There are no points of "
            << "Local Minima \n";
}
 
// Driver Code
int main()
{
    int N = 9;
 
    // Given array arr[]
    int arr[] = { 10, 20, 15, 14, 13,
                25, 5, 4, 3 };
 
    // Function call
    findLocalMaximaMinima(N, arr);
    return 0;    
}
 
// This code is contributed by himanshu77

Java




// Java program for the above approach
import java.util.*;
class GFG{
     
// Function to find all the local maxima
// and minima in the given array arr[]
public static void findLocalMaximaMinima(int n,
                                        int[] arr)
{
     
    // Empty vector to store points of
    // local maxima and minima
    Vector<Integer> mx = new Vector<Integer>();
    Vector<Integer> mn = new Vector<Integer>();
 
    // Checking whether the first point is
    // local maxima or minima or none
    if (arr[0] > arr[1])
        mx.add(0);
 
    else if (arr[0] < arr[1])
        mn.add(0);
 
    // Iterating over all points to check
    // local maxima and local minima
    for(int i = 1; i < n - 1; i++)
    {
        // Condition for local minima
        if ((arr[i - 1] > arr[i]) &&
            (arr[i] < arr[i + 1]))
            mn.add(i);
             
        // Condition for local maxima
        else if ((arr[i - 1] < arr[i]) &&
                (arr[i] > arr[i + 1]))
            mx.add(i);
    }
 
    // Checking whether the last point is
    // local maxima or minima or none
    if (arr[n - 1] > arr[n - 2])
        mx.add(n - 1);
 
    else if (arr[n - 1] < arr[n - 2])
        mn.add(n - 1);
 
    // Print all the local maxima and
    // local minima indexes stored
    if (mx.size() > 0)
    {
        System.out.print("Points of Local " +
                        "maxima are : ");
        for(Integer a : mx)
            System.out.print(a + " ");
        System.out.println();
    }
    else
        System.out.println("There are no points " +
                        "of Local Maxima ");
 
    if (mn.size() > 0)
    {
        System.out.print("Points of Local " +
                        "minima are : ");
        for(Integer a : mn)
            System.out.print(a + " ");
        System.out.println();
    }
    else
        System.out.println("There are no points of " +
                        "Local Maxima ");
}
 
// Driver code
public static void main(String[] args)
{
    int N = 9;
 
    // Given array arr[]
    int arr[] = { 10, 20, 15, 14, 13,
                25, 5, 4, 3 };
 
    // Function call
    findLocalMaximaMinima(N, arr);
}
}
 
// This code is contributed by divyeshrabadiya07

Python3




# Python3 program for the above approach
 
# Function to find all the local maxima
# and minima in the given array arr[]
 
def findLocalMaximaMinima(n, arr):
 
    # Empty lists to store points of
    # local maxima and minima
    mx = []
    mn = []
 
    # Checking whether the first point is
    # local maxima or minima or neither
    if(arr[0] > arr[1]):
        mx.append(0)
    elif(arr[0] < arr[1]):
        mn.append(0)
 
    # Iterating over all points to check
    # local maxima and local minima
    for i in range(1, n-1):
 
        # Condition for local minima
        if(arr[i-1] > arr[i] < arr[i + 1]):
            mn.append(i)
 
        # Condition for local maxima
        elif(arr[i-1] < arr[i] > arr[i + 1]):
            mx.append(i)
 
    # Checking whether the last point is
    # local maxima or minima or neither
    if(arr[-1] > arr[-2]):
        mx.append(n-1)
    elif(arr[-1] < arr[-2]):
        mn.append(n-1)
 
        # Print all the local maxima and
        # local minima indexes stored
    if(len(mx) > 0):
        print("Points of Local maxima"\
            " are : ", end ='')
        print(*mx)
    else:
        print("There are no points of"\
            " Local maxima.")
 
    if(len(mn) > 0):
        print("Points of Local minima"\
            " are : ", end ='')
        print(*mn)
    else:
        print("There are no points"\
            " of Local minima.")
 
# Driver Code
if __name__ == '__main__':
 
    N = 9
    # Given array arr[]
    arr = [10, 20, 15, 14, 13, 25, 5, 4, 3]
 
    # Function Call
    findLocalMaximaMinima(N, arr)

C#




// C# program for the above approach
using System;
using System.Collections;
using System.Collections.Generic;
 
class GFG{
     
// Function to find all the local maxima
// and minima in the given array arr[]
public static void findLocalMaximaMinima(int n,
                                         int[] arr)
{
     
    // Empty vector to store points of
    // local maxima and minima
    ArrayList mx = new ArrayList();
    ArrayList mn = new ArrayList();
 
    // Checking whether the first point is
    // local maxima or minima or none
    if (arr[0] > arr[1])
        mx.Add(0);
 
    else if (arr[0] < arr[1])
        mn.Add(0);
 
    // Iterating over all points to check
    // local maxima and local minima
    for(int i = 1; i < n - 1; i++)
    {
         
        // Condition for local minima 
        if ((arr[i - 1] > arr[i]) && 
            (arr[i] < arr[i + 1]))
            mn.Add(i);
             
        // Condition for local maxima
        else if ((arr[i - 1] < arr[i]) && 
                 (arr[i] > arr[i + 1])) 
            mx.Add(i);
    }
 
    // Checking whether the last point is
    // local maxima or minima or none
    if (arr[n - 1] > arr[n - 2])
        mx.Add(n - 1);
     
    else if (arr[n - 1] < arr[n - 2]) 
        mn.Add(n - 1); 
 
    // Print all the local maxima and 
    // local minima indexes stored 
    if (mx.Count > 0) 
    {
        Console.Write("Points of Local " +
                      "maxima are : ");
        foreach(int a in mx)
            Console.Write(a + " ");
             
        Console.Write("\n");
    }
    else
        Console.Write("There are no points " +
                      "of Local Maxima ");
 
    if (mn.Count > 0)
    {
        Console.Write("Points of Local " +
                        "minima are : ");
        foreach(int a in mn)
            Console.Write(a + " ");
             
        Console.Write("\n");
    }
    else
        Console.Write("There are no points of " +
                      "Local Maxima ");
}
 
// Driver code
public static void Main(string[] args)
{
    int N = 9;
 
    // Given array arr[]
    int []arr = { 10, 20, 15, 14, 13,
                  25, 5, 4, 3 };
 
    // Function call
    findLocalMaximaMinima(N, arr);
}
}
 
// This code is contributed by rutvik_56

Javascript




<script>
 
// JavaScript program for the above approach
 
// Function to find all the local maxima
// and minima in the given array arr[]
function findLocalMaximaMinima(n, arr)
{
     
    // Empty vector to store points of
    // local maxima and minima
    let mx = [], mn = [];
 
    // Checking whether the first point is
    // local maxima or minima or none
    if (arr[0] > arr[1])
        mx.push(0);
 
    else if (arr[0] < arr[1])
        mn.push(0);
 
    // Iterating over all points to check
    // local maxima and local minima
    for(let i = 1; i < n - 1; i++)
    {
         
    // Condition for local minima
    if ((arr[i - 1] > arr[i]) &&
        (arr[i] < arr[i + 1]))
        mn.push(i);
         
    // Condition for local maxima
    else if ((arr[i - 1] < arr[i]) &&
                (arr[i] > arr[i + 1]))
        mx.push(i);
    }
 
    // Checking whether the last point is
    // local maxima or minima or none
    if (arr[n - 1] > arr[n - 2])
        mx.push(n - 1);
 
    else if (arr[n - 1] < arr[n - 2])
        mn.push(n - 1);
 
    // Print all the local maxima and
    // local minima indexes stored
    if (mx.length > 0)
    {
        document.write("Points of Local maxima are : ");
        for(let a of mx){
            document.write(a," ");
        }
        document.write("</br>");
    }
    else
        document.write("There are no points of Local Maxima ","</br>");
 
    if (mn.length > 0)
    {
        document.write("Points of Local minima are : ");
        for(let a of mn){
            document.write(a," ");
        }
        document.write("</br>");
    }
    else
        document.write("There are no points of Local Minima","</br>");
}
 
// Driver Code
 
let N = 9;
 
// Given array arr[]
let arr = [ 10, 20, 15, 14, 13, 25, 5, 4, 3 ];
 
// Function call
findLocalMaximaMinima(N, arr);
 
 
// This code is contributed by shinjanpatra
 
 
</script>

Output: 

Points of Local maxima are : 1 5
Points of Local minima are : 0 4 8

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


My Personal Notes arrow_drop_up
Recommended Articles
Page :

Start Your Coding Journey Now!