

- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
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 Questions & Answers
- 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++
- Show that the following grammar is LR (1) S → A a |b A c |B c | b B a A → d B → d
- Find the number of ways to divide number into four parts such that a = c and b = d in C++
- Emulating a 2-d array using 1-d array in C++
- Find all pairs (a,b) and (c,d) in array which satisfy ab = cd in C++
- Find the Number which contain the digit d in C++
- 8085 program to implement the following function (a*b) + (c*d)
- Stack 1-D arrays as columns into a 2-D array in Numpy
- Construct Quadruples, Triples, and Indirect Triples for the expression -(a + b) * (c + d) - (a + b + c)
- C++ Program to Generate All Possible Combinations Out of a,b,c,d,e
- Find minimum possible digit sum after adding a number d in C++
- Print a 2 D Array or Matrix in C#
- Registers B, C, D, E, H, and L in 8085 Microprocessor