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

📅  最后修改于: 2021-04-29 18:21:30             🧑  作者: Mango

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

例子:

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

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

高效的方法:可以通过使用Eratosthenes筛子,标记数字的所有倍数并将它们存储在有效的数据结构(例如Set中)中来优化上述方法,Set可以在几乎恒定的时间内提供查找操作。请按照以下步骤解决问题:

  • 首先,对于每个数组元素(例如arr [i]) ,使用Eratosthenes的Sieve将所有小于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


输出:
6

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