text: Maximize number of days for which P chocolates can be distributed consecutively to N people | C # program to implement the above approach ; Stores the frequency of each type of chocolate ; Function to check if chocolates can be eaten for ' mid ' no . of days ; If cnt exceeds N , return true ; Function to find the maximum number of days for which chocolates can be eaten ; Store the frequency of each type of chocolate ; Initialize start and end with 0 and P respectively ; Calculate mid ; Check if chocolates can be distributed for mid days ; Check if chocolates can be distributed for more than mid consecutive days ; Driver code ; Function call
code: using System ; using System . Collections . Generic ; class GFG { static Dictionary < int , int > mp = new Dictionary < int , int > ( ) ; static int N , P ; static bool helper ( int mid ) { int cnt = 0 ; foreach ( KeyValuePair < int , int > i in mp ) { int temp = i . Value ; while ( temp >= mid ) { temp -= mid ; cnt ++ ; } } return cnt >= N ; } static int findMaximumDays ( int [ ] arr ) { for ( int i = 0 ; i < P ; i ++ ) { if ( mp . ContainsKey ( arr [ i ] ) ) { mp [ arr [ i ] ] = mp [ arr [ i ] ] + 1 ; } else { mp . Add ( arr [ i ] , 1 ) ; } } int start = 0 , end = P , ans = 0 ; while ( start <= end ) { int mid = start + ( ( end - start ) / 2 ) ; if ( mid != 0 && helper ( mid ) ) { ans = mid ; start = mid + 1 ; } else if ( mid == 0 ) { start = mid + 1 ; } else { end = mid - 1 ; } } return ans ; } public static void Main ( String [ ] args ) { N = 3 ; P = 10 ; int [ ] arr = { 1 , 2 , 2 , 1 , 1 , 3 , 3 , 3 , 2 , 4 } ; Console . Write ( findMaximumDays ( arr ) ) ; } }