Partition array into minimum number of equal length subsets consisting of a single distinct value | Java 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 ; Recursive function to return gcd of a and b ; Driver Code
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 ) ) ; } }