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 code
< ? php function maxProd ( $ n ) { if ( $ n == 2 $ n == 3 ) return ( $ n - 1 ) ; $ res = 1 ; while ( $ n > 4 ) { $ n = $ n - 3 ; $ res = $ res * 3 ; } return ( $ n * $ res ) ; } echo ( " Maximum ▁ Product ▁ is ▁ " ) ; echo ( maxProd ( 10 ) ) ; ? >