Input: arr = [100, 180, 260, 310, 40, 535, 695]
Points of local minima: 0 4
Points of local maxima: 3 6
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]
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:
Points of Local maxima are : 1 5 Points of Local minima are : 0 4 8
Time Complexity: O(N)
Auxiliary Space: O(N)
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.
- Find a local minima in an array
- Number of local extrema in an array
- Global and Local Variables in Python
- Local and Global Optimum in Uni-variate Optimization
- N-Queen Problem | Local Search using Hill climbing with random neighbour
- Find all good indices in the given Array
- Find the count of unvisited indices in an infinite array
- Magical Indices in an array
- Find a permutation such that number of indices for which gcd(p[i], i) > 1 is exactly K
- Find the GCD of N Fibonacci Numbers with given Indices
- Sum of all the Composite Numbers from Odd indices of the given array
- Recursive program to find all Indices of a Number
- Count of indices in an array that satisfy the given condition
- Reorder an Array according to given indices with repetition allowed
- Reverse the substrings of the given String according to the given Array of indices
- Check if the array can be sorted using swaps between given indices only
- Range sum queries for anticlockwise rotations of Array by K indices
- Count the pairs in an array such that the difference between them and their indices is equal
- Form smallest number using indices of numbers chosen from Array with sum less than S
- Leftmost and rightmost indices of the maximum and the minimum element of an array
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to firstname.lastname@example.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.