Minimum replacements required to make sum of all K | Function to find minimum number of operations required to make sum of all subarrays of size K equal ; Stores number of operations ; Iterate in the range [ 0 , K - 1 ] ; Stores frequency of elements separated by distance K ; Stores maximum frequency and corresponding element ; Find max frequency element and its frequency ; Update the number of operations ; Print the result ; Driver Code ; Given Input ; Function Call
def findMinOperations ( arr , N , K ) : NEW_LINE INDENT operations = 0 NEW_LINE for i in range ( K ) : NEW_LINE INDENT freq = { } NEW_LINE for j in range ( i , N , K ) : NEW_LINE INDENT if arr [ j ] in freq : NEW_LINE INDENT freq [ arr [ j ] ] += 1 NEW_LINE DEDENT else : NEW_LINE INDENT freq [ arr [ j ] ] = 1 NEW_LINE DEDENT DEDENT max1 = 0 NEW_LINE num = 0 NEW_LINE for key , value in freq . items ( ) : NEW_LINE INDENT if ( value > max1 ) : NEW_LINE INDENT max1 = value NEW_LINE num = key NEW_LINE DEDENT DEDENT for key , value in freq . items ( ) : NEW_LINE INDENT if ( key != num ) : NEW_LINE INDENT operations += value NEW_LINE DEDENT DEDENT DEDENT print ( operations ) NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT arr = [ 3 , 4 , 3 , 5 , 6 ] NEW_LINE K = 2 NEW_LINE N = len ( arr ) NEW_LINE findMinOperations ( arr , N , K ) NEW_LINE DEDENT