📜  查找 d 以最大化创建为 c[i] = d*a[i] + b[i] 的数组 c[] 中零的数量

📅  最后修改于: 2021-10-27 17:00:30             🧑  作者: Mango

给定两个N整数数组。考虑一个数组C ,其中第 i 个整数将是d*a[i] + b[i] ,其中 d 是任意实数。任务是打印 d 使得数组 C 具有最大数量的零,并打印零的数量。
例子:

可以按照以下步骤解决上述问题:

  • 方程可以改写为d = -b[i]/a[i]
  • 使用hash-table统计任意实数出现的最大次数,得到d的值。
  • 零的数量将是最大计数 +(对 a[i] 和 b[i] 的数量,其中两者均为 0)。

下面是上述方法的实现:

C++
// C++ program to implement the above
// approach
#include 
using namespace std;
 
// Function to find the value of d
// and find the number of zeros in the array
void findDandZeros(int a[], int b[], int n)
{
 
    // Hash table
    unordered_map mpp;
 
    int count = 0;
 
    // Iterate for i-th element
    for (int i = 0; i < n; i++) {
 
        // If both are not 0
        if (b[i] != 0 && a[i] != 0) {
            long double val = (long double)(-1.0 * b[i]) /
                              (long double)(a[i]);
            mpp[val] += 1;
        }
 
        // If both are 0
        else if (b[i] == 0 && a[i] == 0)
            count += 1;
    }
 
    // Find max occurring d
    int maxi = 0;
    for (auto it : mpp) {
        maxi = max(it.second, maxi);
    }
 
    // Print the d which occurs max times
    for (auto it : mpp) {
        if (it.second == maxi) {
            cout << "Value of d is: "
                 << it.first << endl;
            break;
        }
    }
 
    // Print the number of zeros
    cout << "The number of zeros in array C is: "
         << maxi + count;
}
 
// Driver code
int main()
{
    int a[] = { 13, 37, 39 };
    int b[] = { 1, 2, 3 };
    int n = sizeof(a) / sizeof(a[0]);
    findDandZeros(a, b, n);
 
    return 0;
}


Java
// Java program to implement the above
// approach
import java.util.*;
 
class geeks
{
 
    // Function to find the value of d
    // and find the number of zeros in the array
    public static void findDandZeroes(int[] a, int[] b, int n)
    {
 
        // Hash table
        HashMap mpp = new HashMap<>();
 
        int count = 0;
 
        // Iterate for i-th element
        for (int i = 0; i < n; i++)
        {
 
            // If both are not 0
            if (b[i] != 0 && a[i] != 0)
            {
                double val = (double) (-1.0 * b[i]) / (double) (a[i]);
                if (mpp.get(val) != null)
                {
                    int x = mpp.get(val);
                    mpp.put(val, ++x);
                }
                else
                    mpp.put(val, 1);
            }
 
            // If both are 0
            else if (b[i] == 0 && a[i] == 0)
                count += 1;
        }
 
        // Find max occurring d
        int maxi = 0;
        for (HashMap.Entry entry : mpp.entrySet())
        {
            maxi = Math.max(entry.getValue(), maxi);
        }
 
        // Print the d which occurs max times
        for (HashMap.Entry entry : mpp.entrySet())
        {
            if (entry.getValue() == maxi)
            {
                System.out.println("Value of d is: " + entry.getKey());
                break;
            }
        }
 
        // Print the number of zeros
        System.out.println("The number of zeros in array C is: " +
                                                (maxi + count));
    }
 
    // Driver Code
    public static void main(String[] args)
    {
        int[] a = { 13, 37, 39 };
        int[] b = { 1, 2, 3 };
        int n = a.length;
 
        findDandZeroes(a, b, n);
    }
}
 
// This code is contributed by
// sanjeev2552


Python3
# Python3 program to implement the
# above approach
 
# Function to find the value of d and
# find the number of zeros in the array
def findDandZeros(a, b, n) :
 
    # Hash table
    mpp = {};
 
    count = 0;
 
    # Iterate for i-th element
    for i in range(n) :
 
        # If both are not 0
        if (b[i] != 0 and a[i] != 0) :
            val = (-1.0 * b[i]) / a[i];
             
            if val not in mpp :
                mpp[val] = 0;
                 
            mpp[val] += 1;
 
        # If both are 0
        elif (b[i] == 0 and a[i] == 0) :
            count += 1;
 
    # Find max occurring d
    maxi = 0;
    for item in mpp :
        maxi = max(mpp[item], maxi);
 
    # Print the d which occurs max times
    for keys, values in mpp.items() :
        if (values == maxi) :
            print("Value of d is:", keys);
            break;
 
    # Print the number of zeros
    print("The number of zeros in array C is:",
                                 maxi + count);
 
# Driver code
if __name__ == "__main__" :
    a = [ 13, 37, 39 ];
    b = [ 1, 2, 3 ];
     
    n = len(a);
    findDandZeros(a, b, n);
 
# This code is contributed by Ryuga


C#
// C# program to implement the above
// approach
using System;
using System.Collections.Generic;
     
class GFG
{
 
    // Function to find the value of d
    // and find the number of zeros in the array
    public static void findDandZeroes(int[] a,
                                      int[] b, int n)
    {
 
        // Hash table
        Dictionary mpp = new Dictionary();
        int count = 0;
 
        // Iterate for i-th element
        for (int i = 0; i < n; i++)
        {
 
            // If both are not 0
            if (b[i] != 0 && a[i] != 0)
            {
                double val = (double)(-1.0 * b[i]) /
                             (double)(a[i]);
                if (mpp.ContainsKey(val))
                {
                    mpp[val] = ++mpp[val];
                }
                else
                    mpp.Add(val, 1);
            }
 
            // If both are 0
            else if (b[i] == 0 && a[i] == 0)
                count += 1;
        }
 
        // Find max occurring d
        int maxi = 0;
        foreach(KeyValuePair entry in mpp)
        {
            maxi = Math.Max(entry.Value, maxi);
        }
 
        // Print the d which occurs max times
        foreach(KeyValuePair entry in mpp)
        {
            if (entry.Value == maxi)
            {
                Console.WriteLine("Value of d is: " +
                                          entry.Key);
                break;
            }
        }
 
        // Print the number of zeros
        Console.WriteLine("The number of zeros in array C is: " +
                                                 (maxi + count));
    }
 
    // Driver Code
    public static void Main(String[] args)
    {
        int[] a = { 13, 37, 39 };
        int[] b = { 1, 2, 3 };
        int n = a.Length;
 
        findDandZeroes(a, b, n);
    }
}
 
// This code is contributed by Rajput-Ji


Javascript


输出:
Value of d is: -0.0769231
The number of zeros in array C is: 2