# Maximum Product Cutting

Given a rope of length *n* meters, cut the rope in different parts of integer lengths in a way that maximizes the product of lengths of all parts. You must make at least one cut. Assume that the length of the rope is more than 2 meters.

**Problem description:**

The problem wants you to find the maximum value of the product that you can obtain making certain partition at some points on the rope, you can make a partition between rope if the length of the rope is greater than 1 and the partitioned length can again be partitioned into some segments unless you get the maximum value of the product.i.e *n->(1 and (n-1)*, then *(n-1)* can be partitioned into more segments such as *(n-3)* and *(2)* since *(n-3)+2==(n-1)*. You are allowed to make any number of partitions according to your choice but they should return the maximum value.

**Input:**

The first line of input is *T*, number of test cases. Each test case consist of *N* the size of rope.

**Output:**

Output the maximum product of all cutting that you made.

**Examples:**

Input:
T=3
10
5
8
Output:
36
6
18

(a) Recursion Approach:In this approach we will use recursion to solve, we will create a solve function that will take the size of the rope as its parameter.

The base case for the solve function will be n==0 or n==1 since in this case, we can't make any cut.

For any other length of rope we have following option:

solve(n)=max(i*(n-i),solve(n-i)*i) for all i from 1,2,3,4......n-1, and here n>=2 in size.

For n==0 and for n==1 we simply return 0 as there is no further partition.

In the recursive function, we declare some temporary variable res which will be initialized with minimum value as we iterate through the entire length from (i>=1 to i<n) and check for each partition and then return the maximum value.

Time complexityfor the above approach in the worst case isexponentialSpace complexityfor the above approach in the worst case is:O(1)(b) Dynamic Programming Approach:(i) Top Down Approach:In this approach we will use the recursion plus memorization technique, we will create a

dp[n]state which will store the value of the calculated value of maximum product cutting for lengthn.We initially fill all the value of

dp[i]state with -1, then we calculate then we assigndp[n]if not calculated already otherwise we will directly return the value fordp[n]without recalculating it again.Time complexityfor this approach in the worst case is:O(n*n)Space complexityfor this approach in the worst case is:O(n)(ii) Bottom Approach:In this approach of

Dpwe will create thedp[]array of sizen+1because of 0 based indexing. We will fill thedp[]state in a bottom-up manner.The base case here will be for length 0 and 1 as 0.

dp[0]=dp[1]=0And for other lengths of the rope, we will check the maximum value that we can obtain before making a partition from length (1 to current length -1), that's why the inner loop iterate through 1 to i (outer loop variable).

i.e(j>=1 to j<=(i-1))Time complexityfor the above approach in the worst case is:O(n*n)Space complexityfor the above approach in the worst case:O(n)C++ Implementation (Recursion Approach):Output:C++ Implementation (Top-Down Dynamic Programming Approach):Output:C++ Implementation (Bottom-Up Dynamic Programming Approach):

need an explanation for this answer? contact us directly to get an explanation for this answerOutput: