Maximize 0 s in given Array after replacing each element A [ i ] with ( A [ i ] * D + B [ i ] ) | Python program for the above approach ; Function to find the maximum number of 0 s in the array A [ ] after changing the array element to A [ i ] * D + B [ i ] ; Stores the frequency of fractions needed to make each element 0 ; Stores the maximum number of 0 ; Traverse the array ; Find the numerator and the denominator ; Check if den is not equal to 0 ; Divide num and den by their gcd ; Check if num is not greater than 0 ; Check if both num and den are equal to 0 ; Increment the value of { num , den } in the map ; Update the value of ans ; Prthe value of ans + cnt ; Driver Code
from math import gcd NEW_LINE def maxZeroes ( A , B ) : NEW_LINE INDENT mp = { } NEW_LINE N = len ( A ) NEW_LINE ans = 0 NEW_LINE cnt = 0 NEW_LINE for i in range ( N ) : NEW_LINE INDENT num = - B [ i ] NEW_LINE den = A [ i ] NEW_LINE gc = gcd ( num , den ) NEW_LINE if ( den != 0 ) : NEW_LINE INDENT num //= gc NEW_LINE den //= gc NEW_LINE DEDENT if ( num <= 0 ) : NEW_LINE INDENT num *= - 1 NEW_LINE den *= - 1 NEW_LINE DEDENT if ( den == 0 and num == 0 ) : NEW_LINE INDENT cnt += 1 NEW_LINE DEDENT if ( den != 0 ) : NEW_LINE INDENT mp [ ( num , den ) ] = mp . get ( ( num , den ) , 0 ) + 1 NEW_LINE ans = max ( mp [ ( num , den ) ] , ans ) NEW_LINE DEDENT DEDENT print ( ans + cnt ) NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT A = [ 1 , 2 , - 1 ] NEW_LINE B = [ - 6 , - 12 , 6 ] NEW_LINE maxZeroes ( A , B ) NEW_LINE DEDENT