Given an array **A[]** consisting of **N** elements, the task is to find the minimum distance between the minimum and the maximum element of the array.**Examples:**

Input:arr[] = {3, 2, 1, 2, 1, 4, 5, 8, 6, 7, 8, 2}Output:3Explanation:

The minimum element(= 1) is present at indices {2, 4}

The maximum element(= 8) is present at indices {7, 10}.

The minimum distance between an occurrence of 1 and 8 is 7 – 4 = 3Input:arr[] = {1, 3, 69}Output:2Explanation:

The minimum element(= 1) is present at index 0.

The maximum element(= 69) is present at index 2.

Therefore, the minimum distance between them is 2.

**Naive Approach: **

The simplest approach to solve this problem is as follows:

- Find the minimum and maximum element of the array.
- Traverse the array and for every occurrence of the maximum element, calculate its distance from all occurrences of the minimum element in the array and update the minimum distance.
- After complete traversal of the array, print all the minimum distance obtained.

**Time Complexity:** O(N^{2}) **Auxiliary Space:** O(1) **Efficient Approach:**

Follow the steps below to optimize the above approach:

- Traverse the array to find the minimum and maximum element.
- Initialize two variables
**min_index**and**max_index**to store the indices of the minimum and maximum elements of the array respectively. Initialize them with -1. - Traverse the array. If at any instant, both
**min_index**and**max_index**is not equal to -1, i.e. both of them have stored a valid index, calculate there a difference. - Compare this difference with the minimum distance(say,
**min_dist**) and update**min_dist**accordingly. - Finally, print the final value of
**min_dist**obtained after the complete traversal of the array.

Below is the implementation of the above approach:

## C++

`// C++ Program to implement the` `// above approach` `#include <bits/stdc++.h>` `using` `namespace` `std;` `// Function to find the minimum` `// distance between the minimum` `// and the maximum element` `int` `minDistance(` `int` `a[], ` `int` `n)` `{` ` ` `// Stores the minimum and maximum` ` ` `// array element` ` ` `int` `maximum = -1, minimum = INT_MAX;` ` ` `// Stores the most recently traversed` ` ` `// indices of the minimum and the` ` ` `// maximum element` ` ` `int` `min_index = -1, max_index = -1;` ` ` `// Stores the minimum distance` ` ` `// between the minimum and the` ` ` `// maximium` ` ` `int` `min_dist = n + 1;` ` ` `// Find the maximum and` ` ` `// the minimum element` ` ` `// from the given array` ` ` `for` `(` `int` `i = 0; i < n; i++) {` ` ` `if` `(a[i] > maximum)` ` ` `maximum = a[i];` ` ` `if` `(a[i] < minimum)` ` ` `minimum = a[i];` ` ` `}` ` ` `// Find the minimum distance` ` ` `for` `(` `int` `i = 0; i < n; i++) {` ` ` `// Check if current element` ` ` `// is equal to minimum` ` ` `if` `(a[i] == minimum)` ` ` `min_index = i;` ` ` `// Check if current element` ` ` `// is equal to maximum` ` ` `if` `(a[i] == maximum)` ` ` `max_index = i;` ` ` `// If both the minimum and the` ` ` `// maximum element has` ` ` `// occurred at least once` ` ` `if` `(min_index != -1` ` ` `&& max_index != -1)` ` ` `// Update the minimum distance` ` ` `min_dist` ` ` `= min(min_dist,` ` ` `abs` `(min_index` ` ` `- max_index));` ` ` `}` ` ` `// Return the answer` ` ` `return` `min_dist;` `}` `// Driver Code` `int` `main()` `{` ` ` `int` `a[] = { 3, 2, 1, 2, 1, 4,` ` ` `5, 8, 6, 7, 8, 2 };` ` ` `int` `n = ` `sizeof` `a / ` `sizeof` `a[0];` ` ` `cout << minDistance(a, n);` `}` |

*chevron_right*

*filter_none*

## Java

