Minimum distance between any most frequent and least frequent element of an array | Python3 implementation of the approach ; Function to find the minimum distance between any two most and least frequent element ; Initialize sets to store the least and the most frequent elements ; Initialize variables to store max and min frequency ; Initialize HashMap to store frequency of each element ; Loop through the array ; Store the count of each element ; Store the least and most frequent elements in the respective sets ; Store count of current element ; If count is equal to max count ; Store in max set ; If count is greater then max count ; Empty max set ; Update max count ; Store in max set ; If count is equal to min count ; Store in min set ; If count is less then max count ; Empty min set ; Update min count ; Store in min set ; Initialize a variable to store the minimum distance ; Initialize a variable to store the last index of least frequent element ; Traverse array ; If least frequent element ; Update last index of least frequent element ; If most frequent element ; Update minimum distance ; Traverse array from the end ; If least frequent element ; Update last index of least frequent element ; If most frequent element ; Update minimum distance ; Print the minimum distance ; Driver Code ; Given array ; Function Call
import sys NEW_LINE def getMinimumDistance ( a , n ) : NEW_LINE INDENT min_set = { } NEW_LINE max_set = { } NEW_LINE max , min = 0 , sys . maxsize + 1 NEW_LINE frequency = { } NEW_LINE for i in range ( n ) : NEW_LINE INDENT frequency [ a [ i ] ] = frequency . get ( a [ i ] , 0 ) + 1 NEW_LINE DEDENT for i in range ( n ) : NEW_LINE INDENT count = frequency [ a [ i ] ] NEW_LINE if ( count == max ) : NEW_LINE INDENT max_set [ a [ i ] ] = 1 NEW_LINE DEDENT elif ( count > max ) : NEW_LINE INDENT max_set . clear ( ) NEW_LINE max = count NEW_LINE max_set [ a [ i ] ] = 1 NEW_LINE DEDENT if ( count == min ) : NEW_LINE INDENT min_set [ a [ i ] ] = 1 NEW_LINE DEDENT elif ( count < min ) : NEW_LINE INDENT min_set . clear ( ) NEW_LINE min = count NEW_LINE min_set [ a [ i ] ] = 1 NEW_LINE DEDENT DEDENT min_dist = sys . maxsize + 1 NEW_LINE last_min_found = - 1 NEW_LINE for i in range ( n ) : NEW_LINE INDENT if ( a [ i ] in min_set ) : NEW_LINE INDENT last_min_found = i NEW_LINE DEDENT if ( ( a [ i ] in max_set ) and last_min_found != - 1 ) : NEW_LINE INDENT if i - last_min_found < min_dist : NEW_LINE INDENT min_dist = i - last_min_found NEW_LINE DEDENT DEDENT DEDENT last_min_found = - 1 NEW_LINE for i in range ( n - 1 , - 1 , - 1 ) : NEW_LINE INDENT if ( a [ i ] in min_set ) : NEW_LINE INDENT last_min_found = i ; NEW_LINE DEDENT if ( ( a [ i ] in max_set ) and last_min_found != - 1 ) : NEW_LINE INDENT if min_dist > last_min_found - i : NEW_LINE INDENT min_dist = last_min_found - i NEW_LINE DEDENT DEDENT DEDENT print ( min_dist ) NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT arr = [ 1 , 1 , 2 , 3 , 2 , 3 , 3 ] NEW_LINE N = len ( arr ) NEW_LINE getMinimumDistance ( arr , N ) NEW_LINE DEDENT