Maximum count of elements divisible on the left for any element
Last Updated :
21 Dec, 2022
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: 2
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: 3
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++
#include <bits/stdc++.h>
using namespace std;
int findMax( int arr[], int n)
{
int res = 0;
int i, j;
for (i = 0; i < n ; i++)
{
int count = 0;
for (j = 0; j < i; j++)
{
if (arr[j] % arr[i] == 0)
count += 1;
}
res = max(count, res);
}
return res;
}
int main()
{
int arr[] = { 8, 1, 28, 4, 2, 6, 7 };
int n = sizeof (arr) / sizeof ( int );
cout << findMax(arr, n);
return 0;
}
|
Java
class GFG
{
static int findMax( int arr[], int n)
{
int res = 0 ;
int i, j;
for (i = 0 ; i < n ; i++)
{
int count = 0 ;
for (j = 0 ; j < i; j++)
{
if (arr[j] % arr[i] == 0 )
count += 1 ;
}
res = Math.max(count, res);
}
return res;
}
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));
}
}
|
Python3
def findMax(arr, n):
res = 0
for i in range ( 1 , n):
count = 0
for j in range ( 0 , i):
if arr[j] % arr[i] = = 0 :
count + = 1
res = max (count, res)
return res
arr = [ 8 , 1 , 28 , 4 , 2 , 6 , 7 ]
n = len (arr)
print (findMax(arr, n))
|
C#
using System;
class GFG
{
static int findMax( int []arr, int n)
{
int res = 0;
int i, j;
for (i = 0; i < n ; i++)
{
int count = 0;
for (j = 0; j < i; j++)
{
if (arr[j] % arr[i] == 0)
count += 1;
}
res = Math.Max(count, res);
}
return res;
}
public static void Main (String[] args)
{
int []arr = {8, 1, 28, 4, 2, 6, 7};
int n = arr.Length;
Console.WriteLine(findMax(arr, n));
}
}
|
Javascript
<script>
function findMax(arr, n)
{
var res = 0;
var i, j;
for (i = 0; i < n ; i++)
{
var count = 0;
for (j = 0; j < i; j++)
{
if (arr[j] % arr[i] == 0)
count += 1;
}
res = Math.max(count, res);
}
return res;
}
var arr = [8, 1, 28, 4, 2, 6, 7];
var n = arr.length;
document.write( findMax(arr, n));
</script>
|
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++
#include <bits/stdc++.h>
using namespace std;
int findMax( int arr[], int n)
{
bool divisible[n] = { false };
int res = 0;
for ( int i = n - 1; i > 0; i--) {
if (divisible[i])
continue ;
int cnt = 0;
for ( int j = 0; j < i; j++) {
if ((arr[j] % arr[i]) == 0) {
divisible[j] = true ;
cnt++;
}
}
res = max(res, cnt);
}
return res;
}
int main()
{
int arr[] = { 8, 1, 28, 4, 2, 6, 7 };
int n = sizeof (arr) / sizeof ( int );
cout << findMax(arr, n);
return 0;
}
|
Java
class GFG
{
static int findMax( int arr[], int n)
{
boolean []divisible = new boolean [n];
int res = 0 ;
for ( int i = n - 1 ; i > 0 ; i--)
{
if (divisible[i])
continue ;
int cnt = 0 ;
for ( int j = 0 ; j < i; j++)
{
if ((arr[j] % arr[i]) == 0 )
{
divisible[j] = true ;
cnt++;
}
}
res = Math.max(res, cnt);
}
return res;
}
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));
}
}
|
Python3
def findMax(arr, n) :
divisible = [ False ] * n;
res = 0 ;
for i in range (n - 1 , - 1 , - 1 ) :
if (divisible[i]) :
continue ;
cnt = 0 ;
for j in range (i) :
if ((arr[j] % arr[i]) = = 0 ) :
divisible[j] = True ;
cnt + = 1 ;
res = max (res, cnt);
return res;
if __name__ = = "__main__" :
arr = [ 8 , 1 , 28 , 4 , 2 , 6 , 7 ];
n = len (arr);
print (findMax(arr, n));
|
C#
using System;
class GFG
{
static int findMax( int []arr, int n)
{
bool []divisible = new bool [n];
int res = 0;
for ( int i = n - 1; i > 0; i--)
{
if (divisible[i])
continue ;
int cnt = 0;
for ( int j = 0; j < i; j++)
{
if ((arr[j] % arr[i]) == 0)
{
divisible[j] = true ;
cnt++;
}
}
res = Math.Max(res, cnt);
}
return res;
}
public static void Main(String[] args)
{
int []arr = { 8, 1, 28, 4, 2, 6, 7 };
int n = arr.Length;
Console.WriteLine(findMax(arr, n));
}
}
|
Javascript
<script>
function findMax(arr, n)
{
var divisible = Array(n).fill( false );
var res = 0;
for ( var i = n - 1; i > 0; i--) {
if (divisible[i])
continue ;
var cnt = 0;
for ( var j = 0; j < i; j++) {
if ((arr[j] % arr[i]) == 0) {
divisible[j] = true ;
cnt++;
}
}
res = Math.max(res, cnt);
}
return res;
}
var arr = [8, 1, 28, 4, 2, 6, 7];
var n = arr.length;
document.write( findMax(arr, n));
</script>
|
Time Complexity: O(n2)
Auxiliary Space: O(n), where n is the size of the given array.
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...