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++
#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;
}
|
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));
}
}
|
Python3
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#
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));
}
}
|
Javascript
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++
#include <bits/stdc++.h>
using namespace std;
int minimal( int a[], int n)
{
int lo = 0, hi = n - 1;
while (lo < hi) {
int mid = (lo + hi) >> 1;
if (a[mid] < a[mid + 1]) {
hi = mid;
}
else {
lo = mid + 1;
}
}
return lo;
}
int main()
{
int a[] = { 8, 5, 4, 3, 4, 10 };
int n = sizeof (a) / sizeof (a[0]);
int ind = minimal(a, n);
cout << a[ind];
}
|
Java
class Solution
{
static int minimal( int a[], int n)
{
int lo = 0 , hi = n - 1 ;
while (lo < hi) {
int mid = (lo + hi) >> 1 ;
if (a[mid] < a[mid + 1 ]) {
hi = mid;
}
else {
lo = mid + 1 ;
}
}
return lo;
}
public static void main(String args[])
{
int a[] = { 8 , 5 , 4 , 3 , 4 , 10 };
int n = a.length;
int ind = minimal(a, n);
System.out.println( a[ind]);
}
}
|
Python3
def minimal(a, n):
lo, hi = 0 , n - 1
while lo < hi:
mid = (lo + hi) / / 2
if a[mid] < a[mid + 1 ]:
hi = mid
else :
lo = mid + 1
return lo
a = [ 8 , 5 , 4 , 3 , 4 , 10 ]
n = len (a)
ind = minimal(a, n)
print (a[ind])
|
C#
using System;
class Solution
{
static int minimal( int [] a, int n)
{
int lo = 0, hi = n - 1;
while (lo < hi) {
int mid = (lo + hi) >> 1;
if (a[mid] < a[mid + 1]) {
hi = mid;
}
else {
lo = mid + 1;
}
}
return lo;
}
public static void Main()
{
int [] a = { 8, 5, 4, 3, 4, 10 };
int n = a.Length;
int ind = minimal(a, n);
Console.WriteLine( a[ind]);
}
}
|
Javascript
<script>
function minimal(a, n)
{
let lo = 0, hi = n - 1;
while (lo < hi) {
let mid = (lo + hi) >> 1;
if (a[mid] < a[mid + 1]) {
hi = mid;
}
else {
lo = mid + 1;
}
}
return lo;
}
let a = [ 8, 5, 4, 3, 4, 10 ];
let n = a.length;
let ind = minimal(a, n);
document.write(a[ind]);
</script>
|
PHP
<?php
function minimal( $a , $n )
{
$lo = 0;
$hi = $n - 1;
while ( $lo < $hi )
{
$mid = ( $lo + $hi ) >> 1;
if ( $a [ $mid ] < $a [ $mid + 1])
{
$hi = $mid ;
}
else
{
$lo = $mid + 1;
}
}
return $lo ;
}
$a = array ( 8, 5, 4, 3, 4, 10 );
$n = sizeof( $a );
$ind = minimal( $a , $n );
echo $a [ $ind ];
?>
|
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;
for ( int i = 0; i < n; i++) {
if (s.empty() || arr[i] >= arr[s.top()]) {
s.push(i);
}
else {
while (!s.empty() && arr[i] < arr[s.top()]) {
int index = s.top();
s.pop();
if (arr[index] < arr[min]) {
min = index;
}
}
s.push(i);
}
}
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 ;
for ( int i = 0 ; i < n; i++) {
if (s.empty() || arr[i] >= arr[s.peek()]) {
s.push(i);
} else {
while (!s.empty() && arr[i] < arr[s.peek()]) {
int index = s.peek();
s.pop();
if (arr[index] < arr[min]) {
min = index;
}
}
s.push(i);
}
}
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));
}
}
|
Python3
from typing import List
import sys
def findMin(arr: List [ int ], n: int ) - > int :
s = []
min_index = 0
for i in range (n):
if not s or arr[i] > = arr[s[ - 1 ]]:
s.append(i)
else :
while s and arr[i] < arr[s[ - 1 ]]:
index = s.pop()
if arr[index] < arr[min_index]:
min_index = index
s.append(i)
while s:
index = s.pop()
if arr[index] < arr[min_index]:
min_index = index
return arr[min_index]
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;
for ( int i = 0; i < n; i++) {
if (s.Count == 0 || arr[i] >= arr[s.Peek()]){
s.Push(i);
}
else {
while (s.Count > 0 && arr[i] < arr[s.Peek()]){
int index = s.Pop();
if (arr[index] < arr[min]) {
min = index;
}
}
s.Push(i);
}
}
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;
for (let i = 0; i < n; i++) {
if (!s.length || arr[i] >= arr[s[s.length - 1]]) {
s.push(i);
} else {
while (s.length && arr[i] < arr[s[s.length - 1]]) {
const index = s.pop();
if (arr[index] < arr[min_index]) {
min_index = index;
}
}
s.push(i);
}
}
while (s.length) {
const index = s.pop();
if (arr[index] < arr[min_index]) {
min_index = index;
}
}
return arr[min_index];
}
const arr = [50, 20, 3, 1, 6, 7, 10, 56];
const n = arr.length;
console.log( "The minimum element is" , findMin(arr, n));
|
Output
The maximum element is 1
Time Complexity : O(N)
Auxiliary Space : O(N)
Feeling lost in the world of random DSA topics, wasting time without progress? It’s time for a change! Join our DSA course, where we’ll guide you on an exciting journey to master DSA efficiently and on schedule.
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 neveropen!