📌  相关文章
📜  将数组划分为最少数量的由单个不同值组成的等长子集

📅  最后修改于: 2021-09-04 09:31:18             🧑  作者: Mango

给定一个大小为N的数组arr[] ,任务是打印该数组可以划分成的等长子集的最小计数,使得每个子集只包含一个不同的元素

例子:

朴素方法:解决问题的最简单方法是存储每个不同数组元素的频率,使用变量i在范围[N, 1] 上迭代,并检查数组中所有不同元素的频率是否可被整除与否。如果发现为真,则打印(N / i)的值。

时间复杂度: O(N 2 )。
辅助空间: O(N)

高效的方法:优化上述方法的想法是使用 GCD 的概念。请按照以下步骤解决问题:

  • 初始化一个映射,比如freq ,以存储数组中每个不同元素的频率。
  • 初始化一个变量,比如FreqGCD ,以存储数组中每个不同元素的频率的 GCD。
  • 遍历地图以找到FreqGCD的值。
  • 最后,打印(N) % FreqGCD的值。

下面是上述方法的实现:

C++
// C++ program to implement
// the above approach
 
#include 
using namespace std;
 
// Function to find the minimum count of subsets
// by partitioning the array with given conditions
int CntOfSubsetsByPartitioning(int arr[], int N)
{
    // Store frequency of each
    // distinct element of the array
    unordered_map freq;
 
    // Traverse the array
    for (int i = 0; i < N; i++) {
 
        // Update frequency
        // of arr[i]
        freq[arr[i]]++;
    }
 
    // Stores GCD of frequency of
    // each distinct element of the array
    int freqGCD = 0;
    for (auto i : freq) {
 
        // Update freqGCD
        freqGCD = __gcd(freqGCD, i.second);
    }
 
    return (N) / freqGCD;
}
 
// Driver Code
int main()
{
    int arr[] = { 1, 2, 3, 4, 4, 3, 2, 1 };
    int N = sizeof(arr) / sizeof(arr[0]);
    cout << CntOfSubsetsByPartitioning(arr, N);
    return 0;
}


Java
// Java program to implement
// the above approach
import java.util.*;
 
class GFG{
 
// Function to find the minimum count of subsets
// by partitioning the array with given conditions
static int CntOfSubsetsByPartitioning(int arr[], int N)
{
    // Store frequency of each
    // distinct element of the array
    HashMap freq = new HashMap<>();
 
    // Traverse the array
    for (int i = 0; i < N; i++) {
 
        // Update frequency
        // of arr[i]
        if(freq.containsKey(arr[i])){
            freq.put(arr[i], freq.get(arr[i])+1);
        }
        else{
            freq.put(arr[i], 1);
        }
    }
 
    // Stores GCD of frequency of
    // each distinct element of the array
    int freqGCD = 0;
    for (Map.Entry i : freq.entrySet()) {
 
        // Update freqGCD
        freqGCD = __gcd(freqGCD, i.getValue());
    }
 
    return (N) / freqGCD;
}
   
// Recursive function to return gcd of a and b 
static int __gcd(int a, int b) 
{ 
 return b == 0? a:__gcd(b, a % b);    
}
 
// Driver Code
public static void main(String[] args)
{
    int arr[] = { 1, 2, 3, 4, 4, 3, 2, 1 };
    int N = arr.length;
    System.out.print(CntOfSubsetsByPartitioning(arr, N));
}
}
 
// This code is contributed by 29AjayKumar


Python3
# Python3 program to implement
# the above approach
from math import gcd
 
# Function to find the minimum count
# of subsets by partitioning the array
# with given conditions
def CntOfSubsetsByPartitioning(arr, N):
     
    # Store frequency of each
    # distinct element of the array
    freq = {}
 
    # Traverse the array
    for i in range(N):
         
        # Update frequency
        # of arr[i]
        freq[arr[i]] = freq.get(arr[i], 0) + 1
 
    # Stores GCD of frequency of
    # each distinct element of the array
    freqGCD = 0
     
    for i in freq:
         
        # Update freqGCD
        freqGCD = gcd(freqGCD, freq[i])
 
    return (N) // freqGCD
 
# Driver Code
if __name__ == '__main__':
     
    arr = [ 1, 2, 3, 4, 4, 3, 2, 1 ]
    N = len(arr)
     
    print(CntOfSubsetsByPartitioning(arr, N))
 
# This code is contributed by mohit kumar 29


C#
// C# program to implement
// the above approach
using System;
using System.Collections.Generic;
 
class GFG{
 
// Function to find the minimum count of subsets
// by partitioning the array with given conditions
static int CntOfSubsetsByPartitioning(int []arr, int N)
{
    // Store frequency of each
    // distinct element of the array
    Dictionary freq = new Dictionary();
 
    // Traverse the array
    for (int i = 0; i < N; i++) {
 
        // Update frequency
        // of arr[i]
        if(freq.ContainsKey(arr[i])){
            freq[arr[i]] = freq[arr[i]]+1;
        }
        else{
            freq.Add(arr[i], 1);
        }
    }
 
    // Stores GCD of frequency of
    // each distinct element of the array
    int freqGCD = 0;
    foreach (KeyValuePair i in freq) {
 
        // Update freqGCD
        freqGCD = __gcd(freqGCD, i.Value);
    }
 
    return (N) / freqGCD;
}
   
// Recursive function to return gcd of a and b 
static int __gcd(int a, int b) 
{ 
 return b == 0? a:__gcd(b, a % b);    
}
 
// Driver Code
public static void Main(String[] args)
{
    int []arr = { 1, 2, 3, 4, 4, 3, 2, 1 };
    int N = arr.Length;
    Console.Write(CntOfSubsetsByPartitioning(arr, N));
}
}
 
 // This code is contributed by 29AjayKumar


输出:
4

时间复杂度: O(N * log(M)),其中 M 是数组的最小元素
辅助空间: O(N)

如果您想与行业专家一起参加直播课程,请参阅Geeks Classes Live