Find d to maximize the number of zeros in array c[] created as c[i] = d*a[i] + b[i]
Given two arrays of N integers. Consider an array C, where the i-th integer will be d*a[i] + b[i] where d is any arbitrary real number. The task is to print d such that array C has maximum number of zeros and also print the number of zeros.
Examples:
Input: a[] = {1, 2, 3, 4, 5}, b[] = {2, 4, 7, 11, 3}
Output:
Value of d is: -2
The number of zeros in array C is: 2
If we choose d as -2 then we get two zeros in the array C which is the maximum possible.
Input: a[] = {13, 37, 39} b[] = {1, 2, 3}
Output:
Value of d is: -0.0769231
The number of zeros in array C is: 2
The following steps can be followed to solve the above problem:
- The equation can be rewritten as d = -b[i]/a[i]
- Use hash-table to count the maximum number of occurrence of any real number to get the value of d.
- Number of zeros will be the maximum count + (number of pairs a[i] and b[i] where both are 0).
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
void findDandZeros( int a[], int b[], int n)
{
unordered_map< long double , int > mpp;
int count = 0;
for ( int i = 0; i < n; i++) {
if (b[i] != 0 && a[i] != 0) {
long double val = ( long double )(-1.0 * b[i]) /
( long double )(a[i]);
mpp[val] += 1;
}
else if (b[i] == 0 && a[i] == 0)
count += 1;
}
int maxi = 0;
for ( auto it : mpp) {
maxi = max(it.second, maxi);
}
for ( auto it : mpp) {
if (it.second == maxi) {
cout << "Value of d is: "
<< it.first << endl;
break ;
}
}
cout << "The number of zeros in array C is: "
<< maxi + count;
}
int main()
{
int a[] = { 13, 37, 39 };
int b[] = {1, 2, 3};
int n = sizeof (a) / sizeof (a[0]);
findDandZeros(a, b, n);
return 0;
}
|
Java
import java.util.*;
class geeks
{
public static void findDandZeroes( int [] a, int [] b, int n)
{
HashMap<Double, Integer> mpp = new HashMap<>();
int count = 0 ;
for ( int i = 0 ; i < n; i++)
{
if (b[i] != 0 && a[i] != 0 )
{
double val = ( double ) (- 1.0 * b[i]) / ( double ) (a[i]);
if (mpp.get(val) != null )
{
int x = mpp.get(val);
mpp.put(val, ++x);
}
else
mpp.put(val, 1 );
}
else if (b[i] == 0 && a[i] == 0 )
count += 1 ;
}
int maxi = 0 ;
for (HashMap.Entry<Double, Integer> entry : mpp.entrySet())
{
maxi = Math.max(entry.getValue(), maxi);
}
for (HashMap.Entry<Double, Integer> entry : mpp.entrySet())
{
if (entry.getValue() == maxi)
{
System.out.println( "Value of d is: " + entry.getKey());
break ;
}
}
System.out.println( "The number of zeros in array C is: " +
(maxi + count));
}
public static void main(String[] args)
{
int [] a = { 13 , 37 , 39 };
int [] b = { 1 , 2 , 3 };
int n = a.length;
findDandZeroes(a, b, n);
}
}
|
Python3
def findDandZeros(a, b, n) :
mpp = {};
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 mpp :
mpp[val] = 0 ;
mpp[val] + = 1 ;
elif (b[i] = = 0 and a[i] = = 0 ) :
count + = 1 ;
maxi = 0 ;
for item in mpp :
maxi = max (mpp[item], maxi);
for keys, values in mpp.items() :
if (values = = maxi) :
print ( "Value of d is:" , keys);
break ;
print ( "The number of zeros in array C is:" ,
maxi + count);
if __name__ = = "__main__" :
a = [ 13 , 37 , 39 ];
b = [ 1 , 2 , 3 ];
n = len (a);
findDandZeros(a, b, n);
|
C#
using System;
using System.Collections.Generic;
class GFG
{
public static void findDandZeroes( int [] a,
int [] b, int n)
{
Dictionary< double ,
int > mpp = new Dictionary< double ,
int >();
int count = 0;
for ( int i = 0; i < n; i++)
{
if (b[i] != 0 && a[i] != 0)
{
double val = ( double )(-1.0 * b[i]) /
( double )(a[i]);
if (mpp.ContainsKey(val))
{
mpp[val] = ++mpp[val];
}
else
mpp.Add(val, 1);
}
else if (b[i] == 0 && a[i] == 0)
count += 1;
}
int maxi = 0;
foreach (KeyValuePair< double , int > entry in mpp)
{
maxi = Math.Max(entry.Value, maxi);
}
foreach (KeyValuePair< double , int > entry in mpp)
{
if (entry.Value == maxi)
{
Console.WriteLine( "Value of d is: " +
entry.Key);
break ;
}
}
Console.WriteLine( "The number of zeros in array C is: " +
(maxi + count));
}
public static void Main(String[] args)
{
int [] a = { 13, 37, 39 };
int [] b = { 1, 2, 3 };
int n = a.Length;
findDandZeroes(a, b, n);
}
}
|
Javascript
<script>
function findDandZeros(a, b, n)
{
var mpp = new Map();
var count = 0;
for ( var i = 0; i < n; i++) {
if (b[i] != 0 && a[i] != 0) {
var val = (-1.0 * b[i]) / (a[i]);
if (mpp.has(val))
mpp.set(val, mpp.get(val)+1)
else
mpp.set(val, 1)
}
else if (b[i] == 0 && a[i] == 0)
count += 1;
}
var maxi = 0;
mpp.forEach((value, key) => {
maxi = Math.max(value, maxi);
});
mpp.forEach((value, key) => {
if (value == maxi) {
document.write( "Value of d is: "
+ key + "<br>" );
}
});
document.write( "The number of zeros in array C is: "
+ (maxi + count));
}
var a = [13, 37, 39];
var b = [1, 2, 3];
var n = a.length;
findDandZeros(a, b, n);
</script>
|
Output
Value of d is: -0.0769231
The number of zeros in array C is: 2
Time Complexity: O(N), as we are using a loop to traverse N times and map operations will take constant time as we are using a unordered map. Where N is the number of elements in the array.
Auxiliary Space: O(N), as we are using extra space for the map. Where N is the number of elements in the array.
Last Updated :
05 Jan, 2023
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...