Open In App

Maximum count of elements divisible on the left for any element

Last Updated : 21 Dec, 2022
Improve
Improve
Like Article
Like
Save
Share
Report

Given an array arr[] of N elements. The good value of an element arr[i] is the number of valid indices j<i such that arr[j] is divisible by arr[i].
Example: 
 

Input: arr[] = {9, 6, 2, 3} 
Output:
9 doesn’t has any element on its left. 
6 doesn’t divide any element on its left. 
2 divides 6. 
3 divides 6 and 9.
Input: arr[] = {8, 1, 28, 4, 2, 6, 7} 
Output:

Naive approach: For every element, find the count of numbers divisible by it on its left and print the maximum of these values.
Below is the implementation of the above approach: 
 

C++




// C++ implementation of the approach
#include <bits/stdc++.h>
using namespace std;
 
// Function to return the maximum count
// of required elements
int findMax(int arr[], int n)
{
    int res = 0;
    int i, j;
     
    // For every element in the array starting
    // from the second element
    for(i = 0; i < n ; i++)
    {
         
        // Check all the elements on the left
        // of current element which are divisible
        // by the current element
        int count = 0;
        for(j = 0; j < i; j++)
        {
            if (arr[j] % arr[i] == 0)
                count += 1;
        }
        res = max(count, res);
    }
    return res;
}
 
// Driver code
int main()
{
    int arr[] = { 8, 1, 28, 4, 2, 6, 7 };
    int n = sizeof(arr) / sizeof(int);
 
    cout << findMax(arr, n);
 
    return 0;
}
 
// This code is contributed by Rajput-Ji


Java




// Java implementation of the approach
class GFG
{
 
// Function to return the maximum count
// of required elements
static int findMax(int arr[], int n)
{
    int res = 0;
    int i, j;
     
    // For every element in the array starting
    // from the second element
    for(i = 0; i < n ; i++)
    {
         
        // Check all the elements on the left
        // of current element which are divisible
        // by the current element
        int count = 0;
        for(j = 0; j < i; j++)
        {
            if (arr[j] % arr[i] == 0)
                count += 1;
        }
        res = Math.max(count, res);
    }
    return res;
}
 
// Driver Code
public static void main (String[] args)
{
    int arr[] = {8, 1, 28, 4, 2, 6, 7};
    int n = arr.length;
    System.out.println(findMax(arr, n));
}
}
 
// This code is contributed by AnkitRai01


Python3




# Python3 implementation of the approach
 
# Function to return the maximum count
# of required elements
def findMax(arr, n):
    res = 0
     
    # For every element in the array starting
    # from the second element
    for i in range(1, n):
         
        # Check all the elements on the left
        # of current element which are divisible
        # by the current element
        count = 0
        for j in range(0, i):
            if arr[j] % arr[i] == 0:
                count += 1
        res = max(count, res)
    return res
 
# Driver code
arr = [8, 1, 28, 4, 2, 6, 7]
n = len(arr)
print(findMax(arr, n))


C#




// C# implementation of the above approach
using System;
     
class GFG
{
 
// Function to return the maximum count
// of required elements
static int findMax(int []arr, int n)
{
    int res = 0;
    int i, j;
     
    // For every element in the array 
    // starting from the second element
    for(i = 0; i < n ; i++)
    {
         
        // Check all the elements on the left
        // of current element which are divisible
        // by the current element
        int count = 0;
        for(j = 0; j < i; j++)
        {
            if (arr[j] % arr[i] == 0)
                count += 1;
        }
        res = Math.Max(count, res);
    }
    return res;
}
 
// Driver Code
public static void Main (String[] args)
{
    int []arr = {8, 1, 28, 4, 2, 6, 7};
    int n = arr.Length;
    Console.WriteLine(findMax(arr, n));
}
}
 
// This code is contributed by PrinciRaj1992


Javascript




<script>
 
