Find d to maximize the number of zeros in array c[] created as c[i] = d*a[i] + b[i]

Given two arrays of N integers. Consider an array C, where the i-th integer will be d*a[i] + b[i] where d is any arbitrary real number. The task is to print d such that array C has maximum number of zeros and also print the number of zeros.

Examples:

Input: a[] = {1, 2, 3, 4, 5}, b[] = {2, 4, 7, 11, 3}
Output:
Value of d is: -2
The number of zeros in array C is: 2
If we choose d as -2 then we get two zeros in the array C which is the maximum possible.

Input: a[] = {13, 37, 39} b[] = {1, 2, 3}
Output:
Value of d is: -0.0769231
The number of zeros in array C is:



The following steps can be followed to solve the above problem:

  • The equation can be rewritten as d = -b[i]/a[i]
  • Use hash-table to count the maximum number of occurrence of any real number to get the value of d.
  • Number of zeros will be the maximum count + (number of pairs a[i] and b[i] where both are 0).

Below is the implementation of the above approach:

C++

filter_none

edit
close

play_arrow

link
brightness_4
code

// C++ program to implement the above
// approach
#include <bits/stdc++.h>
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<long double, int> 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;
}

chevron_right


Python3

filter_none

edit
close

play_arrow

link
brightness_4
code

# 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

chevron_right


Output:

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


My Personal Notes arrow_drop_up

Striver(underscore)79 at Codechef and codeforces D

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.



Improved By : AnkitRai01



Article Tags :
Practice Tags :


Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.