Find indices of all local maxima and local minima in an Array
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:
- Create two arrays max[] and min[] to store all the local maxima and local minima.
- 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[].
- Check for the local maxima and minima conditions for the first and last elements separately.
- Print the indexes stored in min[] and max[].
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
void findLocalMaximaMinima( int n, int arr[])
{
vector< int > mx, mn;
if (arr[0] > arr[1])
mx.push_back(0);
else if (arr[0] < arr[1])
mn.push_back(0);
for ( int i = 1; i < n - 1; i++)
{
if ((arr[i - 1] > arr[i]) and
(arr[i] < arr[i + 1]))
mn.push_back(i);
else if ((arr[i - 1] < arr[i]) and
(arr[i] > arr[i + 1]))
mx.push_back(i);
}
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);
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" ;
}
int main()
{
int N = 9;
int arr[] = { 10, 20, 15, 14, 13,
25, 5, 4, 3 };
findLocalMaximaMinima(N, arr);
return 0;
}
|
Java
import java.util.*;
class GFG{
public static void findLocalMaximaMinima( int n,
int [] arr)
{
Vector<Integer> mx = new Vector<Integer>();
Vector<Integer> mn = new Vector<Integer>();
if (arr[ 0 ] > arr[ 1 ])
mx.add( 0 );
else if (arr[ 0 ] < arr[ 1 ])
mn.add( 0 );
for ( int i = 1 ; i < n - 1 ; i++)
{
if ((arr[i - 1 ] > arr[i]) &&
(arr[i] < arr[i + 1 ]))
mn.add(i);
else if ((arr[i - 1 ] < arr[i]) &&
(arr[i] > arr[i + 1 ]))
mx.add(i);
}
if (arr[n - 1 ] > arr[n - 2 ])
mx.add(n - 1 );
else if (arr[n - 1 ] < arr[n - 2 ])
mn.add(n - 1 );
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 " );
}
public static void main(String[] args)
{
int N = 9 ;
int arr[] = { 10 , 20 , 15 , 14 , 13 ,
25 , 5 , 4 , 3 };
findLocalMaximaMinima(N, arr);
}
}
|
Python3
def findLocalMaximaMinima(n, arr):
mx = []
mn = []
if (arr[ 0 ] > arr[ 1 ]):
mx.append( 0 )
elif (arr[ 0 ] < arr[ 1 ]):
mn.append( 0 )
for i in range ( 1 , n - 1 ):
if (arr[i - 1 ] > arr[i] < arr[i + 1 ]):
mn.append(i)
elif (arr[i - 1 ] < arr[i] > arr[i + 1 ]):
mx.append(i)
if (arr[ - 1 ] > arr[ - 2 ]):
mx.append(n - 1 )
elif (arr[ - 1 ] < arr[ - 2 ]):
mn.append(n - 1 )
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." )
if __name__ = = '__main__' :
N = 9
arr = [ 10 , 20 , 15 , 14 , 13 , 25 , 5 , 4 , 3 ]
findLocalMaximaMinima(N, arr)
|
C#
using System;
using System.Collections;
using System.Collections.Generic;
class GFG{
public static void findLocalMaximaMinima( int n,
int [] arr)
{
ArrayList mx = new ArrayList();
ArrayList mn = new ArrayList();
if (arr[0] > arr[1])
mx.Add(0);
else if (arr[0] < arr[1])
mn.Add(0);
for ( int i = 1; i < n - 1; i++)
{
if ((arr[i - 1] > arr[i]) &&
(arr[i] < arr[i + 1]))
mn.Add(i);
else if ((arr[i - 1] < arr[i]) &&
(arr[i] > arr[i + 1]))
mx.Add(i);
}
if (arr[n - 1] > arr[n - 2])
mx.Add(n - 1);
else if (arr[n - 1] < arr[n - 2])
mn.Add(n - 1);
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 " );
}
public static void Main( string [] args)
{
int N = 9;
int []arr = { 10, 20, 15, 14, 13,
25, 5, 4, 3 };
findLocalMaximaMinima(N, arr);
}
}
|
Javascript
<script>
function findLocalMaximaMinima(n, arr)
{
let mx = [], mn = [];
if (arr[0] > arr[1])
mx.push(0);
else if (arr[0] < arr[1])
mn.push(0);
for (let i = 1; i < n - 1; i++)
{
if ((arr[i - 1] > arr[i]) &&
(arr[i] < arr[i + 1]))
mn.push(i);
else if ((arr[i - 1] < arr[i]) &&
(arr[i] > arr[i + 1]))
mx.push(i);
}
if (arr[n - 1] > arr[n - 2])
mx.push(n - 1);
else if (arr[n - 1] < arr[n - 2])
mn.push(n - 1);
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>" );
}
let N = 9;
let arr = [ 10, 20, 15, 14, 13, 25, 5, 4, 3 ];
findLocalMaximaMinima(N, arr);
</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)
Last Updated :
08 Mar, 2022
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...