// JavaScript implementation of the approach
 
// Function to return the maximum count
// of required elements
function findMax(arr, n)
{
    var res = 0;
    var i, j;
     
    // For every element in the array starting
    // from the second element
    for(i = 0; i < n ; i++)
    {
         
        // Check all the elements on the left
        // of current element which are divisible
        // by the current element
        var count = 0;
        for(j = 0; j < i; j++)
        {
            if (arr[j] % arr[i] == 0)
                count += 1;
        }
        res = Math.max(count, res);
    }
    return res;
}
 
// Driver code
var arr = [8, 1, 28, 4, 2, 6, 7];
var n = arr.length;
document.write( findMax(arr, n));
 
</script>


Output: 

3

 

Time Complexity: O(N2)
Auxiliary Space: O(1), no extra space is required, so it is a constant.
Efficient approach: It can be observed that for any element pair (arr[i], arr[j]) where i < j and (arr[i] % arr[j]) = 0, if the count of elements divisible by arr[i] on its left is X then the count of elements divisible by arr[j] on its left will definitely be greater than X as all the elements which are divisible by arr[i] will also be divisible by arr[j]. So, for every element which is divisible by any other element on its right, the count of elements on its left which are divisible by it doesn’t need to be calculated which will improve the time complexity of the overall program but it should be noted that for the input where no element is divisible by any other element (for example, when all the elements are prime), the worst-case time complexity would still be O(N2).
Below is the implementation of the above approach: 
 

C++




// C++ implementation of the approach
#include <bits/stdc++.h>
using namespace std;
 
// Function to return the maximum count
// of required elements
int findMax(int arr[], int n)
{
 
    // divisible[i] will store true
    // if arr[i] is divisible by
    // any element on its right
    bool divisible[n] = { false };
 
    // To store the maximum required count
    int res = 0;
 
    // For every element of the array
    for (int i = n - 1; i > 0; i--) {
 
        // If the current element is
        // divisible by any element
        // on its right
        if (divisible[i])
            continue;
 
        // Find the count of element
        // on the left which are divisible
        // by the current element
        int cnt = 0;
        for (int j = 0; j < i; j++) {
 
            // If arr[j] is divisible then
            // set divisible[j] to true
            if ((arr[j] % arr[i]) == 0) {
                divisible[j] = true;
                cnt++;
            }
        }
 
        // Update the maximum required count
        res = max(res, cnt);
    }
 
    return res;
}
 
// Driver code
int main()
{
    int arr[] = { 8, 1, 28, 4, 2, 6, 7 };
    int n = sizeof(arr) / sizeof(int);
 
    cout << findMax(arr, n);
 
    return 0;
}


Java




// Java implementation of the approach
class GFG
{
 
// Function to return the maximum count
// of required elements
static int findMax(int arr[], int n)
{
 
    // divisible[i] will store true
    // if arr[i] is divisible by
    // any element on its right
    boolean []divisible = new boolean[n];
 
    // To store the maximum required count
    int res = 0;
 
    // For every element of the array
    for (int i = n - 1; i > 0; i--)
    {
 
        // If the current element is
        // divisible by any element
        // on its right
        if (divisible[i])
            continue;
 
        // Find the count of element
        // on the left which are divisible
        // by the current element
        int cnt = 0;
        for (int j = 0; j < i; j++)
        {
 
            // If arr[j] is divisible then
            // set divisible[j] to true
            if ((arr[j] % arr[i]) == 0)
            {
                divisible[j] = true;
                cnt++;
            }
        }
 
        // Update the maximum required count
        res = Math.max(res, cnt);
    }
    return res;
}
 
// Driver code
public static void main(String[] args)
{
    int arr[] = { 8, 1, 28, 4, 2, 6, 7 };
    int n = arr.length;
 
    System.out.println(findMax(arr, n));
}
}
 
// This code is contributed by Rajput-Ji


Python3




# Python3 implementation of the approach
 
