Problem of the day
For arr[ ] = { 10, 20, 30, 40}, matrix A1 = [10 * 20], A2 = [20 * 30], A3 = [30 * 40]
Scalar multiplication of matrix with dimension 10 * 20 is equal to 200.
The first line of input contains an integer ‘T’, denoting the number of test cases. Then each test case follows.
The first line of each test case contains the Integer ‘N’ denoting the number of elements in the array.
The second and the last line of each test case contains ‘N’ single space-separated integers representing the elements of the array.
For each test case, print a single integer, denoting the minimum cost of matrix multiplication.
Output of each test case will be printed on a separate line.
You do not need to print anything, it has already been taken care of. Just implement the given function.
1 <= T <= 5
2 <= N <= 100
1 <= arr[i] <= 400
Time Limit: 1 sec.
2
4
4 5 3 2
4
10 15 20 25
8000
70
In the first test case, there are three matrices of dimensions A = [4 5], B = [5 3] and C = [3 2]. The most efficient order of multiplication is A * ( B * C).
Cost of ( B * C ) = 5 * 3 * 2 = 30 and (B * C) = [5 2] and A * (B * C) = [ 4 5] * [5 2] = 4 * 5 * 2 = 40. So the overall cost is equal to 30 + 40 =70.
In the second test case, there are two ways to multiply the chain - A1*(A2*A3) or (A1*A2)*A3.
If we multiply in order- A1*(A2*A3), then the number of multiplications required is 11250.
If we multiply in order- (A1*A2)*A3, then the number of multiplications required is 8000.
Thus a minimum number of multiplications required is 8000.
1
4
1 4 3 2
18
In the first test case, there are three matrices of dimensions A = [1 4], B = [4 3] and C = [3 2]. The most efficient order of multiplication is (A * B) * C .