# Minimum distance between two occurrences of maximum

You are given an array of n-elements with a basic condition that occurrence of greatest element is more than once. You have to find the minimum distance between maximums. (n>=2).

Examples:

```Input : arr[] = {3, 5, 2, 3, 5, 3, 5}
Output : Minimum Distance = 2
Explanation : Greatest element is 5 and its index
are 1, 4 and 6. Resulting minimum distance of 2
from position 4 to 6.

Input : arr[] = {1, 1, 1, 1, 1, 1}
Output : Minimum Distance = 1
Explanation : Greatest element is 1 and its index
are 0, 1, 2, 3, 4 and 5. Resulting minimum distance
of 1.
```

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

A basic approach runs in O(n2). First we find the maximum element. Then for each element equal to maximum element, we find the nearest maximum element.
An efficient solution finish our job in a single traversing of array. We initialize maximum_element = arr, min_distance = n and index = 0. After that for each element we should check whether element is equal to, greater or less than maximum element. Depending upon three cases we have following option:

• case a: If element is equal to maximum_element then update min_dis = min( min_dis , (i-index)) and update index = i;
• case b: If element is greater than maximum_element update maximum_element = arr[i], index = i and min_dis = n.
• case c: If element is less than maximum_element then iterate to next element.
 `// C program to find Min distance  ` `// of maximum element ` `#include ` `using` `namespace` `std; ` ` `  `//function to return min distance ` `int` `minDistance (``int` `arr[], ``int` `n) ` `{ ` `    ``int` `maximum_element = arr; ` `    ``int` `min_dis = n; ` `    ``int` `index = 0; ` ` `  `    ``for` `(``int` `i=1; i

 `// Java program to find Min distance  ` `// of maximum element ` `class` `GFG  ` `{ ` `     `  `    ``// function to return min distance ` `    ``static` `int` `minDistance (``int` `arr[], ``int` `n) ` `    ``{ ` `        ``int` `maximum_element = arr[``0``]; ` `        ``int` `min_dis = n; ` `        ``int` `index = ``0``; ` ` `  `        ``for` `(``int` `i=``1``; i

 `# Python3 program to find Min  ` `# distance of maximum element ` ` `  `# Function to return min distance ` `def` `minDistance (arr, n): ` ` `  `    ``maximum_element ``=` `arr[``0``] ` `    ``min_dis ``=` `n ` `    ``index ``=` `0` ` `  `    ``for` `i ``in` `range``(``1``, n): ` `     `  `        ``# case a ` `        ``if` `(maximum_element ``=``=` `arr[i]): ` `         `  `            ``min_dis ``=` `min``(min_dis, (i ``-` `index)) ` `            ``index ``=` `i ` `         `  ` `  `        ``# case b ` `        ``elif` `(maximum_element < arr[i]): ` `         `  `            ``maximum_element ``=` `arr[i] ` `            ``min_dis ``=` `n ` `            ``index ``=` `i ` `         `  ` `  `        ``# case c ` `        ``else``: ` `            ``continue` `     `  ` `  `    ``return` `min_dis ` ` `  ` `  `# driver program ` `arr ``=` `[``6``, ``3``, ``1``, ``3``, ``6``, ``4``, ``6``] ` `n ``=` `len``(arr) ` `print``(``"Minimum distance ="``, minDistance(arr, n)) ` `  `  `# This code is contributed by Smitha Dinesh Semwal `

 `// C# program to find Min distance  ` `// of maximum element ` `using` `System; ` ` `  `class` `GFG { ` `     `  `    ``// function to return min distance ` `    ``static` `int` `minDistance (``int` `[]arr, ``int` `n) ` `    ``{ ` `        ``int` `maximum_element = arr; ` `        ``int` `min_dis = n; ` `        ``int` `index = 0; ` ` `  `        ``for` `(``int` `i = 1; i < n; i++) ` `        ``{ ` `             `  `            ``// case a ` `            ``if` `(maximum_element == arr[i]) ` `            ``{ ` `                ``min_dis = Math.Min(min_dis,  ` `                                ``(i - index)); ` `                ``index = i; ` `            ``} ` `     `  `            ``// case b ` `            ``else` `if` `(maximum_element < arr[i]) ` `            ``{ ` `                ``maximum_element = arr[i]; ` `                ``min_dis = n; ` `                ``index = i; ` `            ``} ` `     `  `            ``// case c ` `            ``else` `                ``continue``; ` `        ``} ` `     `  `        ``return` `min_dis; ` `    ``} ` `     `  `    ``// Driver code ` `    ``public` `static` `void` `Main () ` `    ``{ ` `        ``int` `[]arr = {6, 3, 1, 3, 6, 4, 6}; ` `        ``int` `n = arr.Length; ` `         `  `        ``Console.WriteLine(``"Minimum distance = "` `                        ``+ minDistance(arr, n)); ` `    ``} ` `} ` ` `  `// This code is contributed by vt_m. `

 ` `

Output :
`Minimum distance = 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.

Improved By : Sam007

Article Tags :
Practice Tags :