# Function to return the maximum count
# of required elements
def findMax(arr, n) :
 
    # divisible[i] will store true
    # if arr[i] is divisible by
    # any element on its right
    divisible = [ False ] * n;
 
    # To store the maximum required count
    res = 0;
 
    # For every element of the array
    for i in range(n - 1, -1, -1) :
 
        # If the current element is
        # divisible by any element
        # on its right
        if (divisible[i]) :
            continue;
 
        # Find the count of element
        # on the left which are divisible
        # by the current element
        cnt = 0;
        for j in range(i) :
 
            # If arr[j] is divisible then
            # set divisible[j] to true
            if ((arr[j] % arr[i]) == 0) :
                divisible[j] = True;
                cnt += 1;
 
        # Update the maximum required count
        res = max(res, cnt);
 
    return res;
 
# Driver code
if __name__ == "__main__" :
 
    arr = [ 8, 1, 28, 4, 2, 6, 7 ];
    n = len(arr);
 
    print(findMax(arr, n));
 
# This code is contributed by kanugargng


C#




// C# implementation of the approach
using System;
 
class GFG
{
 
// Function to return the maximum count
// of required elements
static int findMax(int []arr, int n)
{
 
    // divisible[i] will store true
    // if arr[i] is divisible by
    // any element on its right
    bool []divisible = new bool[n];
 
    // To store the maximum required count
    int res = 0;
 
    // For every element of the array
    for (int i = n - 1; i > 0; i--)
    {
 
        // If the current element is
        // divisible by any element
        // on its right
        if (divisible[i])
            continue;
 
        // Find the count of element
        // on the left which are divisible
        // by the current element
        int cnt = 0;
        for (int j = 0; j < i; j++)
        {
 
            // If arr[j] is divisible then
            // set divisible[j] to true
            if ((arr[j] % arr[i]) == 0)
            {
                divisible[j] = true;
                cnt++;
            }
        }
 
        // Update the maximum required count
        res = Math.Max(res, cnt);
    }
    return res;
}
 
// Driver code
public static void Main(String[] args)
{
    int []arr = { 8, 1, 28, 4, 2, 6, 7 };
    int n = arr.Length;
 
    Console.WriteLine(findMax(arr, n));
}
}
 
// This code is contributed by Rajput-Ji


Javascript




<script>
 
// JavaScript implementation of the approach
 
// Function to return the maximum count
// of required elements
function findMax(arr, n)
{
 
    // divisible[i] will store true
    // if arr[i] is divisible by
    // any element on its right
    var divisible = Array(n).fill(false);
 
    // To store the maximum required count
    var res = 0;
 
    // For every element of the array
    for (var i = n - 1; i > 0; i--) {
 
        // If the current element is
        // divisible by any element
        // on its right
        if (divisible[i])
            continue;
 
        // Find the count of element
        // on the left which are divisible
        // by the current element
        var cnt = 0;
        for (var j = 0; j < i; j++) {
 
            // If arr[j] is divisible then
            // set divisible[j] to true
            if ((arr[j] % arr[i]) == 0) {
                divisible[j] = true;
                cnt++;
            }
        }
 
        // Update the maximum required count
        res = Math.max(res, cnt);
    }
 
    return res;
}
 
// Driver code
var arr = [8, 1, 28, 4, 2, 6, 7];
var n = arr.length;
document.write( findMax(arr, n));
 
</script>


Output: 

3

 

Time Complexity: O(n2)
Auxiliary Space: O(n), where n is the size of the given array.



Similar Reads

