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

📅  最后修改于: 2021-06-26 09:07:41             🧑  作者: Mango

给定两个由N个整数组成的数组。考虑一个数组C ,其中第i个整数将是d * a [i] + b [i] ,其中d是任何任意实数。任务是打印d,使数组C的最大零个数也打印零个。

例子:

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

  • 该方程可重写为d = -b [i] / a [i]
  • 使用哈希表计算任何实数的最大出现次数,以得到d的值。
  • 零数将是最大计数+(对数a [i]和b [i]均为零)。

下面是上述方法的实现:

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


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

如果您希望与行业专家一起参加现场课程,请参阅《 Geeks现场课程》和《 Geeks现场课程美国》。