Maximize 0 s in given Array after replacing each element A [ i ] with ( A [ i ] * D + B [ i ] ) | C ++ 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
#include <bits/stdc++.h> NEW_LINE using namespace std ; void maxZeroes ( vector < int > & A , vector < int > & B ) { map < pair < int , int > , int > mp ; int N = A . size ( ) ; int ans = 0 ; int cnt = 0 ; for ( int i = 0 ; i < N ; i ++ ) { int num = - B [ i ] ; int den = A [ i ] ; int gc = __gcd ( num , den ) ; if ( den != 0 ) { num /= gc ; den /= gc ; } if ( num <= 0 ) { num *= -1 ; den *= -1 ; } if ( den == 0 and num == 0 ) cnt ++ ; if ( den != 0 ) { mp [ { num , den } ] ++ ; ans = max ( mp [ { num , den } ] , ans ) ; } } cout << ans + cnt << endl ; } int main ( ) { vector < int > A = { 1 , 2 , -1 } ; vector < int > B = { -6 , -12 , 6 } ; maxZeroes ( A , B ) ; return 0 ; }