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++
// 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; } |
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<Double, Integer> 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<Double, Integer> entry : mpp.entrySet()) { maxi = Math.max(entry.getValue(), maxi); } // Print the d which occurs max times for (HashMap.Entry<Double, Integer> 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 |
Value of d is: -0.0769231 The number of zeros in array C is: 2
Recommended Posts:
- Find the smallest number X such that X! contains at least Y trailing zeros.
- Count number of trailing zeros in product of array
- Rearrange the array to maximize the number of primes in prefix sum of the array
- Find a partition point in array to maximize its xor sum
- Maximize array elements upto given number
- Maximum subarray sum in an array created after repeated concatenation
- Maximize the size of array by deleting exactly k sub-arrays to make array prime
- Remove leading zeros from an array
- Maximize the median of the given array after adding K elements to the same array
- Maximum consecutive one’s (or zeros) in a binary array
- Maximum consecutive one’s (or zeros) in a binary circular array
- Move all zeros to start and ones to end in an Array of random integers
- Maximize the sum of array by multiplying prefix of array with -1
- Maximize value of (arr[i] - i) - (arr[j] - j) in an array
- Maximize value of (a[i]+i)*(a[j]+j) in an array
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.



