Equally divide into two sets such that one set has maximum distinct elements | Java program to equally divide n elements into two sets such that second set has maximum distinct elements . ; Insert all the resources in the set There will be unique resources in the set ; return minimum of distinct resources and n / 2 ; Driver code
import java . util . * ; class GFG { static int distribution ( int arr [ ] , int n ) { Set < Integer > resources = new HashSet < Integer > ( ) ; for ( int i = 0 ; i < n ; i ++ ) resources . add ( arr [ i ] ) ; return Math . min ( resources . size ( ) , n / 2 ) ; } public static void main ( String [ ] args ) { int arr [ ] = { 1 , 1 , 2 , 1 , 3 , 4 } ; int n = arr . length ; System . out . print ( distribution ( arr , n ) + "NEW_LINE"); } }