Split an array into equal length subsequences consisting of equal elements only | C # program for the above approach ; 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 each array element ; 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 ; Driver Code ; Given array ; Store the size of the array
using System ; using System . Collections . Generic ; using System . Linq ; class GFG { static int gcd ( int a , int b ) { if ( b == 0 ) return a ; return gcd ( b , a % b ) ; } static void splitArray ( int [ ] arr , int n ) { Dictionary < int , int > mp = new Dictionary < int , int > ( ) ; for ( int i = 0 ; i < n ; ++ i ) { if ( mp . ContainsKey ( arr [ i ] ) == true ) mp [ arr [ i ] ] += 1 ; else mp [ arr [ i ] ] = 1 ; } int G = 0 ; foreach ( KeyValuePair < int , int > i in mp ) { G = gcd ( G , i . Value ) ; } if ( G > 1 ) Console . Write ( " YES " ) ; else Console . Write ( " NO " ) ; } public static void Main ( ) { int [ ] arr = { 1 , 2 , 3 , 4 , 4 , 3 , 2 , 1 } ; int n = arr . Length ; splitArray ( arr , n ) ; } }