# Smallest subarray containing minimum and maximum values

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.
```

## Recommended: Please try your approach on {IDE} first, before moving on to the solution.

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++ implementation of above approach ` ` `  `#include ` `using` `namespace` `std; ` ` `  `// Function to return length of ` `// smallest subarray containing both ` `// maximum and minimum value ` `int` `minSubarray(``int` `A[], ``int` `n) ` `{ ` ` `  `    ``// find maximum and minimum ` `    ``// values in the array ` `    ``int` `minValue = *min_element(A, A + n); ` `    ``int` `maxValue = *max_element(A, A + n); ` ` `  `    ``int` `pos_min = -1, pos_max = -1, ans = INT_MAX; ` ` `  `    ``// iterate over the array and set answer ` `    ``// to smallest difference between position ` `    ``// of maximum and position of minimum value ` `    ``for` `(``int` `i = 0; i < n; i++) { ` ` `  `        ``// last occurrence of minValue ` `        ``if` `(A[i] == minValue) ` `            ``pos_min = i; ` ` `  `        ``// last occurrence of maxValue ` `        ``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; ` `} ` ` `  `// Driver code ` `int` `main() ` `{ ` `    ``int` `A[] = { 1, 5, 9, 7, 1, 9, 4 }; ` `    ``int` `n = ``sizeof``(A) / ``sizeof``(A); ` ` `  `    ``cout << minSubarray(A, n); ` ` `  `    ``return` `0; ` `} `

 `// Java implementation of above approach ` `import` `java.util.*; ` ` `  `class` `GFG ` `{ ` ` `  `// Function to return length of ` `// smallest subarray containing both ` `// maximum and minimum value ` `static` `int` `minSubarray(``int` `A[], ``int` `n) ` `{ ` ` `  `    ``// find maximum and minimum ` `    ``// values in the array ` `    ``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; ` ` `  `    ``// iterate over the array and set answer ` `    ``// to smallest difference between position ` `    ``// of maximum and position of minimum value ` `    ``for` `(``int` `i = ``0``; i < n; i++)  ` `    ``{ ` ` `  `        ``// last occurrence of minValue ` `        ``if` `(A[i] == minValue) ` `            ``pos_min = i; ` ` `  `        ``// last occurrence of maxValue ` `        ``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; ` `} ` ` `  `// Driver code ` `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)); ` `} ` `} ` ` `  `// This code is contributed by ` `// Surendra_Gangwar `

 `# Python3 implementation of above approach ` `import` `sys ` ` `  `# Function to return length of smallest  ` `# subarray containing both maximum and  ` `# minimum value ` `def` `minSubarray(A, n): ` ` `  `    ``# find maximum and minimum ` `    ``# values in the array ` `    ``minValue ``=` `min``(A) ` `    ``maxValue ``=` `max``(A) ` ` `  `    ``pos_min, pos_max, ans ``=` `-``1``, ``-``1``, sys.maxsize ` ` `  `    ``# iterate over the array and set answer ` `    ``# to smallest difference between position ` `    ``# of maximum and position of minimum value ` `    ``for` `i ``in` `range``(``0``, n): ` `         `  `        ``# last occurrence of minValue ` `        ``if` `A[i] ``=``=` `minValue: ` `            ``pos_min ``=` `i ` ` `  `        ``# last occurrence of maxValue ` `        ``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 ` ` `  `# Driver code ` `A ``=` `[ ``1``, ``5``, ``9``, ``7``, ``1``, ``9``, ``4` `] ` `n ``=` `len``(A) ` ` `  `print``(minSubarray(A, n)) ` ` `  `# This code is contributed ` `# by Saurabh_Shukla `

 `// C# implementation of above approach ` `using` `System; ` `using` `System.Linq; ` ` `  `public` `class` `GFG{ ` `     `  ` `  ` `  `// Function to return length of ` `// smallest subarray containing both ` `// maximum and minimum value ` `static` `int` `minSubarray(``int` `[]A, ``int` `n) ` `{ ` ` `  `    ``// find maximum and minimum ` `    ``// values in the array ` `    ``int` `minValue = A.Min(); ` `    ``int` `maxValue = A.Max(); ` ` `  `    ``int` `pos_min = -1, pos_max = -1, ans = ``int``.MaxValue; ` ` `  `    ``// iterate over the array and set answer ` `    ``// to smallest difference between position ` `    ``// of maximum and position of minimum value ` `    ``for` `(``int` `i = 0; i < n; i++) { ` ` `  `        ``// last occurrence of minValue ` `        ``if` `(A[i] == minValue) ` `            ``pos_min = i; ` ` `  `        ``// last occurrence of maxValue ` `        ``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; ` `} ` ` `  `// Driver code ` ` `  ` `  `    ``static` `public` `void` `Main (){ ` `            ``int` `[]A = { 1, 5, 9, 7, 1, 9, 4 }; ` `    ``int` `n = A.Length; ` ` `  `    ``Console.WriteLine(minSubarray(A, n)); ` `    ``} ` `} ` `// This code is contributed by anuj_67.. `

 ` `

Output:
```2
```

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.

Article Tags :