查找其值等于其频率的所有 Array 数的 GCD
给定一个整数N的数组arr[] ,任务是找到其值等于其在数组中的频率的所有数字的 GCD。
例子;
Input: arr={2, 3, 4, 2, 3, 3} N=6
Output: 1
Explanation: Here 2 is occuring 2 times and 3 is occuring 3 times.
Hence GCD of 2, 3, is 1. So our output is 1.
Input: arr={2, 3, 4, 4, 3, 5, 4, 4, 2, 8} N=10
Output: 2
Explanation: Here 2 is occuring 2 times, 4 is occuring 4 times.
HenceGCD of 2, 4, is 2. So our output is 2.
方法:解决问题的思路如下:
Find the elements whose value is equal to its frequency. Then calculate the GCD of those number.
请按照以下步骤解决问题:
- 找出数组中所有数字的频率。
- 从数组中找到频率与其值相同的数字并存储它们。
- 计算这些数字的 GCD。
- 返回GCD 。
下面是上述方法的实现:
C++
// C++ code to implement the approach
#include
using namespace std;
// Function to find GCD
int findGcd(int arr[], int n)
{
vector v;
// Declaration of map
unordered_map m1;
for (int i = 0; i < n; i++) {
m1[arr[i]]++;
}
unordered_map::iterator it;
for (it = m1.begin(); it != m1.end();
++it) {
if (it->second == it->first)
// Pushing superior numbers
// into vector.
v.push_back(it->first);
}
int hcf;
if (v.size() == 0)
return 0;
else if (v.size() == 1)
return v[0];
else if (v.size() == 2) {
hcf = __gcd(v[0], v[1]);
return hcf;
}
else if (v.size() > 2) {
hcf = __gcd(v[0], v[1]);
for (int i = 2; i < v.size(); i++) {
hcf = __gcd(v[i], hcf);
}
return hcf;
}
return 1;
}
// Driver Code
int main()
{
int N = 10;
int arr[N] = { 2, 3, 4, 4, 3,
5, 4, 4, 2, 8 };
// Function call
cout << findGcd(arr, N);
return 0;
}
Java
// Java code to implement the approach
import java.io.*;
import java.util.*;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Map.Entry;
class GFG {
public static int gcd(int a, int b)
{
if (b == 0)
return a;
return gcd(b, a % b);
}
// Function to find GCD
public static int findGcd(int arr[], int n)
{
ArrayList v = new ArrayList();
// Declaration of map
HashMap m1
= new HashMap();
for (int i = 0; i < n; i++) {
if (m1.get(arr[i]) != null)
m1.put(arr[i], m1.get(arr[i]) + 1);
else
m1.put(arr[i], 1);
}
Iterator > iter
= m1.entrySet().iterator();
// Iterating every set of entry in the HashMap
while (iter.hasNext()) {
Map.Entry it
= (Map.Entry)iter.next();
if (it.getValue() == it.getKey())
// Pushing superior numbers
// into vector.
v.add(it.getKey());
}
int hcf = 0;
if (v.size() == 0)
return 0;
else if (v.size() == 1)
return v.get(0);
else if (v.size() == 2) {
hcf = gcd(v.get(0), v.get(1));
return hcf;
}
else if (v.size() > 2) {
hcf = gcd(v.get(0), v.get(1));
for (int i = 2; i < v.size(); i++) {
hcf = gcd(v.get(i), hcf);
}
return hcf;
}
return 1;
}
public static void main(String[] args)
{
int N = 10;
int arr[] = { 2, 3, 4, 4, 3, 5, 4, 4, 2, 8 };
// Function call
System.out.print(findGcd(arr, N));
}
}
// This code is contributed by Rohit Pradhan
Python3
# Python3 code to implement the approach
import math
# Function to find GCD
def findGcd(arr, n):
v = []
# Declaration of map/dictionary
m1 = dict()
for i in range(n):
if arr[i] in m1:
m1[arr[i]] += 1
else:
m1[arr[i]] = 1
for it in m1:
if m1[it] == it:
v.append(it)
if len(v) == 0:
return 0
elif len(v) == 1:
return v[0]
elif len(v) == 2:
hcf = math.gcd(v[0], v[1])
return hcf
elif len(v) > 2:
hcf = math.gcd(v[0], v[1])
for i in range(2, len(v)):
hcf = math.gcd(hcf, v[i])
return hcf
return 1
# driver code
N = 10
arr = [2, 3, 4, 4, 3, 5, 4, 4, 2, 8]
# function call
print(findGcd(arr, N))
# This code is contributed by phasing17.
输出
2
时间复杂度: O(N 2 )
辅助空间: O(N)