Smallest subarray containing minimum and maximum values
Last Updated :
12 Sep, 2022
Given an array A of size N. The task is to find the length of smallest subarray which contains both maximum and minimum values.
Examples:
Input : A[] = {1, 5, 9, 7, 1, 9, 4}
Output : 2
subarray {1, 9} has both maximum and minimum value.
Input : A[] = {2, 2, 2, 2}
Output : 1
2 is both maximum and minimum here.
Approach: The idea is to use two-pointer technique here :
- Find the maximum and minimum values of the array.
- Traverse through the array and store the last occurrences of maximum and minimum values.
- If the of last occurrence of maximum is pos_max and minimum is pos_min, then the minimum value of abs(pos_min – pos_max) + 1 is our required answer.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
int minSubarray( int A[], int n)
{
int minValue = *min_element(A, A + n);
int maxValue = *max_element(A, A + n);
int pos_min = -1, pos_max = -1, ans = INT_MAX;
for ( int i = 0; i < n; i++) {
if (A[i] == minValue)
pos_min = i;
if (A[i] == maxValue)
pos_max = i;
if (pos_max != -1 and pos_min != -1)
ans = min(ans, abs (pos_min - pos_max) + 1);
}
return ans;
}
int main()
{
int A[] = { 1, 5, 9, 7, 1, 9, 4 };
int n = sizeof (A) / sizeof (A[0]);
cout << minSubarray(A, n);
return 0;
}
|
Java
import java.util.*;
class GFG
{
static int minSubarray( int A[], int n)
{
int minValue = A[ 0 ];
for ( int i = 1 ; i < n; i++)
{
if (A[i] < minValue)
minValue = A[i];
}
int maxValue = A[ 0 ];
for ( int i = 1 ; i < n; i++)
{
if (A[i] > maxValue)
maxValue = A[i];
}
int pos_min = - 1 , pos_max = - 1 ,
ans = Integer.MAX_VALUE;
for ( int i = 0 ; i < n; i++)
{
if (A[i] == minValue)
pos_min = i;
if (A[i] == maxValue)
pos_max = i;
if (pos_max != - 1 && pos_min != - 1 )
ans = Math.min(ans,
Math.abs(pos_min - pos_max) + 1 );
}
return ans;
}
public static void main(String args[])
{
int A[] = { 1 , 5 , 9 , 7 , 1 , 9 , 4 };
int n = A.length;
System.out.println(minSubarray(A, n));
}
}
|
Python3
import sys
def minSubarray(A, n):
minValue = min (A)
maxValue = max (A)
pos_min, pos_max, ans = - 1 , - 1 , sys.maxsize
for i in range ( 0 , n):
if A[i] = = minValue:
pos_min = i
if A[i] = = maxValue:
pos_max = i
if pos_max ! = - 1 and pos_min ! = - 1 :
ans = min (ans, abs (pos_min - pos_max) + 1 )
return ans
A = [ 1 , 5 , 9 , 7 , 1 , 9 , 4 ]
n = len (A)
print (minSubarray(A, n))
|
C#
using System;
using System.Linq;
public class GFG{
static int minSubarray( int []A, int n)
{
int minValue = A.Min();
int maxValue = A.Max();
int pos_min = -1, pos_max = -1, ans = int .MaxValue;
for ( int i = 0; i < n; i++) {
if (A[i] == minValue)
pos_min = i;
if (A[i] == maxValue)
pos_max = i;
if (pos_max != -1 && pos_min != -1)
ans = Math.Min(ans, Math.Abs(pos_min - pos_max) + 1);
}
return ans;
}
static public void Main (){
int []A = { 1, 5, 9, 7, 1, 9, 4 };
int n = A.Length;
Console.WriteLine(minSubarray(A, n));
}
}
|
PHP
<?php
function minSubarray( $A , $n )
{
$minValue = min( $A );
$maxValue = max( $A );
$pos_min = -1;
$pos_max = -1;
$ans = PHP_INT_MAX;
for ( $i = 0; $i < $n ; $i ++)
{
if ( $A [ $i ] == $minValue )
$pos_min = $i ;
if ( $A [ $i ] == $maxValue )
$pos_max = $i ;
if ( $pos_max != -1 and $pos_min != -1)
$ans = min( $ans , abs ( $pos_min -
$pos_max ) + 1);
}
return $ans ;
}
$A = array (1, 5, 9, 7, 1, 9, 4);
$n = sizeof( $A );
echo minSubarray( $A , $n );
?>
|
Javascript
<script>
function minSubarray(A, n)
{
let minValue = Number.MAX_VALUE;
let maxValue = Number.MIN_VALUE;
for (let i = 0; i < n; i++)
{
minValue = Math.min(minValue, A[i]);
maxValue = Math.max(maxValue, A[i]);
}
let pos_min = -1, pos_max = -1, ans = Number.MAX_VALUE;
for (let i = 0; i < n; i++) {
if (A[i] == minValue)
pos_min = i;
if (A[i] == maxValue)
pos_max = i;
if (pos_max != -1 && pos_min != -1)
ans = Math.min(ans, Math.abs(pos_min - pos_max) + 1);
}
return ans;
}
let A = [ 1, 5, 9, 7, 1, 9, 4 ];
let n = A.length;
document.write(minSubarray(A, n));
</script>
|
Complexity Analysis:
- Time Complexity: O(n)
- Auxiliary Space: O(1)
Share your thoughts in the comments
Please Login to comment...