Partition array into minimum number of equal length subsets consisting of a single distinct value | C ++ program to implement the above approach ; Function to find the minimum count of subsets by partitioning the array with given conditions ; Store frequency of each distinct element of the array ; Traverse the array ; Update frequency of arr [ i ] ; Stores GCD of frequency of each distinct element of the array ; Update freqGCD ; Driver Code
#include <bits/stdc++.h> NEW_LINE 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 ; }