Maximum types of candies a person can eat if only N / 2 of them can be eaten | Java program for the above approach ; Function to find number of candy types ; Declare a hashset to store candies ; Traverse the given array and inserts element into set ; Return the result ; Function to find maximum number of types of candies a person can eat ; Store the number of candies allowed to eat ; Store the number of candy types ; Return the result ; Driver code ; Given Input ; Function Call
import java . util . * ; class GFG { public static int num_candyTypes ( int [ ] candies ) { Dictionary < Integer , Integer > s = new Hashtable < Integer , Integer > ( ) ; for ( int i = 0 ; i < candies . length ; i ++ ) { s . put ( candies [ i ] , 1 ) ; } return s . size ( ) ; } public static void distribute_candies ( int [ ] candies ) { int allowed = candies . length / 2 ; int types = num_candyTypes ( candies ) ; if ( types < allowed ) System . out . println ( types ) ; else System . out . println ( allowed ) ; } public static void main ( String [ ] args ) { int candies [ ] = { 4 , 4 , 5 , 5 , 3 , 3 } ; distribute_candies ( candies ) ; } }