Open In App

Find a local minima in an array

Last Updated : 15 Mar, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

Given an array arr[0 .. n-1] of distinct integers, the task is to find a local minimum in it. We say that an element arr[x] is a local minimum if it is less than both its neighbors. 

  • For corner elements, we need to consider only one neighbor for comparison.
  • There can be more than one local minima in an array, we need to find one of them.

Examples: 

Input: arr[] = {9, 6, 3, 14, 5, 7, 4};
Output: Index of local minima is 2
The output prints index of 3 because it is 
smaller than both of its neighbors. 
Note that indexes of elements 5 and 4 are 
also valid outputs.

Input: arr[] = {23, 8, 15, 2, 3};
Output: Index of local minima is 1

Input: arr[] = {1, 2, 3};
Output: Index of local minima is 0

Input: arr[] = {3, 2, 1};
Output: Index of local minima is 2
 

A simple solution is to do a linear scan of array and as soon as we find a local minima, we return it. The worst case time complexity of this method would be O(n).

An efficient solution is based on Binary Search. We compare middle element with its neighbors. If middle element is not greater than any of its neighbors, then we return it. If the middle element is greater than its left neighbor, then there is always a local minima in left half (Why? take few examples). If the middle element is greater than its right neighbor, then there is always a local minima in right half (due to same reason as left half). 

Below is the implementation of the above idea : 

C++




// A C++ program to find a local minima in an array
#include <stdio.h>
 
// A binary search based function that returns
// index of a local minima.
int localMinUtil(int arr[], int low, int high, int n)
{
    // Find index of middle element
    int mid = low + (high - low)/2;  /* (low + high)/2 */
 
    // Compare middle element with its neighbours
    // (if neighbours exist)
    if ((mid == 0 || arr[mid-1] > arr[mid]) &&
            (mid == n-1 || arr[mid+1] > arr[mid]))
        return mid;
 
    // If middle element is not minima and its left
    // neighbour is smaller than it, then left half
    // must have a local minima.
    else if (mid > 0 && arr[mid-1] < arr[mid])
        return localMinUtil(arr, low, (mid -1), n);
 
    // If middle element is not minima and its right
    // neighbour is smaller than it, then right half
    // must have a local minima.
    return localMinUtil(arr, (mid + 1), high, n);
}
 
// A wrapper over recursive function localMinUtil()
int localMin(int arr[], int n)
{
    return localMinUtil(arr, 0, n-1, n);
}
 
/* Driver program to check above functions */
int main()
{
    int arr[] = {4, 3, 1, 14, 16, 40};
    int n = sizeof(arr)/sizeof(arr[0]);
    printf("Index of a local minima is %d",
                           localMin(arr, n));
    return 0;
}


Java




// A Java program to find a local minima in an array
import java.io.*;
 
class GFG
{
     
    // A binary search based function that returns
    // index of a local minima.
    public static int localMinUtil(int[] arr, int low,
                                   int high, int n)
    {
         
        // Find index of middle element
        int mid = low + (high - low) / 2;
         
         // Compare middle element with its neighbours
        // (if neighbours exist)
        if(mid == 0 || arr[mid - 1] > arr[mid] && mid == n - 1 ||
           arr[mid] < arr[mid + 1])
                return mid;
         
        // If middle element is not minima and its left
        // neighbour is smaller than it, then left half
        // must have a local minima.
        else if(mid > 0 && arr[mid - 1] < arr[mid])
                return localMinUtil(arr, low, mid - 1, n);
         
        // If middle element is not minima and its right
        // neighbour is smaller than it, then right half
        // must have a local minima.
        return localMinUtil(arr, mid + 1, high, n);
    }
     
    // A wrapper over recursive function localMinUtil()
    public static int localMin(int[] arr, int n)
    {
        return localMinUtil(arr, 0, n - 1, n);
    }
     
     
    public static void main (String[] args)
    {
         
        int arr[] = {4, 3, 1, 14, 16, 40};
        int n = arr.length;
        System.out.println("Index of a local minima is " + localMin(arr, n));
    
    }
}
 
//This code is contributed by Dheerendra Singh


Python3




# A Python program to find a local minima in an array
 
def localMinUtil(arr, low, high, n):
    # Find index of middle element
    mid = low + (high - low) // 2  # (low + high) // 2
 
    # Compare middle element with its neighbours (if neighbours exist)
    if ((mid == 0 or arr[mid-1] > arr[mid]) and
            (mid == n-1 or arr[mid+1] > arr[mid])):
        return mid
 
    # If middle element is not minima and its left neighbour is smaller than it, then left half must have a local minima.
    elif (mid > 0 and arr[mid-1] < arr[mid]):
        return localMinUtil(arr, low, (mid -1), n)
 
    # If middle element is not minima and its right neighbour is smaller than it, then right half must have a local minima.
    return localMinUtil(arr, (mid + 1), high, n)
 
 
