Split an array into equal length subsequences consisting of equal elements only | Function to find the GCD of two numbers a and b ; Function to check if it is possible to split the array into equal length subsequences such that all elements in the subsequence are equal ; Store frequencies of array elements ; Traverse the array ; Update frequency of arr [ i ] ; Store the GCD of frequencies of all array elements ; Traverse the map ; Update GCD ; If the GCD is greater than 1 , print YES otherwise print NO ; Given array ; Store the size of the array
function gcd ( a , b ) { if ( b == 0 ) return a ; return gcd ( b , a % b ) ; } function splitArray ( arr , N ) { var mp = new Map ( ) ; for ( var i = 0 ; i < N ; i ++ ) { if ( mp . has ( arr [ i ] ) ) { mp . set ( arr [ i ] , mp . get ( arr [ i ] ) + 1 ) ; } else { mp . set ( arr [ i ] , 1 ) ; } } var G = 0 ; mp . forEach ( ( value , key ) => { G = gcd ( G , value ) ; } ) ; if ( G > 1 ) document . write ( " " ) ; else document . write ( " " ) ; } var arr = [ 1 , 2 , 3 , 4 , 4 , 3 , 2 , 1 ] ; var n = arr . length ; splitArray ( arr , n ) ;