Update appNew update is available. Click here to update.

Matrix Chain Multiplication

Posted: 10 Dec, 2020
Difficulty: Moderate


Try Problem

Given a chain of matrices A1, A2, A3,.....An. Your task is to find out the minimum cost to multiply these matrices. The cost of matrix multiplication is defined as the number of scalar multiplications. A Chain of matrices A1, A2, A3,.....An is represented by a sequence of numbers in an array ‘arr’ where the dimension of 1st matrix is equal to arr[0] * arr[1] , 2nd matrix is arr[1] * arr[2], and so on.

For example:

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.

Input Format:

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.

Output Format:

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.