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

📅  最后修改于: 2021-05-17 16:52:06             🧑  作者: 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)