Find a local minima in an array

• Difficulty Level : Easy
• Last Updated : 19 Dec, 2021

Given an array arr[0 .. n-1] of distinct integers, the task is to find a local minima 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  // 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);    printf("Index of a local minima is %d",                           localMin(arr, n));    return 0;}

Java

 // A Java program to find a local minima in an arrayimport 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

 # Python3 program to find a# local minima in an array # A binary search based function that# returns index of a local minima.def localMinUtil(arr, low, high, n):             # Find index of middle element    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] < 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.    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 codearr = [4, 3, 1, 14, 16, 40]n = len(arr)print("Index of a local minima is " ,                    localMin(arr, n))                     # This code is contributed by Anant Agarwal.

C#

 // A C# program to find a// local minima in an arrayusing 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

 \$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.?>

Output:

Index of a local minima is 2

Time Complexity : O(Log n)
Related Problem :
Find a peak element
This article is contributed by Roshni Agarwal. If you like GeeksforGeeks and would like to contribute, you can also write an article using write.geeksforgeeks.org or mail your article to review-team@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.