Skip to content
Related Articles

Related Articles

Improve Article
Save Article
Like Article

Count of elements in given Array divisible by all elements in their prefix

  • Last Updated : 24 Jan, 2022

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.

 

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:

  1. Create a variable ans which stores the final answer and initialize it with 0.
  2. Create another variable lcm which stores LCM up to the ith element while iterating through the array. Initialize lcm with arr[0].
  3. 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.
  4. 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>

 
 

Output
3

 

Time complexity: O(N * logD) where D is the maximum array element
Auxiliary Space: O(N)

 


My Personal Notes arrow_drop_up
Recommended Articles
Page :

Start Your Coding Journey Now!