Count of elements in given Array divisible by all elements in their prefix
Last Updated :
10 Apr, 2023
Given an array arr[] containing N positive integers, the task is to find the total number of elements in the array that are divisible by all the elements present before them.
Examples:
Input: arr[] = {10, 6, 60, 120, 30, 360}
Output: 3
Explanation: 60, 120 and 360 are the required elements.
Input: arr[] = {2, 6, 5, 60}
Output: 2
Explanation: 6 and 60 are the elements.
Brute Force/ Naive Approach:
The brute force approach to solving the problem of Count of elements in given Array divisible by all elements in their prefix would be to check each element of the array one by one and calculate if it’s divisible by all its previous elements.
Steps to implement the approach:
- Initialize a variable ans to 0 to count the number of elements that satisfy the condition.
- Traverse the array from index 1 to N-1.
- For each index i, traverse the array from index 0 to i-1 to check if the element is divisible by all its previous elements.
- If the element is divisible by all its previous elements, increment the ans variable.
- Return the ans variable as the final count.
Below is the implementation of the approach:
C++
#include <bits/stdc++.h>
using namespace std;
int countElements( int arr[], int N)
{
int ans = 0;
for ( int i = 1; i < N;
i++) {
bool is_divisible
= true ;
for ( int j = 0; j < i; j++) {
if (arr[i] % arr[j]
!= 0) {
is_divisible
= false ;
break ;
}
}
if (is_divisible) {
ans++;
}
}
return ans;
}
int main()
{
int arr[] = {
10, 6, 60, 120, 30, 360
};
int N
= sizeof (arr)
/ sizeof (
int );
cout << countElements(
arr, N);
return 0;
}
|
Java
import java.util.*;
public class Main {
static int countElements( int [] arr, int N)
{
int ans
= 0 ;
for ( int i = 1 ; i < N; i++) {
boolean is_divisible
= true ;
for ( int j = 0 ; j < i;
j++) {
if (arr[i] % arr[j]
!= 0 ) {
is_divisible
= false ;
break ;
}
}
if (is_divisible) {
ans++;
}
}
return ans;
}
public static void main(String[] args)
{
int [] arr = {
10 , 6 , 60 , 120 , 30 , 360
};
int N = arr.length;
System.out.println(countElements(
arr, N));
}
}
|
Python3
def count_elements(arr):
ans = 0
N = len (arr)
for i in range ( 1 , N):
is_divisible = True
for j in range (i):
if arr[i] % arr[j] ! = 0 :
is_divisible = False
break
if is_divisible:
ans + = 1
return ans
arr = [ 10 , 6 , 60 , 120 , 30 , 360 ]
print (count_elements(arr))
|
C#
using System;
class MainClass
{
static int countElements( int [] arr, int N)
{
int ans
= 0;
for ( int i = 1; i < N; i++) {
bool is_divisible
= true ;
for ( int j = 0; j < i;
j++) {
if (arr[i] % arr[j]
!= 0) {
is_divisible
= false ;
break ;
}
}
if (is_divisible) {
ans++;
}
}
return ans;
}
public static void Main()
{
int [] arr = {
10, 6, 60, 120, 30, 360
};
int N = arr.Length;
Console.WriteLine(countElements(
arr, N));
}
}
|
Javascript
function countElements(arr, N) {
let ans = 0;
for (let i = 1; i < N; i++) {
let is_divisible = true ;
for (let j = 0; j < i; j++) {
if (arr[i] % arr[j] !== 0) {
is_divisible = false ;
break ;
}
}
if (is_divisible) {
ans++;
}
}
return ans;
}
let arr = [10, 6, 60, 120, 30, 360];
let N = arr.length;
console.log(countElements(arr, N));
|
Time Complexity: O(N2 )
Space Complexity: O(1)
Another Approach: As known, that any number X is divided by {X1, X2, X3, X4, . . ., Xn}, if X is divided by LCM of {X1, X2, X3, X4, …, Xn). And LCM of any number A, B is [(A*B)/gcd(A, B)]. Now to solve this problem, follow the below steps:
- Create a variable ans which stores the final answer and initializes it with 0.
- Create another variable lcm which stores LCM up to the ith element while iterating through the array. Initialize lcm with arr[0].
- Iterate over the array from i = 1 to i = N, and in each iteration, check if arr[i] is divided by lcm. If yes increment ans by 1. Also, update lcm with lcm up to ith element.
- Print ans as the final answer to this problem.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
int countElements( int arr[], int N)
{
int ans = 0;
int lcm = arr[0];
for ( int i = 1; i < N; i++) {
if (arr[i] % lcm == 0) {
ans++;
}
lcm = (lcm * arr[i]) / __gcd(lcm, arr[i]);
}
return ans;
}
int main()
{
int arr[] = { 10, 6, 60, 120, 30, 360 };
int N = sizeof (arr) / sizeof ( int );
cout << countElements(arr, N);
return 0;
}
|
Java
import java.util.*;
public class GFG {
static int __gcd( int a, int b)
{
if (a == 0 )
return b;
if (b == 0 )
return a;
if (a == b)
return a;
if (a > b)
return __gcd(a - b, b);
return __gcd(a, b - a);
}
static int countElements( int arr[], int N)
{
int ans = 0 ;
int lcm = arr[ 0 ];
for ( int i = 1 ; i < N; i++) {
if (arr[i] % lcm == 0 ) {
ans++;
}
lcm = (lcm * arr[i]) / __gcd(lcm, arr[i]);
}
return ans;
}
public static void main(String args[])
{
int arr[] = { 10 , 6 , 60 , 120 , 30 , 360 };
int N = arr.length;
System.out.print(countElements(arr, N));
}
}
|
Python3
def __gcd(a, b):
if (a = = 0 ):
return b;
if (b = = 0 ):
return a;
if (a = = b):
return a;
if (a > b):
return __gcd(a - b, b);
return __gcd(a, b - a);
def countElements(arr, N):
ans = 0 ;
lcm = arr[ 0 ];
for i in range ( 1 , N):
if (arr[i] % lcm = = 0 ):
ans + = 1
lcm = (lcm * arr[i]) / __gcd(lcm, arr[i]);
return ans;
arr = [ 10 , 6 , 60 , 120 , 30 , 360 ];
N = len (arr)
print (countElements(arr, N));
|
C#
using System;
public class GFG {
static int __gcd( int a, int b)
{
if (a == 0)
return b;
if (b == 0)
return a;
if (a == b)
return a;
if (a > b)
return __gcd(a - b, b);
return __gcd(a, b - a);
}
static int countElements( int [] arr, int N)
{
int ans = 0;
int lcm = arr[0];
for ( int i = 1; i < N; i++) {
if (arr[i] % lcm == 0) {
ans++;
}
lcm = (lcm * arr[i]) / __gcd(lcm, arr[i]);
}
return ans;
}
public static void Main()
{
int [] arr = { 10, 6, 60, 120, 30, 360 };
int N = arr.Length;
Console.Write(countElements(arr, N));
}
}
|
Javascript
<script>
function __gcd(a, b) {
if (a == 0)
return b;
if (b == 0)
return a;
if (a == b)
return a;
if (a > b)
return __gcd(a - b, b);
return __gcd(a, b - a);
}
function countElements(arr, N) {
let ans = 0;
let lcm = arr[0];
for (let i = 1; i < N; i++) {
if (arr[i] % lcm == 0) {
ans++;
}
lcm = (lcm * arr[i]) / __gcd(lcm, arr[i]);
}
return ans;
}
let arr = [10, 6, 60, 120, 30, 360];
let N = arr.length
document.write(countElements(arr, N));
</script>
|
Time complexity: O(N * logD) where D is the maximum array element
Auxiliary Space: O(N)
Share your thoughts in the comments
Please Login to comment...