Find a local minima in an array (original) (raw)

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:

// C++ Code to find local minima of array // using Binary Search #include <bits/stdc++.h> using namespace std;

int findLocalMin(vector& 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 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));

`