Count of elements on the left which are divisible by current element
Given an array A[] of N integers, the task is to generate an array B[] such that B[i] contains the count of indices j in A[] such that j &lt; i and A[j] % A[i] = 0Examples: Input: arr[] = {3, 5, 1} Output: 0 0 2 3 and 5 do not divide any element on their left but 1 divides 3 and 5.Input: arr[] = {8, 1, 28, 4, 2, 6, 7} Output: 0 1 0 2 3 0 1 Approach
5 min read
Count of elements on the left which are divisible by current element | Set 2
Given an array A[] of N integers, the task is to generate an array B[] such that B[i] contains the count of indices j in A[] such that j &lt; i and A[j] % A[i] = 0Examples: Input: arr[] = {3, 5, 1} Output: 0 0 2 Explanation: 3 and 5 do not divide any element on their left but 1 divides 3 and 5.Input: arr[] = {8, 1, 28, 4, 2, 6, 7} Output: 0 1 0 2 3
7 min read
Count of elements not divisible by any other elements of Array
Given an array arr[], the task is to determine the number of elements of the array which are not divisible by any other element in the given array. Examples: Input: arr[] = {86, 45, 18, 4, 8, 28, 19, 33, 2} Output: 4 Explanation: The elements are {2, 19, 33, 45} are not divisible by any other array element. Input: arr[] = {3, 3, 3} Output: 0 Naive
13 min read
Count number of pairs not divisible by any element in the array
Given an array arr[] of size N, the task is to count the number of pairs of integers (i, j) for which there does not exist an integer k such that arr[i] is divisible by arr[k] and arr[j] is divisible by arr[k], such that k can be any index between [0, N - 1]. Examples: Input: N = 4, arr[] = {2, 4, 5, 7}Output: 5Explanation: The 5 pairs are: (0, 2):
5 min read
Count of Array elements greater than all elements on its left and next K elements on its right
Given an array arr[], the task is to print the number of elements which are greater than all the elements on its left as well as greater than the next K elements on its right. Examples: Input: arr[] = { 4, 2, 3, 6, 4, 3, 2}, K = 2 Output: 2 Explanation: arr[0](= 4): arr[0] is the 1st element in the array and greater than its next K(= 2) elements {2
14 min read
C++ Program to Count of Array elements greater than all elements on its left and at least K elements on its right
Given an array A[ ] consisting of N distinct integers, the task is to find the number of elements which are strictly greater than all the elements preceding it and strictly greater than at least K elements on its right. Examples: Input: A[] = {2, 5, 1, 7, 3, 4, 0}, K = 3 Output: 2 Explanation: The only array elements satisfying the given conditions
9 min read
Java Program to Count of Array elements greater than all elements on its left and at least K elements on its right
Given an array A[ ] consisting of N distinct integers, the task is to find the number of elements which are strictly greater than all the elements preceding it and strictly greater than at least K elements on its right. Examples: Input: A[] = {2, 5, 1, 7, 3, 4, 0}, K = 3 Output: 2 Explanation: The only array elements satisfying the given conditions
7 min read
Count of Array elements greater than all elements on its left and at least K elements on its right
Given an array A[ ] consisting of N distinct integers, the task is to find the number of elements which are strictly greater than all the elements preceding it and strictly greater than at least K elements on its right. Examples: Input: A[] = {2, 5, 1, 7, 3, 4, 0}, K = 3 Output: 2 Explanation: The only array elements satisfying the given conditions
29 min read
Elements of an array that are not divisible by any element of another array
Given two arrays A[] and B[], write an efficient code to determine if every element of B[] is divisible by at least 1 element of A[]. Display those elements of B[], which are not divisible by any of the elements in A[]. Examples : Input : A[] = {100, 200, 400, 100, 600} B[] = {45, 90, 48, 1000, 3000} Output : 45, 90, 48 The output elements are thos
10 min read
Maximum Subarray sum of A[] by adding any element of B[] at any end
Given two arrays A[] and B[] having lengths N and M respectively, Where A[] and B[] can contain both positive and negative elements, the task is to find the maximum sub-array sum obtained by applying the below operations till the B[] has no element left in it: Choose either the leftmost or rightmost element from B[] and add it to any end of A[].Del
15 min read