
- C++ Basics
- C++ Home
- C++ Overview
- C++ Environment Setup
- C++ Basic Syntax
- C++ Comments
- C++ Data Types
- C++ Variable Types
- C++ Variable Scope
- C++ Constants/Literals
- C++ Modifier Types
- C++ Storage Classes
- C++ Operators
- C++ Loop Types
- C++ Decision Making
- C++ Functions
- C++ Numbers
- C++ Arrays
- C++ Strings
- C++ Pointers
- C++ References
- C++ Date & Time
- C++ Basic Input/Output
- C++ Data Structures
- C++ Object Oriented
- C++ Classes & Objects
- C++ Inheritance
- C++ Overloading
- C++ Polymorphism
- C++ Abstraction
- C++ Encapsulation
- C++ Interfaces
Find d to maximize the number of zeros in array c[] created as c[i] = d*a[i] + b[i] in C++
Concept
With respect of two given arrays of M integers, assume an array C, where the i-th integer will be d*a[i] + b[i] where d is indicated as any arbitrary real number. Our task is to display or print d such that array C has largest number of zeros and also prints the number of zeros.
Input
a[] = {15, 40, 45} b[] = {4, 5, 6}
Output
Value of d is: -0.133333 The number of zeros in array C is: 1 If we choose d as -0.133333 then we get one zero in the array C which is the maximum possible.
Methods
We follow the below mentioned steps to solve the above problem −
- We rewrite the equation as d = -b[i]/a[i]
- Implement hash-table to count the largest number of occurrence of any real number to obtain the value of d.
- Now,we conclude that number of zeros will be the largest count + (number of pairs a[i] and b[i] where both are 0).
Example
// C++ program to implement the above // approach #include <bits/stdc++.h> using namespace std; // Shows function to find the value of d // and find the number of zeros in the array void findDandZeros1(int a[], int b[], int m){ // Shows hash table unordered_map<long double, int> mpp1; int count1 = 0; // Performs iteration for i-th element for (int i = 0; i < m; i++) { // Now if both are not 0 if (b[i] != 0 && a[i] != 0) { long double val1 = (long double)(-1.0 * b[i]) / (long double)(a[i]); mpp1[val1] += 1; } // Now if both are 0 else if (b[i] == 0 && a[i] == 0) count1 += 1; } // Used to find max occurring d int maxi1 = 0; for (auto it : mpp1) { maxi1 = max(it.second, maxi1); } // Used to print the d which occurs max times for (auto it : mpp1) { if (it.second == maxi1) { cout << "Value of d is: " << it.first << endl; break; } } // Used to print the number of zeros cout << "The number of zeros in array C is: " << maxi1 + count1; } // Driver code int main(){ int a[] = { 15, 40, 45 }; int b[] = { 4, 5, 6 }; int m = sizeof(a) / sizeof(a[0]); findDandZeros1(a, b, m); return 0; }
Output
Value of d is: -0.133333 The number of zeros in array C is: 1
- Related Articles
- Find d to maximize the number of zeros in array c[] created as c[i] = d*a[i] + b[i] in Python
- 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++
- Maximize the median of an array in C++
- Count number of trailing zeros in product of array in C++
- Emulating a 2-d array using 1-d 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

Advertisements