# A wrapper over recursive function localMinUtil()
def localMin(arr, n):
    return localMinUtil(arr, 0, n-1, n)
 
 
# Driver program to check above functions
if __name__ == '__main__':
    arr = [4, 3, 1, 14, 16, 40]
    n = len(arr)
    print("Index of a local minima is", localMin(arr, n))


C#




// A C# program to find a
// local minima in an array
using System;
 
class GFG
{
     
    // A binary search based function that returns
    // index of a local minima.
    public static int localMinUtil(int[] arr, int low,
                                   int high, int n)
    {
         
        // Find index of middle element
        int mid = low + (high - low) / 2;
         
        // Compare middle element with its neighbours
        // (if neighbours exist)
        if(mid == 0 || arr[mid - 1] > arr[mid] &&
           mid == n - 1 || arr[mid] < arr[mid + 1])
                return mid;
         
        // If middle element is not minima and its left
        // neighbour is smaller than it, then left half
        // must have a local minima.
        else if(mid > 0 && arr[mid - 1] < arr[mid])
                return localMinUtil(arr, low, mid - 1, n);
         
        // If middle element is not minima and its right
        // neighbour is smaller than it, then right half
        // must have a local minima.
        return localMinUtil(arr, mid + 1, high, n);
    }
     
    // A wrapper over recursive function localMinUtil()
    public static int localMin(int[] arr, int n)
    {
        return localMinUtil(arr, 0, n - 1, n);
    }
     
    // Driver Code
    public static void Main ()
    {
         
        int []arr = {4, 3, 1, 14, 16, 40};
        int n = arr.Length;
        Console.WriteLine("Index of a local minima is " +
                            localMin(arr, n));
     
    }
}
 
// This code is contributed by vt_m.


PHP




<?php
// A PHP program to find a
// local minima in an array
 
// A binary search based
// function that returns
// index of a local minima.
function localMinUtil($arr, $low, $high, $n)
{
     
    // Find index of middle element
    /* (low + high)/2 */
    $mid = $low + ($high - $low) / 2;
 
    // Compare middle element
    // with its neighbours
    // (if neighbours exist)
    if (($mid == 0 or $arr[$mid - 1] > $arr[$mid]) and
        ($mid == $n - 1 or $arr[$mid + 1] > $arr[$mid]))
        return $mid;
 
    // If middle element is not
    // minima and its left
    // neighbour is smaller than
    // it, then left half
    // must have a local minima.
    else if ($mid > 0 and $arr[$mid - 1] < $arr[$mid])
        return localMinUtil($arr, $low, ($mid - 1), $n);
 
    // If middle element is not
    // minima and its right
    // neighbour is smaller than
    // it, then right half
    // must have a local minima.
    return localMinUtil(arr, (mid + 1), high, n);
}
 
// A wrapper over recursive
// function localMinUtil()
function localMin( $arr, $n)
{
    return floor(localMinUtil($arr, 0, $n - 1, $n));
}
 
    // Driver Code
    $arr = array(4, 3, 1, 14, 16, 40);
    $n = count($arr);
    echo "Index of a local minima is ",
                    localMin($arr, $n);
                     
// This code is contributed by anuj_67.
?>


Javascript




// A JavaScript program to find a local minima in an array
 
// A binary search based function that returns
// index of a local minima.
function localMinUtil(arr, low, high ,n)
{
 
    // Find index of middle element
    let mid = Math.trunc(low + (high - low) / 2);
      
     // Compare middle element with its neighbours
    // (if neighbours exist)
    if(mid == 0 || arr[mid - 1] > arr[mid] && mid == n - 1 ||
       arr[mid] < arr[mid + 1])
            return mid;
      
    // If middle element is not minima and its left
    // neighbour is smaller than it, then left half
    // must have a local minima.
    else if(mid > 0 && arr[mid - 1] < arr[mid])
            return localMinUtil(arr, low, mid - 1, n);
      
    // If middle element is not minima and its right
    // neighbour is smaller than it, then right half
    // must have a local minima.
    return localMinUtil(arr, mid + 1, high, n);
}
 
// A wrapper over recursive function localMinUtil()
function localMin(arr, n){
    return localMinUtil(arr, 0, n-1, n);
}
 
let arr = [ 4, 3, 1, 14, 16, 40 ];
let n = arr.length;
console.log("Index of a local mimima is " + localMin(arr, n));
 
// This code is contributed by lokeshmvs21.


Output

Index of a local minima is 2

Time Complexity: O(Log n)
Auxiliary Space: O(log n), As the recursive call is there, hence implicit stack is used.

Related Problem : 
Find a peak element

 



Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads