📌  相关文章
📜  计算给定范围内不能被任何数组元素整除的数字

📅  最后修改于: 2021-09-04 08:30:01             🧑  作者: Mango

给定一个由N 个正整数和整数LR组成的数组arr[] ,任务是找到范围[L, R]中不能被任何数组元素整除的数字的计数。

例子:

朴素的方法:简单的方法是遍历给定范围[L, R]中的所有数字, 并且对于每个数字,检查它是否可以被任何数组元素整除。如果它不能被任何数组元素整除,则增加计数。检查所有数字后,打印计数。

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

有效的方法:上述方法可以通过使用埃拉托色尼筛法进行优化,标记一个数字的所有倍数并将它们存储在一个有效的数据结构中,比如 Set,它在几乎恒定的时间内提供查找操作。请按照以下步骤解决问题:

  • 首先,对于每个数组元素,比如arr[i] ,使用 Eratosthenes 筛将其所有小于R 的倍数存储在一个 Set 中。
  • 范围[1, R]中不能被给定数组中存在的任何数字整除的整数的数量将等于(R – 集合的大小) 。让它成为A
  • 类似地,找到范围[1, L]中不能被给定数组中存在的任何数字整除的数字。让它成为B
  • 完成以上步骤后,打印(A-B)的值作为结果。

下面是上述方法的实现:

C++
// C++ program for the above approach
 
#include 
using namespace std;
 
// Function to find the non-multiples
// till k
int findNonMultiples(int arr[],
                     int n, int k)
{
    // Stores all unique multiples
    set multiples;
 
    // Iterate the array
    for (int i = 0; i < n; ++i) {
 
        // For finding duplicates
        // only once
        if (multiples.find(arr[i])
            == multiples.end()) {
 
            // Inserting all multiples
            // into the set
            for (int j = 1;
                 j <= k / arr[i]; j++) {
                multiples.insert(arr[i] * j);
            }
        }
    }
 
    // Returning only the count of
    // numbers that are not divisible
    // by any of the array elements
    return k - multiples.size();
}
 
// Function to count the total values
// in the range [L, R]
int countValues(int arr[], int N,
                int L, int R)
{
    // Count all values in the range
    // using exclusion principle
    return findNonMultiples(arr, N, R)
           - findNonMultiples(arr, N, L - 1);
}
 
// Driver Code
int main()
{
    int arr[] = { 2, 3, 4, 5, 6 };
    int N = sizeof(arr) / sizeof(arr[0]);
    int L = 1, R = 20;
 
    // Function Call
    cout << countValues(arr, N, L, R);
 
    return 0;
}


Java
// Java program for the above approach
import java.io.*;
import java.util.*;
 
class GFG{
 
// Function to find the non-multiples
// till k
public static int findNonMultiples(int[] arr, int n,
                                   int k)
{
     
    // Stores all unique multiples
    Set multiples = new HashSet();
     
    // Iterate the array
    for(int i = 0; i < n; ++i)
    {
         
        // For finding duplicates
        // only once
        if (!multiples.contains(arr[i]))
        {
             
            // Inserting all multiples
            // into the set
            for(int j = 1; j <= k / arr[i]; j++)
            {
                multiples.add(arr[i] * j);
            }
        }
    }
     
    // Returning only the count of
    // numbers that are not divisible
    // by any of the array elements
    return k - multiples.size();
}
 
// Function to count the total values
// in the range [L, R]
public static int countValues(int[] arr, int N,
                              int L, int R)
{
     
    // Count all values in the range
    // using exclusion principle
    return findNonMultiples(arr, N, R) -
           findNonMultiples(arr, N, L - 1);
}
 
// Driver code
public static void main(String[] args)
{
    int[] arr = { 2, 3, 4, 5, 6 };
    int N = arr.length;
    int L = 1;
    int R = 20;
 
    // Function Call
    System.out.println(countValues(arr, N, L, R));
}
}
 
// This code is contributed by rohitsingh07052


Python3
# Python3 program for the above approach
 
# Function to find the non-multiples
# till k
def findNonMultiples(arr, n, k):
 
    # Stores all unique multiples
    multiples = set([])
 
    # Iterate the array
    for i in range(n):
 
        # For finding duplicates
        # only once
        if (arr[i] not in multiples):
 
            # Inserting all multiples
            # into the set
            for j in range(1, k // arr[i] + 1):
                multiples.add(arr[i] * j)
 
    # Returning only the count of
    # numbers that are not divisible
    # by any of the array elements
    return k - len(multiples)
 
# Function to count the total values
# in the range [L, R]
def countValues(arr, N, L, R):
 
    # Count all values in the range
    # using exclusion principle
    return (findNonMultiples(arr, N, R) -
            findNonMultiples(arr, N, L - 1))
 
# Driver Code
if __name__ == "__main__":
   
    arr = [ 2, 3, 4, 5, 6 ]
    N = len(arr)
    L = 1
    R = 20
     
    # Function Call
    print( countValues(arr, N, L, R))
 
# This code is contributed by chitranayal


C#
// C# program for the above approach
using System;
using System.Collections.Generic;
class GFG
{
 
// Function to find the non-multiples
// till k
public static int findNonMultiples(int[] arr, int n,
                                   int k)
{
     
    // Stores all unique multiples
    HashSet multiples = new HashSet();
     
    // Iterate the array
    for(int i = 0; i < n; ++i)
    {
         
        // For finding duplicates
        // only once
        if (!multiples.Contains(arr[i]))
        {
             
            // Inserting all multiples
            // into the set
            for(int j = 1; j <= k / arr[i]; j++)
            {
                multiples.Add(arr[i] * j);
            }
        }
    }
     
    // Returning only the count of
    // numbers that are not divisible
    // by any of the array elements
    return k - multiples.Count;
}
 
// Function to count the total values
// in the range [L, R]
public static int countValues(int[] arr, int N,
                              int L, int R)
{
     
    // Count all values in the range
    // using exclusion principle
    return findNonMultiples(arr, N, R) -
           findNonMultiples(arr, N, L - 1);
}
 
// Driver code
public static void Main(String[] args)
{
    int[] arr = { 2, 3, 4, 5, 6 };
    int N = arr.Length;
    int L = 1;
    int R = 20;
 
    // Function Call
    Console.WriteLine(countValues(arr, N, L, R));
}
}
 
// This code is contributed by shikhasingrajput


Javascript


输出:
6

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

如果您想与行业专家一起参加直播课程,请参阅Geeks Classes Live