Maximize 0 s in given Array after replacing each element A [ i ] with ( A [ i ] * D + B [ i ] ) | JavaScript 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 ; Print the value of ans + cnt ; Driver Code
function __gcd ( a , b ) { if ( b == 0 ) return a ; return __gcd ( b , a % b ) ; } function maxZeroes ( A , B ) { let mp = new Map ( ) ; let N = A . length ; let ans = 0 ; let cnt = 0 ; for ( let i = 0 ; i < N ; i ++ ) { let num = - B [ i ] ; let den = A [ i ] ; let gc = __gcd ( num , den ) ; if ( den != 0 ) { num /= gc ; den /= gc ; } if ( num <= 0 ) { num *= - 1 ; den *= - 1 ; } if ( den == 0 && num == 0 ) cnt ++ ; if ( den != 0 ) { if ( mp . has ( ` ${ num } ${ den } ` ) ) { mp . set ( ` ${ num } ${ den } ` , mp . get ( ` ${ num } ${ den } ` ) + 1 ) } else { mp . set ( ` ${ num } ${ den } ` , 1 ) } ans = Math . max ( mp . get ( ` ${ num } ${ den } ` ) , ans ) ; } } document . write ( ans + cnt + " " ) ; } let A = [ 1 , 2 , - 1 ] ; let B = [ - 6 , - 12 , 6 ] ; maxZeroes ( A , B ) ;