text: Maximum types of candies a person can eat if only N / 2 of them can be eaten | C ++ 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
code: #include <bits/stdc++.h> NEW_LINE using namespace std ; int num_candyTypes ( vector < int > & candies ) { unordered_set < int > s ; for ( int i = 0 ; i < candies . size ( ) ; i ++ ) { s . insert ( candies [ i ] ) ; } return s . size ( ) ; } void distribute_candies ( vector < int > & candies ) { int allowed = candies . size ( ) / 2 ; int types = num_candyTypes ( candies ) ; if ( types < allowed ) cout << types ; else cout << allowed ; } int main ( ) { vector < int > candies = { 4 , 4 , 5 , 5 , 3 , 3 } ; distribute_candies ( candies ) ; return 0 ; }