Open In App

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

Last Updated : 09 Jun, 2021
Improve
Improve
Like Article
Like
Save
Share
Report

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:

  • Initialize a map, say freq, to store the frequency of each distinct element of the array.
  • Initialize a variable, say, FreqGCD, to store the GCD of frequency of each distinct element of the array.
  • Traverse the map to find the value of FreqGCD.
  • Finally, print the value of (N) % FreqGCD.

Below is the implementation of the above approach:

C++




// 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;
}


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


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


Javascript




<script>
 
// JavaScript program to implement
// the above approach
 
// Function to find the minimum count of subsets
// by partitioning the array with given conditions
function CntOfSubsetsByPartitioning(arr, N)
{
    // Store frequency of each
    // distinct element of the array
    var freq = {};
     
    // Traverse the array
    for(var i = 0; i < N; i++)
    {
         
        // Update frequency
        // of arr[i]
        if (freq.hasOwnProperty(arr[i]))
        {
            freq[arr[i]] = freq[arr[i]] + 1;
        }
        else
        {
            freq[arr[i]] = 1;
        }
    }
     
    // Stores GCD of frequency of
    // each distinct element of the array
    var freqGCD = 0;
    for(const [key, value] of Object.entries(freq))
    {
         
        // Update freqGCD
        freqGCD = __gcd(freqGCD, value);
    }
    return parseInt(N / freqGCD);
}
 
// Recursive function to return gcd of a and b
function __gcd(a, b)
{
    return b == 0 ? a : __gcd(b, a % b);
}
 
// Driver Code
var arr = [ 1, 2, 3, 4, 4, 3, 2, 1 ];
var N = arr.length;
 
document.write(CntOfSubsetsByPartitioning(arr, N));
 
// This code is contributed by rdtank
 
</script>


Output: 

4

 

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



Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads