Find a local minima in an array
Last Updated :
03 Mar, 2025
Given an array arr[0 .. n-1] of n distinct integers. The task is to find a local minimum in the array. An element arr[x] is considered a local minimum if it is smaller than both of its adjacent elements, meaning
arr[x] < arr[x - 1] and arr[x] < arr[x + 1] for indices where 1 <= x <= n - 2. For corner elements, arr[0] is a local minimum if arr[0] < arr[1], and arr[n - 1] is a local minimum if arr[n - 1] < arr[n - 2].
Note: Since multiple local minima may exist, the goal is to find any one local minimum efficiently.
Examples:
Input: arr[] = {9, 6, 3, 14, 5, 7, 4}
Output: 2
Explanation: The output prints the index of 3 because it is smaller than both of its neighbors 6 and 14. Note that the indexes of elements 5 and 4 are also valid outputs.
Input: arr[] = {23, 8, 15, 2, 3}
Output: 1
Explanation: The output prints the index of 8 because it is smaller than both of its neighbors 23 and 15. Another valid local minimum is 2 at index 3.
Input: arr[] = {5, 4, 3, 2, 1}
Output: 4
Approach- Using Binary Search - O(log n) Time and O(1) Space
The idea is to use Binary search, to efficiently find the smallest index of a local minimum. The thought process begins with the observation that if the middle element is a local minimum, we store it and continue searching on the left for a smaller index. If the left neighbor is smaller, a local minimum must exist on the left, so we move left. Otherwise, we move right. This ensures an O(log n) solution by always reducing the search space by half, guaranteeing we find the smallest index where the local minimum condition holds.
Steps to implement the above idea:
- Initialize l to 0, r to length of arr - 1, and ans to -1 to store the smallest local minimum index.
- Use a while loop to perform binary search until l is less than or equal to r.
- Compute mid as l + (r - l) / 2 to avoid overflow.
- Check if arr[mid] is a local minimum by comparing it with its neighbors:
- If arr[mid] is smaller than both arr[mid - 1] and arr[mid + 1], update ans and continue searching left.
- If arr[mid - 1] is smaller, move r to mid - 1 to explore the left half. Otherwise, move l to mid + 1 to explore the right half.
- Finally, return ans.
C++
// C++ Code to find local minima of array
// using Binary Search
#include <bits/stdc++.h>
using namespace std;
int findLocalMin(vector<int>& arr) {
int l = 0, r = arr.size() - 1;
// Stores the smallest index of
// local minimum
int ans = -1;
while (l <= r) {
int mid = l + (r - l) / 2;
// Check if mid is a local minimum
if ((mid == 0 || arr[mid] < arr[mid - 1]) &&
(mid == arr.size() - 1 ||
arr[mid] < arr[mid + 1])) {
// Store the local minimum index
ans = mid;
// Continue searching for a smaller index
r = mid - 1;
}
// If left neighbor is smaller, move left
else if (mid > 0 && arr[mid - 1] < arr[mid]) {
r = mid - 1;
}
// Otherwise, move right
else {
l = mid + 1;
}
}
return ans;
}
int main() {
vector<int> arr = {9, 6, 3, 14, 5, 7, 4};
cout << findLocalMin(arr) << endl;
return 0;
}
Java
// Java Code to find local minima of array
// using Binary Search
import java.util.*;
class GfG {
static int findLocalMin(int arr[]) {
int l = 0, r = arr.length - 1;
// Stores the smallest index of
// local minimum
int ans = -1;
while (l <= r) {
int mid = l + (r - l) / 2;
// Check if mid is a local minimum
if ((mid == 0 || arr[mid] < arr[mid - 1]) &&
(mid == arr.length - 1 ||
arr[mid] < arr[mid + 1])) {
// Store the local minimum index
ans = mid;
// Continue searching for a smaller index
r = mid - 1;
}
// If left neighbor is smaller, move left
else if (mid > 0 && arr[mid - 1] < arr[mid]) {
r = mid - 1;
}
// Otherwise, move right
else {
l = mid + 1;
}
}
return ans;
}
public static void main(String[] args) {
int arr[] = {9, 6, 3, 14, 5, 7, 4};
System.out.println(findLocalMin(arr));
}
}
Python
# Python Code to find local minima of array
# using Binary Search
def findLocalMin(arr):
l, r = 0, len(arr) - 1
# Stores the smallest index of
# local minimum
ans = -1
while l <= r:
mid = l + (r - l) // 2
# Check if mid is a local minimum
if (mid == 0 or arr[mid] < arr[mid - 1]) and \
(mid == len(arr) - 1 or arr[mid] < arr[mid + 1]):
# Store the local minimum index
ans = mid
# Continue searching for a smaller index
r = mid - 1
# If left neighbor is smaller, move left
elif mid > 0 and arr[mid - 1] < arr[mid]:
r = mid - 1
# Otherwise, move right
else:
l = mid + 1
return ans
if __name__ == "__main__":
arr = [9, 6, 3, 14, 5, 7, 4]
print(findLocalMin(arr))
C#
// C# Code to find local minima of array
// using Binary Search
using System;
class GfG {
static int FindLocalMin(int[] arr) {
int l = 0, r = arr.Length - 1;
// Stores the smallest index of
// local minimum
int ans = -1;
while (l <= r) {
int mid = l + (r - l) / 2;
// Check if mid is a local minimum
if ((mid == 0 || arr[mid] < arr[mid - 1]) &&
(mid == arr.Length - 1 ||
arr[mid] < arr[mid + 1])) {
// Store the local minimum index
ans = mid;
// Continue searching for a smaller index
r = mid - 1;
}
// If left neighbor is smaller, move left
else if (mid > 0 && arr[mid - 1] < arr[mid]) {
r = mid - 1;
}
// Otherwise, move right
else {
l = mid + 1;
}
}
return ans;
}
static void Main() {
int[] arr = {9, 6, 3, 14, 5, 7, 4};
Console.WriteLine(FindLocalMin(arr));
}
}
JavaScript
// JavaScript Code to find local minima of array
// using Binary Search
function findLocalMin(arr) {
let l = 0, r = arr.length - 1;
// Stores the smallest index of
// local minimum
let ans = -1;
while (l <= r) {
let mid = l + Math.floor((r - l) / 2);
// Check if mid is a local minimum
if ((mid === 0 || arr[mid] < arr[mid - 1]) &&
(mid === arr.length - 1 ||
arr[mid] < arr[mid + 1])) {
// Store the local minimum index
ans = mid;
// Continue searching for a smaller index
r = mid - 1;
}
// If left neighbor is smaller, move left
else if (mid > 0 && arr[mid - 1] < arr[mid]) {
r = mid - 1;
}
// Otherwise, move right
else {
l = mid + 1;
}
}
return ans;
}
// Driver code
const arr = [9, 6, 3, 14, 5, 7, 4];
console.log(findLocalMin(arr));
Similar Reads
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 6Explanation:Given array can be break as below sub-arrays:1.
9 min read
Number of local extrema in an array You are given an array on n-elements. An extrema is an elements which is either greater than its both of neighbors or less than its both neighbors. You have to calculate the number of local extrema in given array. Note : 1st and last elements are not extrema. Examples : Input : a[] = {1, 5, 2, 5} Ou
6 min read
Minimum in a Sorted and Rotated Array Given a sorted array of distinct elements arr[] of size n that is rotated at some unknown point, the task is to find the minimum element in it. Examples: Input: arr[] = [5, 6, 1, 2, 3, 4]Output: 1Explanation: 1 is the minimum element present in the array.Input: arr[] = [3, 1, 2]Output: 1Explanation:
9 min read
Search for an element in a Mountain Array Given a mountain array arr[] and an integer X, the task is to find the smallest index of X in the given array. If no such index is found, print -1. Examples: Input: arr = {1, 2, 3, 4, 5, 3, 1}, X = 3Output: 2Explanation: The smallest index of X(= 3) in the array is 2. Therefore, the required output
14 min read
Find minimum value of arr[j] + abs(j - i) for all indices Given an array arr[] of size N, the task is for each index i (1â¤iâ¤n) find it's minimum value, that can be calculated by the formula (arr[j] + abs(jâi)), for 0<=j<n and j != i. Examples: Input: arr = { 1, 3, 11, 2, 15, 7, 5 }Output: 4 2 3 4 3 4 5Explanation: For i = 0, the minimum value is for
9 min read