`// Java Program to implement` `// the above approach` `import` `java.util.*;` `class` `GFG {` ` ` `// Function to find the minimum` ` ` `// distance between the minimum` ` ` `// and the maximum element` ` ` `public` `static` `int` `minDistance(` `int` `a[], ` `int` `n)` ` ` `{` ` ` `// Stores the minimum and maximum` ` ` `// array element` ` ` `int` `max = -` `1` `, min = Integer.MAX_VALUE;` ` ` `// Stores the most recently traversed` ` ` `// indices of the minimum and the` ` ` `// maximum element` ` ` `int` `min_index = -` `1` `, max_index = -` `1` `;` ` ` `// Stores the minimum distance` ` ` `// between the minimum and the` ` ` `// maximium` ` ` `int` `min_dist = n + ` `1` `;` ` ` `// Find the maximum and` ` ` `// the minimum element` ` ` `// from the given array` ` ` `for` `(` `int` `i = ` `0` `; i < n; i++) {` ` ` `if` `(a[i] > max)` ` ` `max = a[i];` ` ` `if` `(a[i] < min)` ` ` `min = a[i];` ` ` `}` ` ` `// Find the minimum distance` ` ` `for` `(` `int` `i = ` `0` `; i < n; i++) {` ` ` `// Check if current element` ` ` `// is equal to minimum` ` ` `if` `(a[i] == min)` ` ` `min_index = i;` ` ` `// Check if current element` ` ` `// is equal to maximum` ` ` `if` `(a[i] == max)` ` ` `max_index = i;` ` ` `// If both the minimum and the` ` ` `// maximum element has` ` ` `// occurred at least once` ` ` `if` `(min_index != -` `1` ` ` `&& max_index != -` `1` `)` ` ` `min_dist` ` ` `= Math.min(min_dist,` ` ` `Math.abs(min_index` ` ` `- max_index));` ` ` `}` ` ` `return` `min_dist;` ` ` `}` ` ` `// Driver Code` ` ` `public` `static` `void` `main(String[] args)` ` ` `{` ` ` `int` `n = ` `12` `;` ` ` `int` `a[] = { ` `3` `, ` `2` `, ` `1` `, ` `2` `, ` `1` `, ` `4` `,` ` ` `5` `, ` `8` `, ` `6` `, ` `7` `, ` `8` `, ` `2` `};` ` ` `System.out.println(minDistance(a, n));` ` ` `}` `}` |

*chevron_right*

*filter_none*

## Python3

`# Python3 Program to implement the` `# above approach` `import` `sys` `# Function to find the minimum` `# distance between the minimum` `# and the maximum element` `def` `minDistance(a, n):` ` ` `# Stores the minimum and maximum` ` ` `# array element` ` ` `maximum ` `=` `-` `1` ` ` `minimum ` `=` `sys.maxsize` ` ` ` ` `# Stores the most recently traversed` ` ` `# indices of the minimum and the` ` ` `# maximum element` ` ` `min_index ` `=` `-` `1` ` ` `max_index ` `=` `-` `1` ` ` ` ` `# Stores the minimum distance` ` ` `# between the minimum and the` ` ` `# maximium` ` ` `min_dist ` `=` `n ` `+` `1` ` ` ` ` `# Find the maximum and` ` ` `# the minimum element` ` ` `# from the given array` ` ` `for` `i ` `in` `range` `(n):` ` ` `if` `(a[i] > maximum):` ` ` `maximum ` `=` `a[i]` ` ` `if` `(a[i] < minimum):` ` ` `minimum ` `=` `a[i]` ` ` ` ` `# Find the minimum distance` ` ` `for` `i ` `in` `range` `(n):` ` ` ` ` `# Check if current element` ` ` `# is equal to minimum` ` ` `if` `(a[i] ` `=` `=` `minimum):` ` ` `min_index ` `=` `i` ` ` ` ` `# Check if current element` ` ` `# is equal to maximum` ` ` `if` `(a[i] ` `=` `=` `maximum):` ` ` `max_index ` `=` `i` ` ` ` ` `# If both the minimum and the` ` ` `# maximum element has` ` ` `# occurred at least once` ` ` `if` `(min_index !` `=` `-` `1` `and` ` ` `max_index !` `=` `-` `1` `):` ` ` ` ` `# Update the minimum distance` ` ` `min_dist ` `=` `(` `min` `(min_dist,` ` ` `abs` `(min_index ` `-` ` ` `max_index)))` ` ` ` ` `# Return the answer` ` ` `return` `min_dist` ` ` `# Driver Code` `if` `__name__ ` `=` `=` `"__main__"` `:` ` ` `a ` `=` `[` `3` `, ` `2` `, ` `1` `, ` `2` `, ` `1` `, ` `4` `,` ` ` `5` `, ` `8` `, ` `6` `, ` `7` `, ` `8` `, ` `2` `]` ` ` `n ` `=` `len` `(a)` ` ` `print` `(minDistance(a, n))` `# This code is contributed by Chitranayal` |

