给定一个包含N 个元素的数组arr[] ,任务是找到数组中具有素数频率的元素的 GCD。请注意, 1既不是质数也不是合数。
例子:
Input: arr[] = {5, 4, 6, 5, 4, 6}
Output: 1
All the elements appear 2 times which is a prime
So, gcd(5, 4, 6) = 1
Input: arr[] = {4, 8, 8, 1, 4, 3, 0}
Output: 4
方法:
- 遍历数组并将所有元素的频率存储在地图中。
- 构建 Eratosthenes 筛,用于在 O(1) 时间内测试数字的素性。
- 使用在上一步中计算的 Sieve 数组计算具有素数频率的元素的 gcd。
下面是上述方法的实现:
C++
// C++ implementation of the approach
#include
using namespace std;
// Function to create Sieve to check primes
void SieveOfEratosthenes(bool prime[], int p_size)
{
// False here indicates
// that it is not prime
prime[0] = false;
prime[1] = false;
for (int p = 2; p * p <= p_size; p++) {
// If prime[p] is not changed,
// then it is a prime
if (prime[p]) {
// Update all multiples of p,
// set them to non-prime
for (int i = p * 2; i <= p_size; i += p)
prime[i] = false;
}
}
}
// Function to return the GCD of elements
// in an array having prime frequency
int gcdPrimeFreq(int arr[], int n)
{
bool prime[n + 1];
memset(prime, true, sizeof(prime));
SieveOfEratosthenes(prime, n + 1);
int i, j;
// Map is used to store
// element frequencies
unordered_map m;
for (i = 0; i < n; i++)
m[arr[i]]++;
int gcd = 0;
// Traverse the map using iterators
for (auto it = m.begin(); it != m.end(); it++) {
// Count the number of elements
// having prime frequencies
if (prime[it->second]) {
gcd = __gcd(gcd, it->first);
}
}
return gcd;
}
// Driver code
int main()
{
int arr[] = { 5, 4, 6, 5, 4, 6 };
int n = sizeof(arr) / sizeof(arr[0]);
cout << gcdPrimeFreq(arr, n);
return 0;
}
Java
// Java implementation of the approach
import java.util.*;
class GFG
{
// Function to create Sieve to check primes
static void SieveOfEratosthenes(boolean prime[],
int p_size)
{
// False here indicates
// that it is not prime
prime[0] = false;
prime[1] = false;
for (int p = 2; p * p <= p_size; p++)
{
// If prime[p] is not changed,
// then it is a prime
if (prime[p])
{
// Update all multiples of p,
// set them to non-prime
for (int i = p * 2;
i <= p_size; i += p)
prime[i] = false;
}
}
}
// Function to return the GCD of elements
// in an array having prime frequency
static int gcdPrimeFreq(int arr[], int n)
{
boolean []prime = new boolean[n + 1];
for (int i = 0; i < n + 1; i++)
prime[i] = true;
SieveOfEratosthenes(prime, n + 1);
int i, j;
// Map is used to store
// element frequencies
HashMap mp = new HashMap();
for (i = 0 ; i < n; i++)
{
if(mp.containsKey(arr[i]))
{
mp.put(arr[i], mp.get(arr[i]) + 1);
}
else
{
mp.put(arr[i], 1);
}
}
int gcd = 0;
// Traverse the map using iterators
for (Map.Entry it : mp.entrySet())
{
// Count the number of elements
// having prime frequencies
if (prime[it.getValue()])
{
gcd = __gcd(gcd, it.getKey());
}
}
return gcd;
}
static int __gcd(int a, int b)
{
if (b == 0)
return a;
return __gcd(b, a % b);
}
// Driver code
static public void main ( String []arg)
{
int arr[] = { 5, 4, 6, 5, 4, 6 };
int n = arr.length;
System.out.println(gcdPrimeFreq(arr, n));
}
}
// This code is contributed by Rajput-Ji
Python3
# Python3 implementation of the approach
from math import sqrt, gcd
# Function to create Sieve to check primes
def SieveOfEratosthenes(prime, p_size) :
# False here indicates
# that it is not prime
prime[0] = False;
prime[1] = False;
for p in range(2, int(sqrt(p_size)) + 1) :
# If prime[p] is not changed,
# then it is a prime
if (prime[p]) :
# Update all multiples of p,
# set them to non-prime
for i in range(2 * p, p_size, p) :
prime[i] = False;
# Function to return the GCD of elements
# in an array having prime frequency
def gcdPrimeFreq(arr, n) :
prime = [True] * (n + 1);
SieveOfEratosthenes(prime, n + 1);
# Map is used to store
# element frequencies
m = dict.fromkeys(arr, 0);
for i in range(n) :
m[arr[i]] += 1;
__gcd = 0;
# Traverse the map using iterators
for key,value in m.items() :
# Count the number of elements
# having prime frequencies
if (prime[value]) :
__gcd = gcd(__gcd, key);
return __gcd;
# Driver code
if __name__ == "__main__" :
arr = [ 5, 4, 6, 5, 4, 6 ];
n = len(arr);
print(gcdPrimeFreq(arr, n));
# This code is contributed by AnkitRai01
C#
// C# implementation of the approach
using System;
using System.Collections.Generic;
class GFG
{
// Function to create Sieve to check primes
static void SieveOfEratosthenes(bool []prime,
int p_size)
{
// False here indicates
// that it is not prime
prime[0] = false;
prime[1] = false;
for (int p = 2; p * p <= p_size; p++)
{
// If prime[p] is not changed,
// then it is a prime
if (prime[p])
{
// Update all multiples of p,
// set them to non-prime
for (int i = p * 2;
i <= p_size; i += p)
prime[i] = false;
}
}
}
// Function to return the GCD of elements
// in an array having prime frequency
static int gcdPrimeFreq(int []arr, int n)
{
int i;
bool []prime = new bool[n + 1];
for (i = 0; i < n + 1; i++)
prime[i] = true;
SieveOfEratosthenes(prime, n + 1);
// Map is used to store
// element frequencies
Dictionary mp = new Dictionary();
for (i = 0 ; i < n; i++)
{
if(mp.ContainsKey(arr[i]))
{
var val = mp[arr[i]];
mp.Remove(arr[i]);
mp.Add(arr[i], val + 1);
}
else
{
mp.Add(arr[i], 1);
}
}
int gcd = 0;
// Traverse the map using iterators
foreach(KeyValuePair it in mp)
{
// Count the number of elements
// having prime frequencies
if (prime[it.Value])
{
gcd = __gcd(gcd, it.Key);
}
}
return gcd;
}
static int __gcd(int a, int b)
{
if (b == 0)
return a;
return __gcd(b, a % b);
}
// Driver code
static public void Main ( String []arg)
{
int []arr = { 5, 4, 6, 5, 4, 6 };
int n = arr.Length;
Console.WriteLine(gcdPrimeFreq(arr, n));
}
}
// This code is contributed by Princi Singh
Javascript
输出:
1
如果您希望与专家一起参加现场课程,请参阅DSA 现场工作专业课程和学生竞争性编程现场课程。