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
- Rearrange array to maximize count of local minima
- Largest subsequence such that all indices and all values are multiples individually
- Find all good indices in the given Array
- Recursive program to find all Indices of a Number
- Sum of all the Composite Numbers from Odd indices of the given array
- Find the count of unvisited indices in an infinite array
- Leftmost and rightmost indices of the maximum and the minimum element of an array
- Number of indices pair such that element pair sum from first Array is greater than second Array
- Count clockwise array rotations required to maximize count of array elements present at indices same as their value
- Count the pairs in an array such that the difference between them and their indices is equal
- 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
- Magical Indices in an array
- Check if the array can be sorted using swaps between given indices only
- Count of indices in an array that satisfy the given condition
- Reverse the substrings of the given String according to the given Array of indices
- Reorder an Array according to given indices with repetition allowed
- Range sum queries for anticlockwise rotations of Array by K indices
- Lexicographically smallest array formed by at most one swap for every pair of adjacent indices
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.