text: Minimize K to let Person A consume at least ceil ( N / ( M + 1 ) ) candies based on given rules | Function to find minimum value of K such that the first person gets at least ( N / ( M + 1 ) ) candies ; Find the minimum required value of candies for the first person ; Iterate K from [ 1 , n ] ; Total number of candies ; Candies taken by Person 1 ; Candies taken by 1 st person is minimum of K and candies left ; Traverse the array arr [ ] ; Amount consumed by the person j ; Update the number of candies ; Good share of candies achieved ; Driver Code
code: function minimumK ( arr , M , N ) { let good = Math . floor ( ( N * 1.0 ) / ( ( M + 1 ) * 1.0 ) ) + 1 ; for ( let i = 1 ; i <= N ; i ++ ) { let K = i ; let candies = N ; let taken = 0 ; while ( candies > 0 ) { taken += Math . min ( K , candies ) ; candies -= Math . min ( K , candies ) ; for ( let j = 0 ; j < M ; j ++ ) { let consume = ( arr [ j ] * candies ) / 100 ; candies -= consume ; } } if ( taken >= good ) { document . write ( i ) ; return ; } } } let N = 13 , M = 1 ; let arr = new Array ( ) ; arr . push ( 50 ) ; minimumK ( arr , M , N ) ;