
- Python Basic Tutorial
- Python - Home
- Python - Overview
- Python - Environment Setup
- Python - Basic Syntax
- Python - Comments
- Python - Variables
- Python - Data Types
- Python - Operators
- Python - Decision Making
- Python - Loops
- Python - Numbers
- Python - Strings
- Python - Lists
- Python - Tuples
- Python - Dictionary
- Python - Date & Time
- Python - Functions
- Python - Modules
- Python - Files I/O
- Python - Exceptions
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 −
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
- Related Articles
- Find d to maximize the number of zeros in array c[] created as c[i] = d*a[i] + b[i] in C++
- Find largest d in array such that a + b + c = d in C++
- Find four elements a, b, c and d in an array such that a+b = c+d in C++
- Maximize the number of subarrays with XOR as zero in C++
- Find the number of ways to divide number into four parts such that a = c and b = d in C++
- In the below figure, \( A B C D \) is a rectangle with \( A B=14 \mathrm{~cm} \) and \( B C=7 \mathrm{~cm} \). Taking \( D C, B C \) and \( A D \) as diameters, three semi-circles are drawn as shown in the figure. Find the area of the shaded region."\n
- In \( \triangle A B C, A D \perp B C \) and \( A D^{2}=B D . C D \).Prove that \( \angle B A C=90^o \)."\n
- Find all pairs (a,b) and (c,d) in array which satisfy ab = cd in C++
- Count number of trailing zeros in product of array in C++
- Emulating a 2-d array using 1-d array in C++
- Maximize the median of an array in C++
- Four students A, B, C, and D looked through pipes of different shapes to see a candle flame as shown in the figure.Who will be able to see the candle flame clearly?$(a)$. A$(b)$. B$(c)$. C$(d)$. D"
- In the figure given below .Is \( A B+B C+C D+D Aanswer."\n
- In a quadrilateral \( A B C D, \angle B=90^{\circ}, A D^{2}=A B^{2}+B C^{2}+C D^{2}, \) prove that $\angle A C D=90^o$.
- The positions of four elements A, B, Cand D in the modern periodic table are shown below(a)A(b)B(c)C(d)D"\n
