


You don’t have to multiply the matrices, you only have to find the minimum number of multiplication operations.
ARR = {2, 4, 3, 2}
Here, we have three matrices with dimensions {2X4, 4X3, 3X2} which can be multiplied in the following ways:
a. If the order of multiplication is (2X4, 4X3)(3X2), then the total number of multiplication operations that need to be performed are: (2*4*3) + (2*3*2) = 36
b. If the order of multiplication is (2X4)(4X3, 3X2), then the total number of multiplication operations that need to be performed are (2*4*2) + (4*3*2) = 40
The first line contains a single integer ‘T’ denoting the number of test cases. The test cases are as follows.
The first line of each test case contains a single integer ‘N’ denoting the number of the matrices.
The second line of each test case contains ‘N + 1’ single space-separated integers denoting where the first ‘N’ integers denote the number of rows in the Matrices and the last element denotes the number of columns of the last matrix.
For each test case, print the minimum number of multiplication operations that need to be performed to multiply all the given matrices.
Print the output of each test case on a new line.
You don’t need to print anything; It has already been taken care of.
1 <= T <= 5
1 <= N <= 100
1 <= ARR[i] <= 10^2
Time Limit: 1 sec
The idea is to use the recursion to try all the possible combinations of multiplying the matrices. After trying all the possible combinations we will take the minimum operations out of all the combinations.
For multiplying two matrices with dimensions (mXn) and (nXp), the number of operations required are m * n * p.
The steps are as follows :
Let’s have a look at the recursion tree of the previous approach.
We see that we are solving the same subproblem more than once.
The idea is to use dynamic programming to store the results of a subproblem so that we don’t solve the same subproblem more than once.
The steps are as follows :
The idea is to use dynamic programming to store the results of a subproblem and then use the stored results to solve other subproblems.
We will iterate over the length of subarrays, let’s say the current length is “len” and in one iteration we will solve the problem for all subarrays of size “len” by using the previously-stored results of subarrays having a length less than “len”.
The steps are as follows: