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.



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[0], 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

// C program to find Min distance 
// of maximum element
#include<bits/stdc++.h>
using namespace std;

//function to return min distance
int minDistance (int arr[], int n)
{
    int maximum_element = arr[0];
    int min_dis = n;
    int index = 0;

    for (int i=1; i<n; i++)
    {
        // case a
        if (maximum_element == arr[i])
        {
            min_dis = 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 program
int main()
{
    int arr[] = {6, 3, 1, 3, 6, 4, 6};
    int n = sizeof(arr)/sizeof(arr[0]);
    cout << "Minimum distance = " 
         << minDistance(arr, n);
    return 0;
} 

Java

// 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<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 (String[] args)
    {
        int arr[] = {6, 3, 1, 3, 6, 4, 6};
        int n = arr.length;
        System.out.print("Minimum distance = "+minDistance(arr, n));
    }
}

// This code is contributed by Anant Agarwal.

Python3

# 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#

// 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[0];
        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.

PHP

<?php
// PHP program to find Min distance 
// of maximum element

//function to return min distance
function minDistance($arr, $n)
{
    $maximum_element = $arr[0];
    $min_dis = $n;
    $index = 0;

    for ($i = 1; $i < $n; $i++)
    {
        
        // case a
        if ($maximum_element == $arr[$i])
        {
            $min_dis = 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
    $arr = array(6, 3, 1, 3, 6, 4, 6);
    $n = count($arr);
    echo "Minimum distance = "
          .minDistance($arr,$n); 
          
// This code is contributed by Sam007
?>


Output :

Minimum distance = 2


My Personal Notes arrow_drop_up

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 write comments if you find anything incorrect, or you want to share more information about the topic discussed above.


Improved By : Sam007




Practice Tags :
Article Tags :
Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.

Recommended Posts:



2 Average Difficulty : 2/5.0
Based on 2 vote(s)






User Actions