📌  相关文章
📜  给定数组中的元素数,可被其前缀中的所有元素整除

📅  最后修改于: 2022-05-13 01:56:05.655000             🧑  作者: Mango

给定数组中的元素数,可被其前缀中的所有元素整除

给定一个包含N个正整数的数组arr[] ,任务是找出数组中可以被之前所有元素整除的元素总数。

例子:

方法:众所周知,任何数X除以{X 1 , X 2 , X 3 , X 4 , . . ., X n } , 如果X除以{X 1 , X 2 , X 3 , X 4 , ..., X n )LCM 。并且任意数量的LCM A , B[(A*B)/gcd(A, B)] 。现在要解决此问题,请按照以下步骤操作:

  1. 创建一个变量ans来存储最终答案并将其初始化为 0。
  2. 创建另一个变量lcm ,它在遍历数组时将 LCM 存储到第i元素。用arr[0]初始化 lcm 。
  3. i = 1i = N迭代数组,并在每次迭代中检查arr[i]是否除以lcm。如果是,则将ans增加 1。此外,将 lcm 更新为lcm直到第 i元素。
  4. 打印ans作为这个问题的最终答案。

下面是上述方法的实现:

C++
// C++ code to implement the above approach
#include 
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



输出
3

时间复杂度: O(N * logD) 其中 D 是最大数组元素
辅助空间: 在)