Maximum frequency of any array element possible by at most K increments | Function to find the maximum possible frequency of a most frequent element after at most K increment operations ; Sort the input array ; Stores the sum of sliding window and the maximum possible frequency of any array element ; Traverse the array ; Add the current element to the window ; If it is not possible to make the array elements in the window equal ; Update the value of sum ; Increment the value of start ; Update the maximum possible frequency ; Print the frequency of the most frequent array element after K increments ; Driver code
def maxFrequency ( arr , N , K ) : NEW_LINE INDENT arr . sort ( ) NEW_LINE start = 0 NEW_LINE end = 0 NEW_LINE sum = 0 NEW_LINE res = 0 NEW_LINE for end in range ( N ) : NEW_LINE INDENT sum += arr [ end ] NEW_LINE while ( ( end - start + 1 ) * arr [ end ] - sum > K ) : NEW_LINE INDENT sum -= arr [ start ] NEW_LINE start += 1 NEW_LINE DEDENT res = max ( res , end - start + 1 ) NEW_LINE DEDENT print ( res ) NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT arr = [ 1 , 4 , 8 , 13 ] NEW_LINE N = 4 NEW_LINE K = 5 NEW_LINE maxFrequency ( arr , N , K ) NEW_LINE DEDENT