📌  相关文章
📜  将每个元素 A[i] 替换为 (A[i]*D + B[i]) 后,最大化给定数组中的 0

📅  最后修改于: 2021-10-26 06:22:03             🧑  作者: Mango

给定两个由 N 个整数组成的数组 A[]B[] ,任务是在将每个数组元素A[i]替换为A[i]*后,在数组A[]中找到最大数量的0 D + B[i]通过选择D 的任何值。

例子:

方法:可以通过计算使每个数组元素A[i]0并将值存储在 Map 中所需的 D值来解决给定的问题。请按照以下步骤解决问题:

  • 初始化一个 Map,比如M和两个整数,比如cntans0
  • 使用变量i在范围[0, N – 1] 上迭代并执行以下步骤:
    • 将两个整数num初始化为-B[i]并将den初始化为A[i]
    • 如果DEN的值不等于0,那么通过NumDEN的GCD划分值NumDEN两者。
    • 如果 num的值不大于0 ,则将numden都乘以 -1
    • 如果 dennum的值都等于0 ,则将 cnt的值增加1 ,如果den的值不等于0则将 mp[{num, den}]的值增加1并更新ans 的值作为max(ans, mp[{num, den}]
  • 完成上述步骤后,打印ans + cnt的值作为 D的可能值,该值在执行给定操作后使给定数组A[i] 中的 0计数最大化。

下面是上述方法的实现:

C++
// C++ program for the above approach
 
#include 
using namespace std;
 
// Function to find the maximum number
// of 0s in the array A[] after changing
// the array element to A[i]*D + B[i]
void maxZeroes(vector& A,
               vector& B)
{
    // Stores the frequency of fractions
    // needed to make each element 0
    map, int> mp;
    int N = A.size();
 
    // Stores the maximum number of 0
    int ans = 0;
    int cnt = 0;
 
    // Traverse the array
    for (int i = 0; i < N; i++) {
 
        // Find the numerator and
        // the denominator
        int num = -B[i];
        int den = A[i];
 
        int gc = __gcd(num, den);
 
        // Check if den is not equal
        // to 0
        if (den != 0) {
 
            // Divide num and den
            // by their gcd
            num /= gc;
            den /= gc;
        }
 
        // Check if num is not
        // greater than 0
        if (num <= 0) {
            num *= -1;
            den *= -1;
        }
 
        // Check if both num and den
        // are equal to 0
        if (den == 0 and num == 0)
            cnt++;
 
        if (den != 0) {
 
            // Increment the value of
            // {num, den} in the map
            mp[{ num, den }]++;
 
            // Update the value of ans
            ans = max(mp[{ num, den }], ans);
        }
    }
 
    // Print the value of ans+cnt
    cout << ans + cnt << endl;
}
 
// Driver Code
int main()
{
    vector A = { 1, 2, -1 };
    vector B = { -6, -12, 6 };
    maxZeroes(A, B);
 
    return 0;
}


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


Javascript


输出:
3

时间复杂度: O(N log N)
辅助空间: O(N)

如果您希望与专家一起参加现场课程,请参阅DSA 现场工作专业课程学生竞争性编程现场课程。