अवधारणा
एम पूर्णांकों के दो दिए गए सरणियों के संबंध में, एक सरणी सी मान लें, जहां i-th पूर्णांक d*a[i] + b[i] होगा जहां d को किसी भी मनमानी वास्तविक संख्या के रूप में दर्शाया गया है। हमारा काम d को इस तरह प्रदर्शित करना या प्रिंट करना है कि सरणी C में शून्य की संख्या सबसे अधिक हो और शून्य की संख्या भी प्रिंट हो।
इनपुट
a[] = {15, 40, 45}
b[] = {4, 5, 6} आउटपुट
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.
तरीके
उपरोक्त समस्या को हल करने के लिए हम नीचे दिए गए चरणों का पालन करते हैं -
- हम समीकरण को d =-b[i]/a[i] . के रूप में फिर से लिखते हैं
- d का मान प्राप्त करने के लिए किसी भी वास्तविक संख्या की सबसे बड़ी संख्या की गणना करने के लिए हैश-टेबल लागू करें।
- अब, हम यह निष्कर्ष निकालते हैं कि शून्यों की संख्या सबसे बड़ी गिनती होगी + (जोड़ों की संख्या a[i] और b[i] जहां दोनों 0 हैं)।
उदाहरण
// 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;
} आउटपुट
Value of d is: -0.133333 The number of zeros in array C is: 1