

- 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 integers that divides maximum number of elements of the array in C++
<p>In this problem, we are given an array arr[] of n integers.</p><p>Our task is to<em> find integers that divides maximum number of elements of the array. </em></p><p><strong>Problem Description: </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’s take an example to understand the problem, </strong></p><p><strong>Input: </strong>arr[] = {4, 5, 6, 7, 8}</p><p><strong>Output: </strong>2</p><p><strong>Explanation: </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 </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="http://tpcg.io/Gv5GD7oS" rel="nofollow" target="_blank">Live Demo</a></p><pre class="prettyprint notranslate">#include <bits/stdc++.h> using namespace std; #define MAXN 100001 int primes[MAXN]; void findPrimeSieve() { primes[1] = 1; for (int i = 2; i < MAXN; i++) primes[i] = i; for (int i = 4; i < MAXN; i += 2) primes[i] = 2; for (int i = 3; i * i < MAXN; i++) { if (primes[i] == i) { for (int j = i * i; j < MAXN; j += i) if (primes[j] == j) primes[j] = i; } } } vector<int> findFactors(int num) { vector<int> factors; while (num != 1) { int temp = primes[num]; factors.push_back(temp); while (num % temp == 0) num = num / temp; } return factors; } int findmaxDivElement(int arr[], int n) { findPrimeSieve(); map<int, int> factorFreq; for (int i = 0; i < n; ++i) { vector<int> p = findFactors(arr[i]); for (int i = 0; i < p.size(); i++) factorFreq[p[i]]++; } int cnt = 0, ans = 1e+7; for (auto itr : factorFreq) { if (itr.second >= cnt) { cnt = itr.second; ans > itr.first ? ans = itr.first : ans = ans; } } return ans; } int main() { int arr[] = { 4, 5, 6, 7, 8 }; int n = sizeof(arr) / sizeof(arr[0]); cout<<"The number that divides the maximum elements of the array is "<<findmaxDivElement(arr, n); return 0; }</pre><h2>Output</h2><pre class="result notranslate">The number that divides the maximum elements of the array is 2</pre>
- Related Questions & Answers
- Find a number that divides maximum array elements in C++
- Find maximum power of a number that divides a factorial in C++
- Given an array of integers, find the pair of adjacent elements that has the largest product and return that product JavaScript
- Maximum number of Unique integers in Sub- Array of given sizes in C++
- Find a pair with maximum product in array of Integers in C++
- Maximum number of contiguous array elements with same number of set bits in C++
- Find array elements that are out of order in JavaScript
- Write a program in C++ to find the maximum and second maximum in a given unsorted array of integers
- Find k maximum elements of array in original order in C++
- Find the maximum number of composite summands of a number in Python
- Find a non empty subset in an array of N integers such that sum of elements of subset is divisible by N in C++
- Find maximum XOR of given integer in a stream of integers in C++
- Find the first repeating element in an array of integers C++
- Find the minimum and maximum values that can be calculated by summing exactly four of the five integers in JavaScript
- C++ program to find out the maximum number of cells that can be illuminated
Advertisements