text: Divide array in two maximum equal length arrays of similar and dissimilar elements | Function to find the max - size to which an array can be divided leto 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 ; initialise n ; array declaration ; size of array
code: function Solve ( arr , size , n ) { let v = Array . from ( { length : n + 1 } , ( _ , i ) => 0 ) ; for ( let i = 0 ; i < size ; i ++ ) v [ arr [ i ] ] ++ ; let max1 = - 1 , mx = - 1 ; for ( let i = 0 ; i < v . length ; i ++ ) { if ( v [ i ] > mx ) { mx = v [ i ] ; max1 = i ; } } let cnt = 0 ; for ( let i in v ) { if ( i == 0 ) ++ cnt ; } let diff1 = n + 1 - cnt ; let max_size = Math . max ( Math . min ( v [ max1 ] - 1 , diff1 ) , Math . min ( v [ max1 ] , diff1 - 1 ) ) ; document . write ( " " + max_size + " " ) ; document . write ( " " + " " ) ; for ( let i = 0 ; i < max_size ; i ++ ) { document . write ( max1 + " " ) ; v [ max1 ] -= 1 ; } document . write ( " " ) ; document . write ( " " + " " ) ; for ( let i = 0 ; i < ( n + 1 ) ; i ++ ) { if ( v [ i ] > 0 ) { document . write ( i + " " ) ; max_size -- ; } if ( max_size < 1 ) break ; } document . write ( " " ) ; } let n = 7 ; let arr = [ 1 , 2 , 1 , 5 , 1 , 6 , 7 , 2 ] ; let size = arr . length ; Solve ( arr , size , n ) ;