text: Divide array in two maximum equal length arrays of similar and dissimilar elements | C # program to find the max - size to which an array can be divided into 2 equal parts such that one part contains unique elements while another contains similar elements ; Function to find the max - size to which an array can be divided into 2 equal parts ; Array to find the frequency of each element of array ; Find the index maximum frequency element present in array arr [ ] ; Find total unique elements present in array arr [ ] ; Find the Max - Size to which an array arr [ ] can be splitted ; Find the first array containing same elements ; Find the second array containing unique elements ; Driver Code ; Initialise n ; Array declaration ; Size of array
code: using System ; class GFG { static void Solve ( int [ ] arr , int size , int n ) { int [ ] v = new int [ n + 1 ] ; for ( int i = 0 ; i < size ; i ++ ) v [ arr [ i ] ] ++ ; int max1 = - 1 , mx = - 1 ; for ( int i = 0 ; i < v . Length ; i ++ ) { if ( v [ i ] > mx ) { mx = v [ i ] ; max1 = i ; } } int cnt = 0 ; foreach ( int i in v ) { if ( i == 0 ) ++ cnt ; } int diff1 = n + 1 - cnt ; int max_size = Math . Max ( Math . Min ( v [ max1 ] - 1 , diff1 ) , Math . Min ( v [ max1 ] , diff1 - 1 ) ) ; Console . Write ( " Maximum ▁ size ▁ is ▁ : " + max_size + " STRNEWLINE " ) ; Console . Write ( " The ▁ First ▁ Array ▁ Is ▁ : STRNEWLINE " ) ; for ( int i = 0 ; i < max_size ; i ++ ) { Console . Write ( max1 + " ▁ " ) ; v [ max1 ] -= 1 ; } Console . Write ( " STRNEWLINE " ) ; Console . Write ( " The ▁ Second ▁ Array ▁ Is ▁ : STRNEWLINE " ) ; for ( int i = 0 ; i < ( n + 1 ) ; i ++ ) { if ( v [ i ] > 0 ) { Console . Write ( i + " ▁ " ) ; max_size -- ; } if ( max_size < 1 ) break ; } Console . Write ( " STRNEWLINE " ) ; } public static void Main ( string [ ] args ) { int n = 7 ; int [ ] arr = new int [ ] { 1 , 2 , 1 , 5 , 1 , 6 , 7 , 2 } ; int size = arr . Length ; Solve ( arr , size , n ) ; } }