Skip to content
Related Articles

Related Articles

Minimum in an array which is first decreasing then increasing

View Discussion
Improve Article
Save Article
  • Difficulty Level : Medium
  • Last Updated : 14 Jun, 2022
View Discussion
Improve Article
Save Article

Given an array of N integers where array elements form a strictly decreasing and increasing sequence. The task is to find the smallest number in such an array. 
Constraints: N >= 3 
Examples: 
 

Input: a[] = {2, 1, 2, 3, 4}
Output: 1

Input: a[] = {8, 5, 4, 3, 4, 10}
Output: 3 

 

A naive approach is to linearly traverse the array and find out the smallest number. Time Complexity: O(N), we need to use a loop to traverse N times linearly. 

Auxiliary Space: O(1), as we are not using any extra space.
An efficient approach is to modify the binary search and use it. Divide the array into two halves use binary search to check if a[mid] < a[mid+1] or not. If a[mid] < a[mid+1], then the smallest number lies in the first half which is low to mid, else it lies in the second half which is mid+1 to high
Algorithm: 
 

while(lo > 1

    if a[mid] < a[mid+1] then hi = mid

    else lo = mid+1
} 

Below is the implementation of the above approach: 
 

C++




// C++ program to find the smallest number
// in an array of decrease and increasing numbers
#include <bits/stdc++.h>
using namespace std;
 
// Function to find the smallest number's index
int minimal(int a[], int n)
{
    int lo = 0, hi = n - 1;
 
    // Do a binary search
    while (lo < hi) {
 
        // Find the mid element
        int mid = (lo + hi) >> 1;
 
        // Check for break point
        if (a[mid] < a[mid + 1]) {
            hi = mid;
        }
        else {
            lo = mid + 1;
        }
    }
 
    // Return the index
    return lo;
}
 
// Driver Code
int main()
{
    int a[] = { 8, 5, 4, 3, 4, 10 };
    int n = sizeof(a) / sizeof(a[0]);
    int ind = minimal(a, n);
 
    // Print the smallest number
    cout << a[ind];
}

Java




// Java program to find the smallest number
// in an array of decrease and increasing numbers
class Solution
{
// Function to find the smallest number's index
static int minimal(int a[], int n)
{
    int lo = 0, hi = n - 1;
 
    // Do a binary search
    while (lo < hi) {
 
        // Find the mid element
        int mid = (lo + hi) >> 1;
 
        // Check for break point
        if (a[mid] < a[mid + 1]) {
            hi = mid;
        }
        else {
            lo = mid + 1;
        }
    }
 
    // Return the index
    return lo;
}
 
// Driver Code
public static void main(String args[])
{
    int a[] = { 8, 5, 4, 3, 4, 10 };
    int n = a.length;
    int ind = minimal(a, n);
 
    // Print the smallest number
    System.out.println( a[ind]);
}
}
//contributed by Arnab Kundu

Python3




# Python 3 program to find the smallest
# number in a array of decrease and
# increasing numbers
 
# function to find the smallest
# number's index
def minimal(a, n):
     
    lo, hi = 0, n - 1
     
    # Do a binary search
    while lo < hi:
         
        # find the mid element
        mid = (lo + hi) // 2
         
        # Check for break point
        if a[mid] < a[mid + 1]:
            hi = mid
        else:
            lo = mid + 1
        return lo
     
# Driver code
a = [8, 5, 4, 3, 4, 10]
 
n = len(a)
 
ind = minimal(a, n)
 
# print the smallest number
print(a[ind])
 
# This code is contributed
# by Mohit Kumar

C#




// C# program to find the smallest number
// in an array of decrease and increasing numbers
using System;
class Solution
{
// Function to find the smallest number's index
static int minimal(int[] a, int n)
{
    int lo = 0, hi = n - 1;
 
    // Do a binary search
    while (lo < hi) {
 
        // Find the mid element
        int mid = (lo + hi) >> 1;
 
        // Check for break point
        if (a[mid] < a[mid + 1]) {
            hi = mid;
        }
        else {
            lo = mid + 1;
        }
    }
 
    // Return the index
    return lo;
}
 
// Driver Code
public static void Main()
{
    int[] a = { 8, 5, 4, 3, 4, 10 };
    int n = a.Length;
    int ind = minimal(a, n);
 
    // Print the smallest number
    Console.WriteLine( a[ind]);
}
}
//contributed by Mukul singh

PHP




<?php
// PHP program to find the smallest number
// in an array of decrease and increasing numbers
 
// Function to find the smallest
// number's index
function minimal($a, $n)
{
    $lo = 0;
    $hi = $n - 1;
 
    // Do a binary search
    while ($lo < $hi)
    {
 
        // Find the mid element
        $mid = ($lo + $hi) >> 1;
 
        // Check for break point
        if ($a[$mid] < $a[$mid + 1])
        {
            $hi = $mid;
        }
        else
        {
            $lo = $mid + 1;
        }
    }
 
    // Return the index
    return $lo;
}
 
// Driver Code
$a = array( 8, 5, 4, 3, 4, 10 );
$n = sizeof($a);
$ind = minimal($a, $n);
 
// Print the smallest number
echo $a[$ind];
 
// This code is contributed
// by Sach_Code
?>

Javascript




<script>
 
    // Javascript program to find the smallest number
    // in an array of decrease and increasing numbers
     
    // Function to find the smallest number's index
    function minimal(a, n)
    {
        let lo = 0, hi = n - 1;
       
        // Do a binary search
        while (lo < hi) {
       
            // Find the mid element
            let mid = (lo + hi) >> 1;
       
            // Check for break point
            if (a[mid] < a[mid + 1]) {
                hi = mid;
            }
            else {
                lo = mid + 1;
            }
        }
       
        // Return the index
        return lo;
    }
     
    let a = [ 8, 5, 4, 3, 4, 10 ];
    let n = a.length;
    let ind = minimal(a, n);
   
    // Print the smallest number
    document.write(a[ind]);
     
</script>

Output: 

3

 

Time Complexity: O(Log N), as we are using binary search, in binary search in each traversal we reduce by division of 2 so the effective time will be 1+1/2+1/4+… which is equivalent to logN.
Auxiliary Space: O(1), as we are not using any extra space.
 


My Personal Notes arrow_drop_up
Recommended Articles
Page :

Start Your Coding Journey Now!