📌  相关文章
📜  查找范围 [L, R] 中与给定 Array 元素互质的数字

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

查找范围 [L, R] 中与给定 Array 元素互质的数字

给定一个由N个不同的正整数和一个范围[L, R ] 组成的数组arr[] ,任务是在给定的范围[L, R]中找到与所有数组元素互质的元素。

例子:

方法:给定的问题可以通过将每个数组元素的所有除数存储在 HashSet 中来解决。假设有另一个HashSet ,比如说S[L, R]范围内的数字组成,现在任务是删除从这个 HashSet S计算的除数的倍数以获得结果数字。请按照以下步骤解决问题:

  • 将每个数组元素的除数存储在无序集中,例如S
  • [L, R]范围内的所有值存储在另一个 HashSet 中,例如M
  • 遍历无序集合S并且对于S中的每个元素,如果value存在于M中,则从集合M中删除所有value的倍数。
  • 完成上述步骤后,将 HashSet M中存储的所有元素打印为所需的结果数。

下面是上述方法的实现:

C++
// C++ program for the above approach
#include 
using namespace std;
 
// Function to find all the elements in
// the range [L, R] which are co prime
// with all array elements
void elementsCoprimeWithArr(
    int A[], int N, int L, int R)
{
    // Store all the divisors of array
    // element in S
    unordered_set S;
 
    // Find the divisors
    for (int i = 0; i < N; i++) {
 
        int curr_ele = A[i];
 
        for (int j = 1;
             j <= sqrt(curr_ele) + 1; j++) {
            if (curr_ele % j == 0) {
                S.insert(j);
                S.insert(curr_ele / j);
            }
        }
    }
 
    // Stores all possible required number
    // satisfying the given criteria
    unordered_set store;
 
    // Insert all element [L, R]
    for (int i = L; i <= R; i++)
        store.insert(i);
 
    S.erase(1);
 
    // Traverse the set
    for (auto it : S) {
 
        int ele = it;
 
        int index = 1;
 
        // Remove the multiples of ele
        while (index * ele <= R) {
            store.erase(index * ele);
            index++;
        }
    }
 
    // Print the resultant numbers
    for (auto i : store) {
        cout << i << " ";
    }
}
 
// Driver Code
int main()
{
    int arr[] = { 3, 5 };
    int L = 1, R = 10;
    int N = sizeof(arr) / sizeof(arr[0]);
 
    elementsCoprimeWithArr(arr, N, L, R);
 
    return 0;
}


Java
// Java program for the above approach
import java.util.*;
 
class GFG
{
 
// Function to find all the elements in
// the range [L, R] which are co prime
// with all array elements
static void elementsCoprimeWithArr(
    int A[], int N, int L, int R)
{
   
    // Store all the divisors of array
    // element in S
    HashSet S = new HashSet();
 
    // Find the divisors
    for (int i = 0; i < N; i++) {
 
        int curr_ele = A[i];
 
        for (int j = 1;
             j <= Math.sqrt(curr_ele) + 1; j++) {
            if (curr_ele % j == 0) {
                S.add(j);
                S.add(curr_ele / j);
            }
        }
    }
 
    // Stores all possible required number
    // satisfying the given criteria
    HashSet store= new HashSet();
 
    // Insert all element [L, R]
    for (int i = L; i <= R; i++)
        store.add(i);
 
    S.remove(1);
 
    // Traverse the set
    for (int it : S) {
 
        int ele = it;
 
        int index = 1;
 
        // Remove the multiples of ele
        while (index * ele <= R) {
            store.remove(index * ele);
            index++;
        }
    }
 
    // Print the resultant numbers
    for (int i : store) {
        System.out.print(i+ " ");
    }
}
 
// Driver Code
public static void main(String[] args)
{
    int arr[] = { 3, 5 };
    int L = 1, R = 10;
    int N = arr.length;
 
    elementsCoprimeWithArr(arr, N, L, R);
}
}
 
// This code is contributed by shikhasingrajput


Python3
# python 3 program for the above approach
import math
 
# Function to find all the elements in
# the range [L, R] which are co prime
# with all array elements
def elementsCoprimeWithArr(
        A, N, L, R):
 
    # Store all the divisors of array
    # element in S
    S = []
     
    # Find the divisors
    for i in range(N):
 
        curr_ele = A[i]
 
        for j in range(1, (int)(math.sqrt(curr_ele)) + 1):
            if (curr_ele % j == 0):
                S.append(j)
                S.append(curr_ele // j)
 
    # Stores all possible required number
    # satisfying the given criteria
    store = []
    S = set(S)
 
    # Insert all element [L, R]
    for i in range(L, R+1):
        store.append(i)
 
    S.remove(1)
 
    # / Traverse the set
    for it in S:
 
        ele = it
 
        index = 1
 
        # Remove the multiples of ele
        while (index * ele <= R):
            
            store.remove(index * ele)
            index += 1
 
    # Print the resultant numbers
    for i in store:
        print(i, end=" ")
 
# Driver Code
if __name__ == "__main__":
 
    arr = [3, 5]
    L = 1
    R = 10
    N = len(arr)
 
    elementsCoprimeWithArr(arr, N, L, R)
 
    # This code is contributed by ukasp.


C#
// C# program for the above approach
using System;
using System.Collections.Generic;
 
public class GFG
{
 
// Function to find all the elements in
// the range [L, R] which are co prime
// with all array elements
static void elementsCoprimeWithArr(
    int []A, int N, int L, int R)
{
   
    // Store all the divisors of array
    // element in S
    HashSet S = new HashSet();
 
    // Find the divisors
    for (int i = 0; i < N; i++) {
 
        int curr_ele = A[i];
 
        for (int j = 1;
             j <= Math.Sqrt(curr_ele) + 1; j++) {
            if (curr_ele % j == 0) {
                S.Add(j);
                S.Add(curr_ele / j);
            }
        }
    }
 
    // Stores all possible required number
    // satisfying the given criteria
    HashSet store= new HashSet();
 
    // Insert all element [L, R]
    for (int i = L; i <= R; i++)
        store.Add(i);
 
    S.Remove(1);
 
    // Traverse the set
    foreach (int it in S) {
 
        int ele = it;
 
        int index = 1;
 
        // Remove the multiples of ele
        while (index * ele <= R) {
            store.Remove(index * ele);
            index++;
        }
    }
 
    // Print the resultant numbers
    foreach (int i in store) {
        Console.Write(i+ " ");
    }
}
 
// Driver Code
public static void Main(String[] args)
{
    int []arr = { 3, 5 };
    int L = 1, R = 10;
    int N = arr.Length;
 
    elementsCoprimeWithArr(arr, N, L, R);
}
}
 
// This code is contributed by shikhasingrajput


输出:
8 7 2 1 4

时间复杂度: O(N*sqrt(M)),其中 M 是最大数组元素
辅助空间: O(max(R – L, N))