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

Suppose we have two arrays A and B of n integers, now consider an array C, where the i-th number will be d*A[i] + B[i] and here d is any arbitrary real number. We have to find d such that array C has maximum number of zeros. Also return the number of zeros.

So, if the input is like A = [15, 40, 45] and B = [4, 5, 6], then the output will be d = -0.266666, number of zeros will be 1

To solve this, we will follow these steps −

• n := size of A

• my_map := a new map

• count := 0

• for i in range 0 to n, do

• if B[i] is not same as 0 and A[i] is not same as 0, then

• val :=(-1.0 * B[i]) / A[i]

• if val not in my_map, then

• my_map[val] := 0

• my_map[val] := my_map[val] + 1

• otherwise when B[i] is same as 0 and A[i] is same as 0, then

• count := count + 1

• maximum := 0;

• for each item in my_map, do

• maximum := maximum of my_map[item], maximum

• for each keys, values in my_map, do

• if values is same as maximum, then

• display keys

• come out from the loop

• display maximum + count

## Example (Python)

Let us see the following implementation to get better understanding −

Live Demo

def find_d_zero(A, B) :
n = len(A)
my_map = {}
count = 0
for i in range(n) :
if (B[i] != 0 and A[i] != 0) :
val = (-1.0 * B[i]) / A[i]
if val not in my_map :
my_map[val] = 0
my_map[val] += 1
elif (B[i] == 0 and A[i] == 0) :
count += 1
maximum = 0;
for item in my_map :
maximum = max(my_map[item], maximum)
for keys, values in my_map.items() :
if (values == maximum) :
print("d = ", keys)
break
print("Number of 0s: ", maximum + count)
a = [15, 40, 45]
b = [4, 5, 6]
find_d_zero(a, b)

## Input

[15, 40, 45], [4,5,6]

## Output

d = -0.26666666666666666
Number of 0s: 1