Data Structure & Algorithms
DSA
Array carry forword
Closest Min Max

Closest MinMax

Problem Description

Given an array A, find the size of the smallest subarray such that it contains at least one occurrence of the maximum value of the array and at least one occurrence of the minimum value of the array.

Problem Constraints

1 <= |A| <= 2000

Input Format

First and only argument is vector A

Output Format

Return the length of the smallest subarray which has at least one occurrence of minimum and maximum element of the array

Example Input

Input 1:
A = [1, 3, 2]

Input 2:
A = [2, 6, 1, 6, 9]

Example Output

Output 1:
2

Output 2:
3

Example Explanation

Explanation 1:
Take the 1st and 2nd elements as they are the minimum and maximum elements respectievly.

Explanation 2:
Take the last 3 elements of the array.

Output

Java
import java.util.*;
 
public class ClosestMinMax {
    public static int closestMinMaxSize(ArrayList<Integer> A) {
        int n = A.size();
        int minIndex = -1, maxIndex = -1;
        int minMaxSize = Integer.MAX_VALUE;
 
        int min = Collections.min(A);
        int max = Collections.max(A);
 
        for (int i = 0; i < n; i++) {
            if (A.get(i) == min) {
                minIndex = i;
            }
            if (A.get(i) == max) {
                maxIndex = i;
            }
            if (minIndex != -1 && maxIndex != -1) {
                minMaxSize = Math.min(minMaxSize, Math.abs(maxIndex - minIndex) + 1);
            }
        }
        return minMaxSize;
    }
 
    public static void main(String[] args) {
        ArrayList<Integer> A1 = new ArrayList<>(Arrays.asList(1, 3, 2));
        ArrayList<Integer> A2 = new ArrayList<>(Arrays.asList(2, 6, 1, 6, 9));
 
        System.out.println("Output 1: " + closestMinMaxSize(A1));
        System.out.println("Output 2: " + closestMinMaxSize(A2));
    }
}
Python
def closest_min_max_size(A):
    min_index = -1
    max_index = -1
    min_max_size = float('inf')
 
    minimum = min(A)
    maximum = max(A)
 
    for i in range(len(A)):
        if A[i] == minimum:
            min_index = i
        if A[i] == maximum:
            max_index = i
        if min_index != -1 and max_index != -1:
            min_max_size = min(min_max_size, abs(max_index - min_index) + 1)
 
    return min_max_size
 
A1 = [1, 3, 2]
A2 = [2, 6, 1, 6, 9]
 
print("Output 1:", closest_min_max_size(A1))
print("Output 2:", closest_min_max_size(A2))
JavaScript
function closestMinMaxSize(A) {
    let minIndex = -1;
    let maxIndex = -1;
    let minMaxSize = Infinity;
 
    const min = Math.min(...A);
    const max = Math.max(...A);
 
    A.forEach((value, index) => {
        if (value === min) {
            minIndex = index;
        }
        if (value === max) {
            maxIndex = index;
        }
        if (minIndex !== -1 && maxIndex !== -1) {
            minMaxSize = Math.min(minMaxSize, Math.abs(maxIndex - minIndex) + 1);
        }
    });
 
    return minMaxSize;
}
 
const A1 = [1, 3, 2];
const A2 = [2, 6, 1, 6, 9];
 
console.log("Output 1:", closestMinMaxSize(A1));
console.log("Output 2:", closestMinMaxSize(A2));