Maximum number of multiples in an array before any element
Given an array arr[], the task is to find the maximum number of indices j < i such that (arr[j] % arr[i]) = 0 among all the array elements.
Example:
Input: arr[] = {8, 1, 28, 4, 2, 6, 7}
Output: 3
No of multiples for each element before itself –
N(8) = 0 ()
N(1) = 1 (8)
N(28) = 0 ()
N(4) = 2 (28, 8)
N(2) = 3 (4, 28, 8)
N(6) = 0 ()
N(7) = 1 (28)
Maximum out of these multiples is – 3
Input: arr[] = {8, 12, 56, 32, 10, 3, 2, 4}
Output: 5
Approach:
- Use a map to store all the divisors of each array element.
- Generate all the divisors of an element in sqrt(n) time using the approach discussed in this article.
- Now, take the maximum of all the stored divisors for each element and update it.
Below is the implementation of the above approach:
C++14
#include <bits/stdc++.h>
using namespace std;
const int MAX = 100000;
int divisors[MAX];
int generateDivisors( int n)
{
for ( int i = 1; i <= sqrt (n); i++) {
if (n % i == 0) {
if (n / i == i) {
divisors[i]++;
}
else {
divisors[i]++;
divisors[n / i]++;
}
}
}
}
int findMaxMultiples( int * arr, int n)
{
int ans = 0;
for ( int i = 0; i < n; i++) {
ans = max(divisors[arr[i]], ans);
generateDivisors(arr[i]);
}
return ans;
}
int main()
{
int arr[] = { 8, 1, 28, 4, 2, 6, 7 };
int n = sizeof (arr) / sizeof ( int );
cout << findMaxMultiples(arr, n);
return 0;
}
|
Java
import java.util.*;
class GFG
{
static int MAX = 100000 ;
static int []divisors = new int [MAX];
static void generateDivisors( int n)
{
for ( int i = 1 ; i <= Math.sqrt(n); i++)
{
if (n % i == 0 )
{
if (n / i == i)
{
divisors[i]++;
}
else
{
divisors[i]++;
divisors[n / i]++;
}
}
}
}
static int findMaxMultiples( int []arr, int n)
{
int ans = 0 ;
for ( int i = 0 ; i < n; i++)
{
ans = Math.max(divisors[arr[i]], ans);
generateDivisors(arr[i]);
}
return ans;
}
public static void main(String[] args)
{
int arr[] = { 8 , 1 , 28 , 4 , 2 , 6 , 7 };
int n = arr.length;
System.out.print(findMaxMultiples(arr, n));
}
}
|
Python3
from math import ceil,sqrt
MAX = 100000
divisors = [ 0 ] * MAX
def generateDivisors(n):
for i in range ( 1 ,ceil(sqrt(n)) + 1 ):
if (n % i = = 0 ):
if (n / / i = = i):
divisors[i] + = 1
else :
divisors[i] + = 1
divisors[n / / i] + = 1
def findMaxMultiples(arr, n):
ans = 0
for i in range (n):
ans = max (divisors[arr[i]], ans)
generateDivisors(arr[i])
return ans
arr = [ 8 , 1 , 28 , 4 , 2 , 6 , 7 ]
n = len (arr)
print (findMaxMultiples(arr, n))
|
C#
using System;
class GFG
{
static int MAX = 100000;
static int []divisors = new int [MAX];
static void generateDivisors( int n)
{
for ( int i = 1; i <= Math.Sqrt(n); i++)
{
if (n % i == 0)
{
if (n / i == i)
{
divisors[i]++;
}
else
{
divisors[i]++;
divisors[n / i]++;
}
}
}
}
static int findMaxMultiples( int []arr, int n)
{
int ans = 0;
for ( int i = 0; i < n; i++)
{
ans = Math.Max(divisors[arr[i]], ans);
generateDivisors(arr[i]);
}
return ans;
}
public static void Main(String[] args)
{
int []arr = { 8, 1, 28, 4, 2, 6, 7 };
int n = arr.Length;
Console.Write(findMaxMultiples(arr, n));
}
}
|
Javascript
<script>
const MAX = 100000;
var divisors = new Array(MAX).fill(0);
function generateDivisors(n)
{
for ( var i = 1; i <= Math.sqrt(n); i++)
{
if (n % i == 0)
{
if (n / i == i)
{
divisors[i]++;
}
else
{
divisors[i]++;
divisors[n / i]++;
}
}
}
}
function findMaxMultiples(arr, n)
{
var ans = 0;
for ( var i = 0; i < n; i++)
{
ans = Math.max(divisors[arr[i]], ans);
generateDivisors(arr[i]);
}
return ans;
}
var arr = [ 8, 1, 28, 4, 2, 6, 7 ];
var n = arr.length;
document.write(findMaxMultiples(arr, n));
</script>
|
Time Complexity: O(N*sqrt(val)), where N is the length of the array and val is the maximum value of the array elements.
Auxiliary Space: O(100000), as we are using extra space.
Last Updated :
25 May, 2022
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...