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: 4
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: 8
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++
#include <bits/stdc++.h>
using namespace std;
int CntOfSubsetsByPartitioning( int arr[], int N)
{
unordered_map< int , int > freq;
for ( int i = 0; i < N; i++) {
freq[arr[i]]++;
}
int freqGCD = 0;
for ( auto i : freq) {
freqGCD = __gcd(freqGCD, i.second);
}
return (N) / freqGCD;
}
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
import java.util.*;
class GFG{
static int CntOfSubsetsByPartitioning( int arr[], int N)
{
HashMap<Integer,Integer> freq = new HashMap<>();
for ( int i = 0 ; i < N; i++) {
if (freq.containsKey(arr[i])){
freq.put(arr[i], freq.get(arr[i])+ 1 );
}
else {
freq.put(arr[i], 1 );
}
}
int freqGCD = 0 ;
for (Map.Entry<Integer,Integer> i : freq.entrySet()) {
freqGCD = __gcd(freqGCD, i.getValue());
}
return (N) / freqGCD;
}
static int __gcd( int a, int b)
{
return b == 0 ? a:__gcd(b, a % b);
}
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));
}
}
|
Python3
from math import gcd
def CntOfSubsetsByPartitioning(arr, N):
freq = {}
for i in range (N):
freq[arr[i]] = freq.get(arr[i], 0 ) + 1
freqGCD = 0
for i in freq:
freqGCD = gcd(freqGCD, freq[i])
return (N) / / freqGCD
if __name__ = = '__main__' :
arr = [ 1 , 2 , 3 , 4 , 4 , 3 , 2 , 1 ]
N = len (arr)
print (CntOfSubsetsByPartitioning(arr, N))
|
C#
using System;
using System.Collections.Generic;
class GFG{
static int CntOfSubsetsByPartitioning( int []arr, int N)
{
Dictionary< int , int > freq = new Dictionary< int , int >();
for ( int i = 0; i < N; i++) {
if (freq.ContainsKey(arr[i])){
freq[arr[i]] = freq[arr[i]]+1;
}
else {
freq.Add(arr[i], 1);
}
}
int freqGCD = 0;
foreach (KeyValuePair< int , int > i in freq) {
freqGCD = __gcd(freqGCD, i.Value);
}
return (N) / freqGCD;
}
static int __gcd( int a, int b)
{
return b == 0? a:__gcd(b, a % b);
}
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));
}
}
|
Javascript
<script>
function CntOfSubsetsByPartitioning(arr, N)
{
var freq = {};
for ( var i = 0; i < N; i++)
{
if (freq.hasOwnProperty(arr[i]))
{
freq[arr[i]] = freq[arr[i]] + 1;
}
else
{
freq[arr[i]] = 1;
}
}
var freqGCD = 0;
for (const [key, value] of Object.entries(freq))
{
freqGCD = __gcd(freqGCD, value);
}
return parseInt(N / freqGCD);
}
function __gcd(a, b)
{
return b == 0 ? a : __gcd(b, a % b);
}
var arr = [ 1, 2, 3, 4, 4, 3, 2, 1 ];
var N = arr.length;
document.write(CntOfSubsetsByPartitioning(arr, N));
</script>
|
Time Complexity: O(N * log(M)), where M is the smallest element of the array
Auxiliary Space: O(N)
Feeling lost in the world of random DSA topics, wasting time without progress? It's time for a change! Join our DSA course, where we'll guide you on an exciting journey to master DSA efficiently and on schedule.
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 geeks!
Last Updated :
09 Jun, 2021
Like Article
Save Article