*chevron_right*

*filter_none*

## C#

`// C# program to implement` `// the above approach` `using` `System;` `class` `GFG{` ` ` `// Function to find the minimum` `// distance between the minimum` `// and the maximum element` `static` `int` `minDistance(` `int` `[]a, ` `int` `n)` `{` ` ` ` ` `// Stores the minimum and maximum` ` ` `// array element` ` ` `int` `max = -1, min = Int32.MaxValue;` ` ` `// Stores the most recently traversed` ` ` `// indices of the minimum and the` ` ` `// maximum element` ` ` `int` `min_index = -1, max_index = -1;` ` ` `// Stores the minimum distance` ` ` `// between the minimum and the` ` ` `// maximium` ` ` `int` `min_dist = n + 1;` ` ` `// Find the maximum and` ` ` `// the minimum element` ` ` `// from the given array` ` ` `for` `(` `int` `i = 0; i < n; i++)` ` ` `{` ` ` `if` `(a[i] > max)` ` ` `max = a[i];` ` ` `if` `(a[i] < min)` ` ` `min = a[i];` ` ` `}` ` ` `// Find the minimum distance` ` ` `for` `(` `int` `i = 0; i < n; i++)` ` ` `{` ` ` `// Check if current element` ` ` `// is equal to minimum` ` ` `if` `(a[i] == min)` ` ` `min_index = i;` ` ` `// Check if current element` ` ` `// is equal to maximum` ` ` `if` `(a[i] == max)` ` ` `max_index = i;` ` ` `// If both the minimum and the` ` ` `// maximum element has` ` ` `// occurred at least once` ` ` `if` `(min_index != -1 && max_index != -1)` ` ` `min_dist = Math.Min(min_dist,` ` ` `Math.Abs(` ` ` `min_index - ` ` ` `max_index));` ` ` `}` ` ` `return` `min_dist;` `}` `// Driver Code` `public` `static` `void` `Main()` `{` ` ` `int` `n = 12;` ` ` `int` `[]a = { 3, 2, 1, 2, 1, 4,` ` ` `5, 8, 6, 7, 8, 2 };` ` ` ` ` `Console.WriteLine(minDistance(a, n));` `}` `}` `// This code is contributed by piyush3010` |

*chevron_right*

*filter_none*

**Output:**

3

**Time Complexity:** O(N) **Auxiliary Space:** O(1)

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.

## Recommended Posts:

- Distance of chord from center when distance between center and another equal length chord is given
- Minimum distance between any most frequent and least frequent element of an array
- Maximum distance between two occurrences of same element in array
- Maximum element in an array such that its previous and next element product is maximum
- Maximum distance between two 1s in a Binary Array in a given range
- Maximize minimum distance between repetitions from any permutation of the given Array
- Minimum distance between any special pair in the given array
- Minimum distance between two occurrences of maximum
- Find Array formed by adding each element of given array with largest element in new array to its left
- Sum of (maximum element - minimum element) for all the subsets of an array.
- Maximum XOR value of maximum and second maximum element among all possible subarrays
- Find maximum distance between any city and station
- Minimum and Maximum element of an array which is divisible by a given number k
- Find the radii of the circles which are lined in a row, and distance between the centers of first and last circle is given
- Find the side of the squares which are lined in a row, and distance between the centers of first and last square is given
- Binary Array Range Queries to find the minimum distance between two Zeros
- Minimum distance between any two equal elements in an Array
- Pair with given sum and maximum shortest distance from end
- Sum and Product of minimum and maximum element of an Array
- Leftmost and rightmost indices of the maximum and the minimum element of an array

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.