Geeksforgeeks

Error

×

Leaderboard

Showing:

Handle | Score |
---|---|

@Ibrahim Nash | 5761 |

@blackshadows | 5715 |

@akhayrutdinov | 5111 |

@mb1973 | 4989 |

@Quandray | 4944 |

@saiujwal13083 | 4506 |

@sanjay05 | 3762 |

@marius_valentin_dragoi | 3516 |

@sushant_a | 3459 |

@verma_ji | 3341 |

@KshamaGupta | 3318 |

Complete Leaderboard | |

Handle | Score |

@aroranayan999 | 1115 |

@bt8816103042 | 739 |

@SherlockHolmes3 | 447 |

@codeantik | 441 |

@SHOAIBVIJAPURE | 430 |

@shalinibhataniya1097 | 408 |

@ShamaKhan1 | 392 |

@neverevergiveup | 381 |

@amrutakashikar2 | 355 |

@mahlawatep | 353 |

@murarry3625 | 352 |

Complete Leaderboard |

Given an array **p[]** of length **n **used to denote the dimensions of a series of matrices such that dimension of **i'th** matrix is **p[i] * p[i+1]**. There are a total of **n-1** matrices. Find the most efficient way to multiply these matrices together.

The problem is not actually to perform the multiplications, but merely to decide in which order to perform the multiplications such that you need to perform minimum number of multiplications. There are many options to multiply a chain of matrices because matrix multiplication is associative i.e. no matter how one parenthesize the product, the result will be the same.

**Example 1:**

**Input:**
n = 5
p[] = {1, 2, 3, 4, 5}
**Output:** (((AB)C)D)
**Explaination:** The total number of
multiplications are (1*2*3) + (1*3*4)
+ (1*4*5) = 6 + 12 + 20 = 38.

**Example 2:**

**Input:**
n = 3
p = {3, 3, 3}
**Output:** (AB)
**Explaination:** The total number of
multiplications are (3*3*3) = 27.

**Your Task:**

You do not need to read input or print anything. Your task is to complete the function **matrixChainOrder()** which takes n and p[] as input parameters and returns the string with the proper order of parenthesis for n-1 matrices. Use uppercase alphabets to denote each matrix.

**Expected Time Complexity:** O(n^{3})

**Expected Auxiliary Space:** O(n^{2})

**Constraints:**

2 ≤ n ≤ 26

1 ≤ p[i] ≤ 500

Login to report an issue on this page.

We strongly recommend solving this problem on your own before viewing its editorial. Do you still want to view the editorial?

Yes
Brackets in Matrix Chain Multiplication

...