Minimum in an array which is first decreasing then increasing
Last Updated :
11 Jul, 2025
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.
C++
// CPP program to find minimum element
// in an array.
#include <bits/stdc++.h>
using namespace std;
int minimal(int arr[], int n)
{
int ans = arr[0];
for (int i = 1; i < n; i++)
ans = min(ans, arr[i]);
return ans;
}
int main()
{
int arr[] = { 8, 5, 4, 3, 4, 10 };
int n = sizeof(arr) / sizeof(arr[0]);
cout << minimal(arr, n);
return 0;
}
// This code is contributed by Vishal Dhaygude
Java
import java.util.*;
public class Main {
public static int minimal(int[] arr, int n) {
int ans = arr[0];
for (int i = 1; i < n; i++) {
ans = Math.min(ans, arr[i]);
}
return ans;
}
public static void main(String[] args) {
int[] arr = { 8, 5, 4, 3, 4, 10 };
int n = arr.length;
System.out.println(minimal(arr, n));
}
}
Python
#python code
def minimal(arr, n):
ans = arr[0]
for i in range(1, n):
ans = min(ans, arr[i])
return ans
arr = [8, 5, 4, 3, 4, 10]
n = len(arr)
print(minimal(arr, n))
C#
// C# program to find minimum element
// in an array.
using System;
public class Program {
public static int Minimal(int[] arr, int n)
{
int ans = arr[0];
for (int i = 1; i < n; i++)
ans = Math.Min(ans, arr[i]);
return ans;
}
public static void Main()
{
int[] arr = { 8, 5, 4, 3, 4, 10 };
int n = arr.Length;
Console.WriteLine(Minimal(arr, n));
}
}
// This code is contributed by user_dtewbxkn77n
JavaScript
// JavaScript program to find minimum element
// in an array.
function minimal(arr, n) {
let ans = arr[0];
for (let i = 1; i < n; i++) {
ans = Math.min(ans, arr[i]);
}
return ans;
}
let arr = [8, 5, 4, 3, 4, 10];
let n = arr.length;
console.log(minimal(arr, n));
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
Python
# 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
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>
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
?>
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.
Method 3(Using Stack) :
1.Create an empty stack to hold the indices of the array elements.
2.Traverse the array from left to right until we find the minimum element. Push the index of each element onto the
stack as long as the element is greater than or equal to the previous element.
3.Once we find an element that is lesser than the previous element, we know that the minimum element has been
reached. We can then pop all the indices from the 4.stack until we find an index whose corresponding element
is less than the current element.
4.The minimum element is the element corresponding to the last index remaining on the stack.
Implementation of the above code :
C++
#include <bits/stdc++.h>
using namespace std;
int findMin(int arr[], int n)
{
stack<int> s;
int min = 0;
// traverse the array from left to right
for (int i = 0; i < n; i++) {
// push the index onto the stack if the element is
// greater than or equal to the previous element
if (s.empty() || arr[i] >= arr[s.top()]) {
s.push(i);
}
else {
// pop all the indices from the stack until we
// find an index whose corresponding element is
// less than the current element
while (!s.empty() && arr[i] < arr[s.top()]) {
int index = s.top();
s.pop();
// update the minimum element
if (arr[index] < arr[min]) {
min = index;
}
}
// push the current index onto the stack
s.push(i);
}
}
// the minimum element is the element corresponding to
// the last index remaining on the stack
while (!s.empty()) {
int index = s.top();
s.pop();
if (arr[index] < arr[min]) {
min = index;
}
}
return arr[min];
}
int main()
{
int arr[] = { 50, 20, 3, 1, 6, 7, 10, 56 };
int n = sizeof(arr) / sizeof(arr[0]);
cout << "The minimum element is " << findMin(arr, n);
return 0;
}
Java
import java.util.*;
public class Main {
public static int findMin(int[] arr, int n) {
Stack<Integer> s = new Stack<>();
int min = 0;
// traverse the array from left to right
for (int i = 0; i < n; i++) {
// push the index onto the stack if the element is
// greater than or equal to the previous element
if (s.empty() || arr[i] >= arr[s.peek()]) {
s.push(i);
} else {
// pop all the indices from the stack until we
// find an index whose corresponding element is
// less than the current element
while (!s.empty() && arr[i] < arr[s.peek()]) {
int index = s.peek();
s.pop();
// update the minimum element
if (arr[index] < arr[min]) {
min = index;
}
}
// push the current index onto the stack
s.push(i);
}
}
// the minimum element is the element corresponding to
// the last index remaining on the stack
while (!s.empty()) {
int index = s.peek();
s.pop();
if (arr[index] < arr[min]) {
min = index;
}
}
return arr[min];
}
public static void main(String[] args) {
int[] arr = { 50, 20, 3, 1, 6, 7, 10, 56 };
int n = arr.length;
System.out.println("The minimum element is " + findMin(arr, n));
}
}
Python
# code
from typing import List
import sys
def findMin(arr: List[int], n: int) -> int:
s = []
min_index = 0
# traverse the array from left to right
for i in range(n):
# push the index onto the stack if the element is
# greater than or equal to the previous element
if not s or arr[i] >= arr[s[-1]]:
s.append(i)
else:
# pop all the indices from the stack until we
# find an index whose corresponding element is
# less than the current element
while s and arr[i] < arr[s[-1]]:
index = s.pop()
# update the minimum element
if arr[index] < arr[min_index]:
min_index = index
# push the current index onto the stack
s.append(i)
# the minimum element is the element corresponding to
# the last index remaining on the stack
while s:
index = s.pop()
if arr[index] < arr[min_index]:
min_index = index
return arr[min_index]
# Driver code
arr = [50, 20, 3, 1, 6, 7, 10, 56]
n = len(arr)
print("The minimum element is", findMin(arr, n))
C#
using System;
using System.Collections.Generic;
public class GFG{
static int findMin(int[] arr, int n)
{
Stack<int> s = new Stack<int>();
int min = 0;
// traverse the array from left to right
for (int i = 0; i < n; i++) {
// push the index onto the stack if the element is
// greater than or equal to the previous element
if (s.Count == 0 || arr[i] >= arr[s.Peek()]){
s.Push(i);
}
else {
// pop all the indices from the stack until we
// find an index whose corresponding element is
// less than the current element
while (s.Count > 0 && arr[i] < arr[s.Peek()]){
int index = s.Pop();
// update the minimum element
if (arr[index] < arr[min]) {
min = index;
}
}
// push the current index onto the stack
s.Push(i);
}
}
// the minimum element is the element corresponding to
// the last index remaining on the stack
while (s.Count > 0){
int index = s.Pop();
if (arr[index] < arr[min]) {
min = index;
}
}
return arr[min];
}
static void Main(string[] args)
{
int[] arr = { 50, 20, 3, 1, 6, 7, 10, 56 };
int n = arr.Length;
Console.WriteLine("The minimum element is " + findMin(arr, n));
}
}
JavaScript
function findMin(arr, n) {
const s = [];
let min_index = 0;
// Traverse the array from left to right
for (let i = 0; i < n; i++) {
// Push the index onto the stack if the element is
// greater than or equal to the previous element
if (!s.length || arr[i] >= arr[s[s.length - 1]]) {
s.push(i);
} else {
// Pop all the indices from the stack until we
// find an index whose corresponding element is
// less than the current element
while (s.length && arr[i] < arr[s[s.length - 1]]) {
const index = s.pop();
// Update the minimum element
if (arr[index] < arr[min_index]) {
min_index = index;
}
}
// Push the current index onto the stack
s.push(i);
}
}
// The minimum element is the element corresponding to
// the last index remaining on the stack
while (s.length) {
const index = s.pop();
if (arr[index] < arr[min_index]) {
min_index = index;
}
}
return arr[min_index];
}
// Driver code
const arr = [50, 20, 3, 1, 6, 7, 10, 56];
const n = arr.length;
console.log("The minimum element is", findMin(arr, n));
// This code is Contributed By - Dwaipayan Bandyopadhyay
OutputThe maximum element is 1
Time Complexity : O(N)
Auxiliary Space : O(N)
Similar Reads
Basics & Prerequisites
Data Structures
Array Data StructureIn this article, we introduce array, implementation in different popular languages, its basic operations and commonly seen problems / interview questions. An array stores items (in case of C/C++ and Java Primitive Arrays) or their references (in case of Python, JS, Java Non-Primitive) at contiguous
3 min read
String in Data StructureA string is a sequence of characters. The following facts make string an interesting data structure.Small set of elements. Unlike normal array, strings typically have smaller set of items. For example, lowercase English alphabet has only 26 characters. ASCII has only 256 characters.Strings are immut
2 min read
Hashing in Data StructureHashing is a technique used in data structures that efficiently stores and retrieves data in a way that allows for quick access. Hashing involves mapping data to a specific index in a hash table (an array of items) using a hash function. It enables fast retrieval of information based on its key. The
2 min read
Linked List Data StructureA linked list is a fundamental data structure in computer science. It mainly allows efficient insertion and deletion operations compared to arrays. Like arrays, it is also used to implement other data structures like stack, queue and deque. Hereâs the comparison of Linked List vs Arrays Linked List:
2 min read
Stack Data StructureA Stack is a linear data structure that follows a particular order in which the operations are performed. The order may be LIFO(Last In First Out) or FILO(First In Last Out). LIFO implies that the element that is inserted last, comes out first and FILO implies that the element that is inserted first
2 min read
Queue Data StructureA Queue Data Structure is a fundamental concept in computer science used for storing and managing data in a specific order. It follows the principle of "First in, First out" (FIFO), where the first element added to the queue is the first one to be removed. It is used as a buffer in computer systems
2 min read
Tree Data StructureTree Data Structure is a non-linear data structure in which a collection of elements known as nodes are connected to each other via edges such that there exists exactly one path between any two nodes. Types of TreeBinary Tree : Every node has at most two childrenTernary Tree : Every node has at most
4 min read
Graph Data StructureGraph Data Structure is a collection of nodes connected by edges. It's used to represent relationships between different entities. If you are looking for topic-wise list of problems on different topics like DFS, BFS, Topological Sort, Shortest Path, etc., please refer to Graph Algorithms. Basics of
3 min read
Trie Data StructureThe Trie data structure is a tree-like structure used for storing a dynamic set of strings. It allows for efficient retrieval and storage of keys, making it highly effective in handling large datasets. Trie supports operations such as insertion, search, deletion of keys, and prefix searches. In this
15+ min read
Algorithms
Searching AlgorithmsSearching algorithms are essential tools in computer science used to locate specific items within a collection of data. In this tutorial, we are mainly going to focus upon searching in an array. When we search an item in an array, there are two most common algorithms used based on the type of input
2 min read
Sorting AlgorithmsA Sorting Algorithm is used to rearrange a given array or list of elements in an order. For example, a given array [10, 20, 5, 2] becomes [2, 5, 10, 20] after sorting in increasing order and becomes [20, 10, 5, 2] after sorting in decreasing order. There exist different sorting algorithms for differ
3 min read
Introduction to RecursionThe process in which a function calls itself directly or indirectly is called recursion and the corresponding function is called a recursive function. A recursive algorithm takes one step toward solution and then recursively call itself to further move. The algorithm stops once we reach the solution
14 min read
Greedy AlgorithmsGreedy algorithms are a class of algorithms that make locally optimal choices at each step with the hope of finding a global optimum solution. At every step of the algorithm, we make a choice that looks the best at the moment. To make the choice, we sometimes sort the array so that we can always get
3 min read
Graph AlgorithmsGraph is a non-linear data structure like tree data structure. The limitation of tree is, it can only represent hierarchical data. For situations where nodes or vertices are randomly connected with each other other, we use Graph. Example situations where we use graph data structure are, a social net
3 min read
Dynamic Programming or DPDynamic Programming is an algorithmic technique with the following properties.It is mainly an optimization over plain recursion. Wherever we see a recursive solution that has repeated calls for the same inputs, we can optimize it using Dynamic Programming. The idea is to simply store the results of
3 min read
Bitwise AlgorithmsBitwise algorithms in Data Structures and Algorithms (DSA) involve manipulating individual bits of binary representations of numbers to perform operations efficiently. These algorithms utilize bitwise operators like AND, OR, XOR, NOT, Left Shift, and Right Shift.BasicsIntroduction to Bitwise Algorit
4 min read
Advanced
Segment TreeSegment Tree is a data structure that allows efficient querying and updating of intervals or segments of an array. It is particularly useful for problems involving range queries, such as finding the sum, minimum, maximum, or any other operation over a specific range of elements in an array. The tree
3 min read
Pattern SearchingPattern searching algorithms are essential tools in computer science and data processing. These algorithms are designed to efficiently find a particular pattern within a larger set of data. Patten SearchingImportant Pattern Searching Algorithms:Naive String Matching : A Simple Algorithm that works i
2 min read
GeometryGeometry is a branch of mathematics that studies the properties, measurements, and relationships of points, lines, angles, surfaces, and solids. From basic lines and angles to complex structures, it helps us understand the world around us.Geometry for Students and BeginnersThis section covers key br
2 min read
Interview Preparation
Practice Problem