Partition array into minimum number of equal length subsets consisting of a single distinct value

Given an array arr[] of size N, the task is to print the minimum count of equal length subsets the array can be partitioned into such that each subset contains only a single distinct element

Examples:

Input: arr[] = { 1, 2, 3, 4, 4, 3, 2, 1 } 
Output:
Explanation: 
Possible partition of the array is { {1, 1}, {2, 2}, {3, 3}, {4, 4} }. 
Therefore, the required output is 4.

Input: arr[] = { 1, 1, 1, 2, 2, 2, 3, 3 } 
Output:
Explanation: 
Possible partition of the array is { {1}, {1}, {1}, {2}, {2}, {2}, {3}, {3} }. 
Therefore, the required output is 8.

Naive Approach: The simplest approach to solve the problem is to store the frequency of each distinct array element, iterate over the range [N, 1] using the variable i, and check if the frequency of all distinct elements of the array is divisible by i or not. If found to be true, then print the value of (N / i)



Time Complexity: O(N2). 
Auxiliary Space: O(N) 

Efficient Approach: To optimize the above approach the idea is to use the concept of GCD. Follow the steps below to solve the problem:

Below is the implementation of the above approach:

filter_none

edit
close

play_arrow

link
brightness_4
code

// C++ program to implement
// the above approach
 
#include <bits/stdc++.h>
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<int, int> 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;
}
chevron_right

filter_none

edit
close

play_arrow

link
brightness_4
code

// 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<Integer,Integer> 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<Integer,Integer> 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
chevron_right

filter_none

edit
close

play_arrow

link
brightness_4
code

# 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
chevron_right

filter_none

edit
close

play_arrow

link
brightness_4
code

// 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<int,int> freq = new Dictionary<int,int>();
 
    // 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<int,int> 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
chevron_right

Output: 
4

 

Time Complexity: O(N * log(M)), where M is the smallest element of the array 
Auxiliary Space: O(N)

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.




Article Tags :
Practice Tags :