`{"id":"561516bb-0111-4c1c-9f1f-de1311f48a02","name":"Matrix Chain Multiplication","description":"1. You are given an array(arr) of positive integers of length N which represents the dimensions of N-1 matrices such that the ith matrix is of dimension arr[i-1] x arr[i].\r\n2. You have to find the minimum number of multiplications needed to multiply the given chain of matrices.","inputFormat":"A number N\r\narr1\r\narr2.. N integers","outputFormat":"Check the sample output and question video.","constraints":"2 &lt;= N &lt;= 1000\r\n1 &lt;= arr[i] &lt;= 500","sampleCode":{"cpp":{"code":""},"java":{"code":"import java.io.*;\r\nimport java.util.*;\r\n\r\npublic class Main {\r\n\r\n\tpublic static int mcm(int[] arr){\r\n\t\t//write your code here\r\n\r\n\t\treturn 0;\r\n\t}\r\n\tpublic static void main(String[] args) {\r\n\t\tScanner scn = new Scanner(System.in);\r\n\t\tint n = scn.nextInt();\r\n\t\tint[] arr = new int[n];\r\n\t\tfor(int i = 0 ; i < n; i++){\r\n\t\t\tarr[i] = scn.nextInt();\r\n\t\t}\r\n\t\tSystem.out.println(mcm(arr));\r\n\t}\r\n\r\n\t\r\n}"},"node":{"code":""},"ruby":{"code":""},"python":{"code":""}},"points":10,"difficulty":"medium","sampleInput":"3\r\n1\r\n2\r\n3","sampleOutput":"6\r\n","questionVideo":"https://www.youtube.com/embed/pEYwLmGJcvs?end=393","hints":[],"associated":[],"solutionSeen":false,"tags":[],"meta":{"path":[{"id":0,"name":"home"},{"id":"0c54b191-7b99-4f2c-acb3-e7f2ec748b2a","name":"Data Structures and Algorithms","slug":"data-structures-and-algorithms","type":0},{"id":"5539a6e8-c8bf-4f04-805c-e43e9d20e72a","name":"Dynamic Programming For Intermediate","slug":"dynamic-programming-for-intermediate-408","type":0},{"id":"ead477b3-cc66-44be-93e9-cb188e2cb633","name":"Matrix Chain Multiplication","slug":"matrix-chain-multiplication","type":1}],"next":{"id":"8756c240-025e-40d9-9e91-b507e41c3469","name":"MATRIX CHAIN MULTIPLICATION MCQ","type":0,"slug":"matrix-chain-multiplication-mcq"},"prev":{"id":"fb04a848-d437-4696-83a2-864d5d96a4b1","name":"Minimum Palindromic Cut","type":3,"slug":"minimum-palindromic-cut"}}}`

# Matrix Chain Multiplication

1. You are given an array(arr) of positive integers of length N which represents the dimensions of N-1 matrices such that the ith matrix is of dimension arr[i-1] x arr[i]. 2. You have to find the minimum number of multiplications needed to multiply the given chain of matrices.

`{"id":"561516bb-0111-4c1c-9f1f-de1311f48a02","name":"Matrix Chain Multiplication","description":"1. You are given an array(arr) of positive integers of length N which represents the dimensions of N-1 matrices such that the ith matrix is of dimension arr[i-1] x arr[i].\r\n2. You have to find the minimum number of multiplications needed to multiply the given chain of matrices.","inputFormat":"A number N\r\narr1\r\narr2.. N integers","outputFormat":"Check the sample output and question video.","constraints":"2 &lt;= N &lt;= 1000\r\n1 &lt;= arr[i] &lt;= 500","sampleCode":{"cpp":{"code":""},"java":{"code":"import java.io.*;\r\nimport java.util.*;\r\n\r\npublic class Main {\r\n\r\n\tpublic static int mcm(int[] arr){\r\n\t\t//write your code here\r\n\r\n\t\treturn 0;\r\n\t}\r\n\tpublic static void main(String[] args) {\r\n\t\tScanner scn = new Scanner(System.in);\r\n\t\tint n = scn.nextInt();\r\n\t\tint[] arr = new int[n];\r\n\t\tfor(int i = 0 ; i < n; i++){\r\n\t\t\tarr[i] = scn.nextInt();\r\n\t\t}\r\n\t\tSystem.out.println(mcm(arr));\r\n\t}\r\n\r\n\t\r\n}"},"node":{"code":""},"ruby":{"code":""},"python":{"code":""}},"points":10,"difficulty":"medium","sampleInput":"3\r\n1\r\n2\r\n3","sampleOutput":"6\r\n","questionVideo":"https://www.youtube.com/embed/pEYwLmGJcvs?end=393","hints":[],"associated":[],"solutionSeen":false,"tags":[],"meta":{"path":[{"id":0,"name":"home"},{"id":"0c54b191-7b99-4f2c-acb3-e7f2ec748b2a","name":"Data Structures and Algorithms","slug":"data-structures-and-algorithms","type":0},{"id":"5539a6e8-c8bf-4f04-805c-e43e9d20e72a","name":"Dynamic Programming For Intermediate","slug":"dynamic-programming-for-intermediate-408","type":0},{"id":"ead477b3-cc66-44be-93e9-cb188e2cb633","name":"Matrix Chain Multiplication","slug":"matrix-chain-multiplication","type":1}],"next":{"id":"8756c240-025e-40d9-9e91-b507e41c3469","name":"MATRIX CHAIN MULTIPLICATION MCQ","type":0,"slug":"matrix-chain-multiplication-mcq"},"prev":{"id":"fb04a848-d437-4696-83a2-864d5d96a4b1","name":"Minimum Palindromic Cut","type":3,"slug":"minimum-palindromic-cut"}}}` Editor

# Matrix Chain Multiplication

medium

1. You are given an array(arr) of positive integers of length N which represents the dimensions of N-1 matrices such that the ith matrix is of dimension arr[i-1] x arr[i]. 2. You have to find the minimum number of multiplications needed to multiply the given chain of matrices.

## Constraints

2 <= N <= 1000 1 <= arr[i] <= 500

## Format

### Input

A number N arr1 arr2.. N integers

### Output

Check the sample output and question video.

## Example

Sample Input

```.css-23h8hz{color:inherit;font-size:0.875rem;line-height:1.125rem;letter-spacing:0.016rem;font-weight:var(--chakra-fontWeights-normal);white-space:pre-wrap;}3 1 2 3```

### Sample Output

```.css-3oaykw{color:var(--chakra-colors-active-primary);font-size:0.875rem;line-height:1.125rem;letter-spacing:0.016rem;font-weight:var(--chakra-fontWeights-normal);white-space:pre-wrap;font-family:Monospace;}6 ```

Question Video

Discussions

Show Discussion

Related Resources 