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.
// 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 |
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 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# 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 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)); |
3
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++ 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 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 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# 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 |
<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 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 ?> |
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.
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 :
#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;
} |
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));
} } |
# 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))
|
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));
}
} |
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 |
The maximum element is 1
Time Complexity : O(N)
Auxiliary Space : O(N)