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 – 3Input: 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
// C++ implementation of the approachÂ
#include <bits/stdc++.h>using namespace std;Â
const int MAX = 100000;Â
// Map to store the divisor countint divisors[MAX];Â
// Function to generate the divisors// of all the array elementsint 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]++;Â Â Â Â Â Â Â Â Â Â Â Â }Â Â Â Â Â Â Â Â }Â Â Â Â }}Â
// Function to find the maximum number// of multiples in an array before itint findMaxMultiples(int* arr, int n){    // To store the maximum divisor count    int ans = 0;Â
    for (int i = 0; i < n; i++) {Â
        // Update ans if more number        // of divisors are found        ans = max(divisors[arr[i]], ans);Â
        // Generating all the divisors of        // the next element of the array        generateDivisors(arr[i]);    }    return ans;}Â
// Driver codeint main(){Â Â Â Â int arr[] = { 8, 1, 28, 4, 2, 6, 7 };Â Â Â Â int n = sizeof(arr) / sizeof(int);Â
    cout << findMaxMultiples(arr, n);Â
    return 0;} |
Java
// Java implementation of the approachimport java.util.*;Â
class GFG{Â
static int MAX = 100000;Â
// Map to store the divisor countstatic int []divisors = new int[MAX];Â
// Function to generate the divisors// of all the array elementsstatic 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]++;            }        }    }}Â
// Function to find the maximum number// of multiples in an array before itstatic int findMaxMultiples(int []arr, int n){    // To store the maximum divisor count    int ans = 0;Â
    for (int i = 0; i < n; i++)    {Â
        // Update ans if more number        // of divisors are found        ans = Math.max(divisors[arr[i]], ans);Â
        // Generating all the divisors of        // the next element of the array        generateDivisors(arr[i]);    }    return ans;}Â
// Driver codepublic static void main(String[] args){Â Â Â Â int arr[] = { 8, 1, 28, 4, 2, 6, 7 };Â Â Â Â int n = arr.length;Â
    System.out.print(findMaxMultiples(arr, n));}}Â
// This code is contributed by PrinciRaj1992 |
Python3
# Python3 implementation of the approachfrom math import ceil,sqrtMAX = 100000Â
# Map to store the divisor countdivisors = [0] * MAXÂ
# Function to generate the divisors# of all the array elementsdef 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Â
# Function to find the maximum number# of multiples in an array before itdef findMaxMultiples(arr, n):         # To store the maximum divisor count    ans = 0    for i in range(n):Â
        # Update ans if more number        # of divisors are found        ans = max(divisors[arr[i]], ans)Â
        # Generating all the divisors of        # the next element of the array        generateDivisors(arr[i])    return ansÂ
# Driver codearr = [8, 1, 28, 4, 2, 6, 7]n = len(arr)Â
print(findMaxMultiples(arr, n))Â
# This code is contributed by mohit kumar 29 |
C#
// C# implementation of the approachusing System;Â
class GFG{Â
static int MAX = 100000;Â
// Map to store the divisor countstatic int []divisors = new int[MAX];Â
// Function to generate the divisors// of all the array elementsstatic 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]++;            }        }    }}Â
// Function to find the maximum number// of multiples in an array before itstatic int findMaxMultiples(int []arr, int n){    // To store the maximum divisor count    int ans = 0;Â
    for (int i = 0; i < n; i++)    {Â
        // Update ans if more number        // of divisors are found        ans = Math.Max(divisors[arr[i]], ans);Â
        // Generating all the divisors of        // the next element of the array        generateDivisors(arr[i]);    }    return ans;}Â
// Driver codepublic static void Main(String[] args){Â Â Â Â int []arr = { 8, 1, 28, 4, 2, 6, 7 };Â Â Â Â int n = arr.Length;Â
    Console.Write(findMaxMultiples(arr, n));}}Â
// This code is contributed by 29AjayKumar |
Javascript
<script>Â
// JavaScript implementation of the approachconst MAX = 100000;Â
// Map to store the divisor countvar divisors = new Array(MAX).fill(0);Â
// Function to generate the divisors// of all the array elementsfunction 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 to find the maximum number// of multiples in an array before itfunction findMaxMultiples(arr, n) {         // To store the maximum divisor count    var ans = 0;         for(var i = 0; i < n; i++)    {                 // Update ans if more number        // of divisors are found        ans = Math.max(divisors[arr[i]], ans);                 // Generating all the divisors of        // the next element of the array        generateDivisors(arr[i]);    }    return ans;}Â
// Driver codevar arr = [ 8, 1, 28, 4, 2, 6, 7 ];var n = arr.length;Â
document.write(findMaxMultiples(arr, n));Â
// This code is contributed by rdtankÂ
</script> |
Output:Â
3
Â
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.
Feeling lost in the world of random DSA topics, wasting time without progress? It’s time for a change! Join our DSA course, where we’ll guide you on an exciting journey to master DSA efficiently and on schedule.
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 zambiatek!
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 zambiatek!



