-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMcm.cpp
More file actions
45 lines (25 loc) · 706 Bytes
/
Mcm.cpp
File metadata and controls
45 lines (25 loc) · 706 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
#include <bits/stdc++.h>
using namespace std;
int f(vector<int>& arr, int i, int j){
// base condition
if(i == j)
return 0;
int mini = INT_MAX;
// partioning loop
for(int k = i; k<= j-1; k++){
int ans = f(arr,i,k) + f(arr, k+1,j) + arr[i-1]*arr[k]*arr[j];
mini = min(mini,ans);
}
return mini;
}
int matrixMultiplication(vector<int>& arr, int N){
int i =1;
int j = N-1;
return f(arr,i,j);
}
int main() {
vector<int> arr = {10, 20, 30, 40, 50};
int n = arr.size();
cout<<"The minimum number of operations is "<<matrixMultiplication(arr,n);
return 0;
}