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;
// Function to return total number of
// elements which are divisible by
// all their previous elements
int countElements(int arr[], int N)
{
int ans = 0; // Initialize the count of elements to zero
for (int i = 1; i < N;
i++) { // Traverse the array from index 1 to N-1
bool is_divisible
= true; // Initialize the flag to true
for (int j = 0; j < i; j++) { // Traverse the array
// from index 0 to i-1
if (arr[i] % arr[j]
!= 0) { // Check if the i-th element is not
// divisible by any of the previous
// elements
is_divisible
= false; // Set the flag to false
break; // Break out of the inner loop
}
}
if (is_divisible) { // If the i-th element is
// divisible by all its previous
// elements
ans++; // Increment the count of elements
}
}
return ans; // Return the count of elements that are
// divisible by all their previous elements
}
// Driver code
int main()
{
int arr[] = {
10, 6, 60, 120, 30, 360
}; // Initialize the input array
int N
= sizeof(arr)
/ sizeof(
int); // Calculate the size of the input array
cout << countElements(
arr, N); // Call the countElements() function and
// print the result
return 0;
}
Java
import java.util.*;
public class Main {
// Function to return total number of
// elements which are divisible by
// all their previous elements
static int countElements(int[] arr, int N)
{
int ans
= 0; // Initialize the count of elements to zero
for (int i = 1; i < N; i++) { // Traverse the array
// from index 1 to N-1
boolean is_divisible
= true; // Initialize the flag to true
for (int j = 0; j < i;
j++) { // Traverse the array from index 0
// to i-1
if (arr[i] % arr[j]
!= 0) { // Check if the i-th element is
// not divisible by any of the
// previous elements
is_divisible
= false; // Set the flag to false
break; // Break out of the inner loop
}
}
if (is_divisible) { // If the i-th element is
// divisible by all its
// previous elements
ans++; // Increment the count of elements
}
}
return ans; // Return the count of elements that are
// divisible by all their previous
// elements
}
// Driver code
public static void main(String[] args)
{
int[] arr = {
10, 6, 60, 120, 30, 360
}; // Initialize the input array
int N = arr.length; // Calculate the size of the
// input array
System.out.println(countElements(
arr, N)); // Call the countElements() function
// and print the result
}
}
Python3
def count_elements(arr):
ans = 0 # Initialize the count of elements to zero
N = len(arr) # Calculate the size of the input array
for i in range(1, N): # Traverse the array from index 1 to N-1
is_divisible = True # Initialize the flag to true
for j in range(i): # Traverse the array from index 0 to i-1
if arr[i] % arr[j] != 0: # Check if the i-th element is not divisible by any of the previous elements
is_divisible = False # Set the flag to false
break # Break out of the inner loop
if is_divisible: # If the i-th element is divisible by all its previous elements
ans += 1 # Increment the count of elements
return ans # Return the count of elements that are divisible by all their previous elements
# Driver code
arr = [10, 6, 60, 120, 30, 360] # Initialize the input array
print(count_elements(arr)) # Call the count_elements() function and print the result
C#
// C# program to count the number of elements which are
// divisible by all their previous elements.
using System;
class MainClass
{
// Function to return total number of
// elements which are divisible by
// all their previous elements
static int countElements(int[] arr, int N)
{
int ans
= 0; // Initialize the count of elements to zero
for (int i = 1; i < N; i++) { // Traverse the array
// from index 1 to N-1
bool is_divisible
= true; // Initialize the flag to true
for (int j = 0; j < i;
j++) { // Traverse the array from index 0
// to i-1
if (arr[i] % arr[j]
!= 0) { // Check if the i-th element is
// not divisible by any of the
// previous elements
is_divisible
= false; // Set the flag to false
break; // Break out of the inner loop
}
}
if (is_divisible) { // If the i-th element is
// divisible by all its
// previous elements
ans++; // Increment the count of elements
}
}
return ans; // Return the count of elements that are
// divisible by all their previous
// elements
}
// Driver code
public static void Main()
{
int[] arr = {
10, 6, 60, 120, 30, 360
}; // Initialize the input array
int N = arr.Length; // Calculate the size of the
// input array
Console.WriteLine(countElements(
arr, N)); // Call the countElements() function
// and print the result
}
}
JavaScript
// Function to return total number of
// elements which are divisible by
// all their previous elements
function countElements(arr, N) {
let ans = 0;// Initialize the count of elements to zero
// Traverse the array
// from index 1 to N-1
for (let i = 1; i < N; i++) {
let is_divisible = true; // Initialize the flag to true
// Traverse the array from index 0
// to i-1
for (let j = 0; j < i; j++) {
// Check if the i-th element is
// not divisible by any of the
// previous elements
if (arr[i] % arr[j] !== 0) {
is_divisible = false; // Set the flag to false
break; // Break out of the inner loop
}
}
// If the i-th element is
// divisible by all its
// previous elements
if (is_divisible) {
ans++;// Increment the count of elements
}
}
// Return the count of elements that are
// divisible by all their previous
// elements
return ans;
}
// Usage
let arr = [10, 6, 60, 120, 30, 360];
let N = arr.length;
console.log(countElements(arr, N)); // Output: count of elements that are divisible by all previous elements in the array
// This code is contributed by shivhack999
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++
// C++ code to implement the above approach
#include <bits/stdc++.h>
using namespace std;
// Function to return total number of
// elements which are divisible by
// all their previous elements
int countElements(int arr[], int N)
{
int ans = 0;
int lcm = arr[0];
for (int i = 1; i < N; i++) {
// To check if number is divisible
// by lcm of all previous elements
if (arr[i] % lcm == 0) {
ans++;
}
// Updating LCM
lcm = (lcm * arr[i]) / __gcd(lcm, arr[i]);
}
return ans;
}
// Driver code
int main()
{
int arr[] = { 10, 6, 60, 120, 30, 360 };
int N = sizeof(arr) / sizeof(int);
cout << countElements(arr, N);
return 0;
}
Java
// Java program for the above approach
import java.util.*;
public class GFG {
// Recursive function to return gcd of a and b
static int __gcd(int a, int b)
{
// Everything divides 0
if (a == 0)
return b;
if (b == 0)
return a;
// base case
if (a == b)
return a;
// a is greater
if (a > b)
return __gcd(a - b, b);
return __gcd(a, b - a);
}
// Function to return total number of
// elements which are divisible by
// all their previous elements
static int countElements(int arr[], int N)
{
int ans = 0;
int lcm = arr[0];
for (int i = 1; i < N; i++) {
// To check if number is divisible
// by lcm of all previous elements
if (arr[i] % lcm == 0) {
ans++;
}
// Updating LCM
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));
}
}
// This code is contributed by Samim Hossain Mondal.
Python3
# Python code for the above approach
# Recursive function to return gcd of a and b
def __gcd(a, b):
# Everything divides 0
if (a == 0):
return b;
if (b == 0):
return a;
# base case
if (a == b):
return a;
# a is greater
if (a > b):
return __gcd(a - b, b);
return __gcd(a, b - a);
# Function to return total number of
# elements which are divisible by
# all their previous elements
def countElements(arr, N):
ans = 0;
lcm = arr[0];
for i in range(1, N):
# To check if number is divisible
# by lcm of all previous elements
if (arr[i] % lcm == 0):
ans += 1
# Updating LCM
lcm = (lcm * arr[i]) / __gcd(lcm, arr[i]);
return ans;
# Driver code
arr = [10, 6, 60, 120, 30, 360];
N = len(arr)
print(countElements(arr, N));
# This code is contributed by Saurabh Jaiswal
C#
// C#program for the above approach
using System;
public class GFG {
// Recursive function to return gcd of a and b
static int __gcd(int a, int b)
{
// Everything divides 0
if (a == 0)
return b;
if (b == 0)
return a;
// base case
if (a == b)
return a;
// a is greater
if (a > b)
return __gcd(a - b, b);
return __gcd(a, b - a);
}
// Function to return total number of
// elements which are divisible by
// all their previous elements
static int countElements(int[] arr, int N)
{
int ans = 0;
int lcm = arr[0];
for (int i = 1; i < N; i++) {
// To check if number is divisible
// by lcm of all previous elements
if (arr[i] % lcm == 0) {
ans++;
}
// Updating LCM
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));
}
}
// This code is contributed by ukasp.
JavaScript
<script>
// JavaScript code for the above approach
// Recursive function to return gcd of a and b
function __gcd(a, b) {
// Everything divides 0
if (a == 0)
return b;
if (b == 0)
return a;
// base case
if (a == b)
return a;
// a is greater
if (a > b)
return __gcd(a - b, b);
return __gcd(a, b - a);
}
// Function to return total number of
// elements which are divisible by
// all their previous elements
function countElements(arr, N) {
let ans = 0;
let lcm = arr[0];
for (let i = 1; i < N; i++) {
// To check if number is divisible
// by lcm of all previous elements
if (arr[i] % lcm == 0) {
ans++;
}
// Updating LCM
lcm = (lcm * arr[i]) / __gcd(lcm, arr[i]);
}
return ans;
}
// Driver code
let arr = [10, 6, 60, 120, 30, 360];
let N = arr.length
document.write(countElements(arr, N));
// This code is contributed by Potta Lokesh
</script>
Time complexity: O(N * logD) where D is the maximum array element
Auxiliary Space: O(N)
Similar Reads
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
5 min read
Count numbers from a given range that are not divisible by any of the array elements
Given an array arr[] consisting of N positive integers and integers L and R, the task is to find the count of numbers in the range [L, R] which are not divisible by any of the array elements. Examples: Input: arr[] = {2, 3, 4, 5, 6}, L = 1, R = 20Output: 6Explanation:The 6 numbers in the range [1, 2
7 min read
Count elements that are divisible by at-least one element in another array
Given two arrays arr1[] and arr2[]. The task is to find the count of such elements in the first array whose at-least one factor is present in the second array.Examples: Input : arr1[] = {10, 2, 13, 4, 15} ; arr2[] = {2, 4, 5, 6} Output : 4 There is no factor of 13 which is present in the second arra
8 min read
Sum of all the elements in an array divisible by a given number K
Given an array containing N elements and a number K. The task is to find the sum of all such elements which are divisible by K. Examples: Input : arr[] = {15, 16, 10, 9, 6, 7, 17} K = 3 Output : 30 Explanation: As 15, 9, 6 are divisible by 3. So, sum of elements divisible by K = 15 + 9 + 6 = 30. Inp
13 min read
Count the number of elements in an array which are divisible by k
Given an array of integers. The task is to calculate the count of a number of elements which are divisible by a given number k. Examples: Input: arr[] = { 2, 6, 7, 12, 14, 18 }, k = 3 Output: 3 Numbers which are divisible by k are { 6, 12, 18 } Input: arr[] = { 2, 6, 7, 12, 14, 18 }, k = 2 Output: 5
6 min read
Count array elements that divide the sum of all other elements
Given an array arr[], the task is to count the number of elements from the array which divide the sum of all other elements. Examples: Input: arr[] = {3, 10, 4, 6, 7} Output: 3 3 divides (10 + 4 + 6 + 7) i.e. 27 10 divides (3 + 4 + 6 + 7) i.e. 20 6 divides (3 + 10 + 4 + 7) i.e. 24 Input: arr[] = {1,
9 min read
Count of integers that divide all the elements of the given array
Given an array arr[] of N elements. The task is to find the count of positive integers that divide all the array elements. Examples: Input: arr[] = {2, 8, 10, 6} Output: 2 1 and 2 are the only integers that divide all the elements of the given array. Input: arr[] = {6, 12, 18, 12, 6} Output: 4 Appro
5 min read
Count all distinct pairs of repeating elements from the array for every array element
Given an array arr[] of N integers. For each element in the array, the task is to count the possible pairs (i, j), excluding the current element, such that i < j and arr[i] = arr[j]. Examples: Input: arr[] = {1, 1, 2, 1, 2}Output: 2 2 3 2 3Explanation:For index 1, remaining elements after excludi
7 min read
Find an array element such that all elements are divisible by it
Given an array of numbers, find the number among them such that all numbers are divisible by it. If not possible print -1. Examples: Input : arr = {25, 20, 5, 10, 100} Output : 5 Explanation : 5 is an array element which divides all numbers. Input : arr = {9, 3, 6, 2, 15} Output : -1 Explanation : N
8 min read
Count all prefixes of the given binary array which are divisible by x
Given a binary array arr[] and an integer x, the task is to count all the prefixes of the given array which are divisible by x. Note: The ith prefix from arr[0] to arr[i] is interpreted as a binary number (from most-significant-bit to least-significant-bit.)Examples: Input: arr[] = {0, 1, 0, 1, 1},
10 min read