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

Given two arrays A and B of n integers, we need to create array C where C[i] = d*A[i] + B[i] and d is any real number. Our goal is to find the value of d that maximizes the number of zeros in array C.

For C[i] to be zero, we need d*A[i] + B[i] = 0, which gives us d = -B[i]/A[i] (when A[i] ? 0).

Algorithm

The approach is to ?

  • Calculate possible values of d for each element where A[i] ? 0

  • Count frequency of each d value using a dictionary

  • Handle special case where both A[i] = 0 and B[i] = 0 (always zero regardless of d)

  • Find the d value with maximum frequency

Example

def find_d_zero(A, B):
    n = len(A)
    freq_map = {}
    always_zero_count = 0
    
    for i in range(n):
        if B[i] != 0 and A[i] != 0:
            # Calculate d that makes C[i] = 0
            d_value = (-1.0 * B[i]) / A[i]
            freq_map[d_value] = freq_map.get(d_value, 0) + 1
        elif B[i] == 0 and A[i] == 0:
            # Always zero regardless of d
            always_zero_count += 1
    
    # Find maximum frequency
    max_zeros = 0
    best_d = None
    
    for d_val, count in freq_map.items():
        if count > max_zeros:
            max_zeros = count
            best_d = d_val
    
    total_zeros = max_zeros + always_zero_count
    
    print(f"d = {best_d}")
    print(f"Number of zeros: {total_zeros}")
    
    return best_d, total_zeros

# Test the function
A = [15, 40, 45]
B = [4, 5, 6]
find_d_zero(A, B)
d = -0.26666666666666666
Number of zeros: 1

How It Works

For the given example A = [15, 40, 45] and B = [4, 5, 6] ?

# Let's trace through the calculation
A = [15, 40, 45]
B = [4, 5, 6]

print("Calculating d values for each element:")
for i in range(len(A)):
    if A[i] != 0:
        d = -B[i] / A[i]
        print(f"Element {i}: d = -{B[i]}/{A[i]} = {d:.6f}")
        print(f"Verification: {d} * {A[i]} + {B[i]} = {d * A[i] + B[i]:.10f}")
    print()
Calculating d values for each element:
Element 0: d = -4/15 = -0.266667
Verification: -0.26666666666666666 * 15 + 4 = 0.0000000000

Element 1: d = -5/40 = -0.125000
Verification: -0.125 * 40 + 6 = 1.0000000000

Element 2: d = -6/45 = -0.133333
Verification: -0.13333333333333333 * 45 + 6 = 0.0000000000

Edge Cases

# Case 1: Multiple elements with same d value
A1 = [2, 4, 6]
B1 = [1, 2, 3]
print("Case 1 - Same d value:")
find_d_zero(A1, B1)
print()

# Case 2: Elements that are always zero
A2 = [0, 2, 0]
B2 = [0, 4, 1]
print("Case 2 - Always zero elements:")
find_d_zero(A2, B2)
Case 1 - Same d value:
d = -0.5
Number of zeros: 3

Case 2 - Always zero elements:
d = -2.0
Number of zeros: 2

Time Complexity

The algorithm has O(n) time complexity where n is the length of arrays A and B. We iterate through the arrays once to calculate frequencies and once more to find the maximum.

Conclusion

This solution efficiently finds the optimal d value by calculating all possible d values that make individual elements zero and selecting the one with maximum frequency. Elements where both A[i] and B[i] are zero contribute to the final count regardless of d value.

Updated on: 2026-03-25T09:36:15+05:30

301 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements