📌  相关文章
📜  查找 [L, R] 之间可被所有 Array 元素整除的数字

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

查找 [L, R] 之间可被所有 Array 元素整除的数字

给定一个数组arr[] ,其中包含N个正整数和两个变量LR ,表示从LR (含)的整数范围。任务是打印LR之间的所有数字,这些数字可以被所有数组元素整除。如果不存在这样的值,则打印 -1。

朴素方法:在这种方法中,对于范围[L, R]中的每个元素,检查它是否可以被数组的所有元素整除。

时间复杂度: O((RL)*N)
辅助空间: O(1)

有效的方法:给定的问题可以用基本的数学来解决。任何可被数组的所有元素整除的元素都是所有数组元素的 LCM 的倍数。在 [L, R] 范围内找到 LCM 的倍数并存储在数组中。最后打印存储在数组中的数字。

时间复杂度: O(N)
辅助空间: O(R – L)

空间优化方法:可以使用以下步骤来解决问题:

  1. 计算给定 arr[] 的所有元素的 LCM
  2. 现在,检查LCM的这些条件:
    1. 如果(LCM < L and LCM*2 > R) ,则打印 -1。
    2. 如果(LCM > R) ,则打印 -1。
  3. 现在,取L的最接近的值(在LR之间),它可以被LCM整除,比如i
  4. 现在,开始打印i并在每次打印后将其递增LCM ,直到它变得大于R

下面是上述方法的实现:

C++
// C++ program for the above approach
#include 
using namespace std;
 
// Function to return Kth smallest
// prime number if it exists
void solve(int* arr, int N, int L, int R)
{
    // For storing the LCM
    int LCM = arr[0];
 
    // Loop to iterate the array
    for (int i = 1; i < N; i++) {
        // Taking LCM of numbers
        LCM = (LCM * arr[i]) /
            (__gcd(LCM, arr[i]));
    }
 
    // Checking if no elements is divisible
    // by all elements of given array of given
    // range, print -1
    if ((LCM < L && LCM * 2 > R) || LCM > R) {
        cout << "-1";
        return;
    }
 
    // Taking nearest value of L which is
    // divisible by whole array
    int k = (L / LCM) * LCM;
 
    // If k is less than L, make it in the
    // range between L to R
    if (k < L)
        k = k + LCM;
 
    // Loop to iterate the from L to R
    // and printing the numbers which
    // are divisible by all array elements
    for (int i = k; i <= R; i = i + LCM) {
        cout << i << ' ';
    }
}
 
// Driver Code
int main()
{
    int L = 90;
    int R = 280;
    int arr[] = { 3, 5, 12 };
    int N = sizeof(arr) / sizeof(arr[0]);
    solve(arr, N, L, R);
    return 0;
}


Java
// Java program for the above approach
import java.util.*;
 
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 Kth smallest
// prime number if it exists
static void solve(int[] arr, int N, int L, int R)
{
     
    // For storing the LCM
    int LCM = arr[0];
 
    // Loop to iterate the array
    for(int i = 1; i < N; i++)
    {
         
        // Taking LCM of numbers
        LCM = (LCM * arr[i]) /
        (__gcd(LCM, arr[i]));
    }
 
    // Checking if no elements is divisible
    // by all elements of given array of given
    // range, print -1
    if ((LCM < L && LCM * 2 > R) || LCM > R)
    {
        System.out.println("-1");
        return;
    }
 
    // Taking nearest value of L which is
    // divisible by whole array
    int k = (L / LCM) * LCM;
 
    // If k is less than L, make it in the
    // range between L to R
    if (k < L)
        k = k + LCM;
 
    // Loop to iterate the from L to R
    // and printing the numbers which
    // are divisible by all array elements
    for(int i = k; i <= R; i = i + LCM)
    {
        System.out.print(i + " ");
    }
}
 
// Driver Code
public static void main(String args[])
{
    int L = 90;
    int R = 280;
    int arr[] = { 3, 5, 12 };
    int N = arr.length;
     
    solve(arr, N, L, R);
}
}
 
// This code is contributed by sanjoy_62


Python3
# Python program 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 Kth smallest
# prime number if it exists
def solve(arr, N, L, R):
   
    # For storing the LCM
    LCM = arr[0];
 
    # Loop to iterate the array
    for i in range(1, N):
       
        # Taking LCM of numbers
        LCM = (LCM * arr[i]) // (__gcd(LCM, arr[i]));
 
    # Checking if no elements is divisible
    # by all elements of given array of given
    # range, pr-1
    if ((LCM < L and LCM * 2 > R) or LCM > R):
        print("-1");
        return;
 
    # Taking nearest value of L which is
    # divisible by whole array
    k = (L // LCM) * LCM;
 
    # If k is less than L, make it in the
    # range between L to R
    if (k < L):
        k = k + LCM;
 
    # Loop to iterate the from L to R
    # and printing the numbers which
    # are divisible by all array elements
    for i in range(k,R+1,LCM):
        print(i, end=" ");
 
# Driver Code
if __name__ == '__main__':
    L = 90;
    R = 280;
    arr = [3, 5, 12];
    N = len(arr);
 
    solve(arr, N, L, R);
 
# This code is contributed by 29AjayKumar


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 Kth smallest
  // prime number if it exists
  static void solve(int[] arr, int N, int L, int R)
  {
 
    // For storing the LCM
    int LCM = arr[0];
 
    // Loop to iterate the array
    for(int i = 1; i < N; i++)
    {
 
      // Taking LCM of numbers
      LCM = (LCM * arr[i]) /
        (__gcd(LCM, arr[i]));
    }
 
    // Checking if no elements is divisible
    // by all elements of given array of given
    // range, print -1
    if ((LCM < L && LCM * 2 > R) || LCM > R)
    {
      Console.WriteLine("-1");
      return;
    }
 
    // Taking nearest value of L which is
    // divisible by whole array
    int k = (L / LCM) * LCM;
 
    // If k is less than L, make it in the
    // range between L to R
    if (k < L)
      k = k + LCM;
 
    // Loop to iterate the from L to R
    // and printing the numbers which
    // are divisible by all array elements
    for(int i = k; i <= R; i = i + LCM)
    {
      Console.Write(i + " ");
    }
  }
 
  // Driver Code
  public static void Main(String []args)
  {
    int L = 90;
    int R = 280;
    int []arr = { 3, 5, 12 };
    int N = arr.Length;
 
    solve(arr, N, L, R);
  }
}
 
// This code is contributed by 29AjayKumar


Javascript


输出
120 180 240 

时间复杂度: O(N)
辅助空间: O(1)