Find integers that divides maximum number of elements of the array in C++

C++Server Side ProgrammingProgramming

<p>In this problem, we are given an array arr[] of n integers.</p><p>Our task is to<em>&nbsp;find integers that divides maximum number of elements of the array.&nbsp;</em></p><p><strong>Problem Description:&nbsp;</strong>We need to find a number p which can divide the maximum number of elements of the array. In case, there are more than one such element we will return the smaller one.</p><p><strong>Let&rsquo;s take an example to understand the problem,&nbsp;</strong></p><p><strong>Input:&nbsp;</strong>arr[] = {4, 5, 6, 7, 8}</p><p><strong>Output:&nbsp;</strong>2</p><p><strong>Explanation:&nbsp;</strong></p><p>The element 2 divides {4, 6, 8}.</p><h2>Solution Approach</h2><p>A simple solution to the problem is by looping through the array and then for each element of the array, divide each element from the array with elements from 1 to k. And return the element which divides the maximum number of elements of the array.</p><p><strong>Another approach&nbsp;</strong>to solve the problem is by using the fact that all elements of the array are divided by the prime factors.</p><p>We will store the frequency of division by each prime number and then return factor with maximum frequency. We store the prime numbers and their frequencies in a hash.</p><h2>Program to illustrate the working of our solution,</h2><h2>Example</h2><p><a class="demo" href="" rel="nofollow" target="_blank">Live Demo</a></p><pre class="prettyprint notranslate">#include &lt;bits/stdc++.h&gt; using namespace std; #define MAXN 100001 int primes[MAXN]; void findPrimeSieve() { &nbsp; &nbsp;primes[1] = 1; &nbsp; &nbsp;for (int i = 2; i &lt; MAXN; i++) &nbsp; &nbsp; &nbsp; primes[i] = i; &nbsp; &nbsp;for (int i = 4; i &lt; MAXN; i += 2) &nbsp; &nbsp; &nbsp; primes[i] = 2; &nbsp; &nbsp;for (int i = 3; i * i &lt; MAXN; i++) { &nbsp; &nbsp; &nbsp; if (primes[i] == i) { &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;for (int j = i * i; j &lt; MAXN; j += i) &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if (primes[j] == j) &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;primes[j] = i; &nbsp; &nbsp; &nbsp; } &nbsp; &nbsp;} } vector&lt;int&gt; findFactors(int num) { &nbsp; &nbsp;vector&lt;int&gt; factors; &nbsp; &nbsp;while (num != 1) { &nbsp; &nbsp; &nbsp; int temp = primes[num]; &nbsp; &nbsp; &nbsp; factors.push_back(temp); &nbsp; &nbsp; &nbsp; while (num % temp == 0) &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;num = num / temp; &nbsp; &nbsp;} &nbsp; &nbsp;return factors; } int findmaxDivElement(int arr[], int n) { &nbsp; &nbsp;findPrimeSieve(); &nbsp; &nbsp;map&lt;int, int&gt; factorFreq; &nbsp; &nbsp;for (int i = 0; i &lt; n; ++i) { &nbsp; &nbsp; &nbsp; vector&lt;int&gt; p = findFactors(arr[i]); &nbsp; &nbsp; &nbsp; for (int i = 0; i &lt; p.size(); i++) &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;factorFreq[p[i]]++; &nbsp; &nbsp;} &nbsp; &nbsp;int cnt = 0, ans = 1e+7; &nbsp; &nbsp;for (auto itr : factorFreq) { &nbsp; &nbsp; &nbsp; if (itr.second &gt;= cnt) { &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;cnt = itr.second; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;ans &gt; itr.first ? ans = itr.first : ans = ans; &nbsp; &nbsp; &nbsp; } &nbsp; &nbsp;} &nbsp; &nbsp;return ans; } int main() { &nbsp; &nbsp;int arr[] = { 4, 5, 6, 7, 8 }; &nbsp; &nbsp;int n = sizeof(arr) / sizeof(arr[0]); &nbsp; &nbsp;cout&lt;&lt;&quot;The number that divides the maximum elements of the array is &quot;&lt;&lt;findmaxDivElement(arr, n); &nbsp; &nbsp;return 0; }</pre><h2>Output</h2><pre class="result notranslate">The number that divides the maximum elements of the array is 2</pre>
Updated on 25-Jan-2021 04:51:49