Maximum Product Cutting | DP | The main function that returns the max possible product ; n equals to 2 or 3 must be handled explicitly ; Keep removing parts of size 3 while n is greater than 4 ; Keep multiplying 3 to res ; The last part multiplied by previous parts ; Driver program to test above functions
function maxProd ( n ) { if ( n == 2 n == 3 ) { return ( n - 1 ) ; } let res = 1 ; while ( n > 4 ) { n -= 3 ; res *= 3 ; } return ( n * res ) ; } document . write ( " " + maxProd ( 10 ) ) ;