查找给定 Array 中出现次数最多和出现次数最少的元素的 GCD
给定一个大小为n的数组arr[] ,任务是找到给定数组中最高和最低频率元素的 GCD。
例子:
Input: arr[] = {2, 2, 4, 4, 5, 5, 6, 6, 6, 6}
Output: 2
Explanation: The frequency of the elements in the above array is
freq(2) = 2,
freq(4) = 2,
freq(5) = 2,
freq(6) = 4,
The minimum frequency is 2 (of elements 2, 4, and 5). So 2 will be picked as the least among 2, 4, and 5.
The largest frequency is 4 (of element 6).
Hence GCD of 2 and 6 = gcd(2, 6) is 2.
Input: arr[] = {3, 2, 2, 44, 44, 44, 44}
Output: 1
方法:这个想法是计算所有元素的频率并将它们存储在一个向量中,然后找到出现次数最多和出现次数最少的元素的 gcd。
下面是上述方法的实现:
C++14
// C++ program for the above approach
#include
using namespace std;
// Function to find frequency and find gcd
int find_gcd(vector v, int n)
{
map mp;
// Update the frequency
for (int i = 0; i < n; i++) {
mp[v[i]]++;
}
int mini = v[0], maxi = v[0];
for (auto it : mp) {
mini = mp[mini] < it.second
? it.first
: mini;
}
for (auto it : mp) {
maxi = mp[maxi] > it.second
? it.first
: maxi;
}
// Find gcd
int res = __gcd(mini, maxi);
return res;
}
// Drive Code
int main()
{
vector v = { 2, 2, 4, 4, 5, 5, 6, 6, 6, 6 };
int n = v.size();
cout << find_gcd(v, n) << endl;
vector v1 = { 3, 2, 2, 44, 44, 44, 44 };
cout << find_gcd(v1, v1.size()) << endl;
return 0;
}
Java
// Java program for the above approach
import java.util.*;
class GFG{
// Function to find frequency and find gcd
static int find_gcd(int []v, int n)
{
HashMap mp = new HashMap();
// Update the frequency
for (int i = 0; i < n; i++) {
if(mp.containsKey(v[i])){
mp.put(v[i], mp.get(v[i])+1);
}
else{
mp.put(v[i], 1);
}
}
int mini = v[0], maxi = v[0];
for (Map.Entry it : mp.entrySet()) {
mini = mp.get(mini) < it.getValue()
? it.getKey()
: mini;
}
for (Map.Entry it : mp.entrySet()) {
maxi = mp.get(maxi) > it.getValue()
? it.getKey()
: maxi;
}
// Find gcd
int res = __gcd(mini, maxi);
return res;
}
static int __gcd(int a, int b)
{
return b == 0? a:__gcd(b, a % b);
}
// Drive Code
public static void main(String[] args)
{
int [] v = { 2, 2, 4, 4, 5, 5, 6, 6, 6, 6 };
int n = v.length;
System.out.print(find_gcd(v, n) +"\n");
int [] v1 = { 3, 2, 2, 44, 44, 44, 44 };
System.out.print(find_gcd(v1, v1.length) +"\n");
}
}
// This code is contributed by shikhasingrajput
Python3
# python program for the above approach
import math
# Function to find frequency and find gcd
def find_gcd(v, n):
mp = {}
# Update the frequency
for i in range(0, n):
if v[i] in mp:
mp[v[i]] += 1
else:
mp[v[i]] = 1
mini = v[0]
maxi = v[0]
for it in mp:
if mini in mp and mp[mini] < mp[it]:
mini = it
for it in mp:
if mp[maxi] > mp[it]:
maxi = it
# Find gcd
res = math.gcd(mini, maxi)
return res
# Drive Code
if __name__ == "__main__":
v = [2, 2, 4, 4, 5, 5, 6, 6, 6, 6]
n = len(v)
print(find_gcd(v, n))
v1 = [3, 2, 2, 44, 44, 44, 44]
print(find_gcd(v1, len(v1)))
# This code is contributed by rakeshsahni
C#
// C# program for the above approach
using System;
using System.Collections.Generic;
class GFG {
static int __gcd(int a, int b)
{
return b == 0 ? a : __gcd(b, a % b);
}
// Function to find frequency and find gcd
static int find_gcd(int[] v, int n)
{
Dictionary mp
= new Dictionary();
// Update the frequency
for (int i = 0; i < n; i++) {
if (mp.ContainsKey(v[i])) {
mp[v[i]] += 1;
}
else {
mp[v[i]] = 1;
}
}
int mini = v[0], maxi = v[0];
foreach(KeyValuePair it in mp)
{
mini = mp[mini] < it.Value ? it.Key : mini;
}
foreach(KeyValuePair it in mp)
{
maxi = mp[maxi] > it.Value ? it.Key : maxi;
}
// Find gcd
int res = __gcd(mini, maxi);
return res;
}
// Drive Code
public static void Main()
{
int[] v = { 2, 2, 4, 4, 5, 5, 6, 6, 6, 6 };
int n = v.Length;
Console.WriteLine(find_gcd(v, n));
int[] v1 = { 3, 2, 2, 44, 44, 44, 44 };
Console.WriteLine(find_gcd(v1, v1.Length));
}
}
// This code is contributed by ukasp.
Javascript
输出:
2
1
时间复杂度: O(n*log(n))
辅助空间: O(n)