Asked inJPMorgan Chase & Co.,Quantitative Research

Chocolate Bar Problem Statement

You are given a chocolate bar represented as a grid with dimensions N x M. Your task is to cut the chocolate bar, either horizontally or vertically, to obtain exactly 'K' pieces. The cost of each cut is calculated as the square of the break length.

Example:

If you have a chocolate bar with dimensions 3x4, you can:

  • Cut horizontally to create two bars of size 1x4 and 2x4, incurring a cost of 42 = 16
  • Cut vertically to create two bars of size 3x1 and 3x3, incurring a cost of 32 = 9

Input:

The first line contains an integer 'Q', representing the number of queries. Each subsequent line for each query contains three space-separated integers 'N', 'M', 'K', indicating the dimensions of the chocolate bar and the number of pieces desired, respectively.

Output:

Output 'Q' lines. Each line contains an integer representing the minimum total cost to break the chocolate bar to achieve exactly 'K' pieces for each query.

Constraints:

  • 1 <= Q <= 104
  • 1 <= N, M <= 30
  • 1 <= K <= min(N*M, 50)
  • Time Limit: 1 second

Note:

You can disregard any remaining pieces beyond 'K' after the cutting process. The implementation should aim to find and return the minimum cost for obtaining exactly 'K' pieces, without the need for printing, as it is handled by the driver code.

Be the first one to answer
Add answer anonymously...
JPMorgan Chase & Co. Quantitative Research Interview Questions
Stay ahead in your career. Get AmbitionBox app
qr-code
Helping over 1 Crore job seekers every month in choosing their right fit company
65 L+

Reviews

4 L+

Interviews

4 Cr+

Salaries

1 Cr+

Users/Month

Contribute to help millions

Made with ❤️ in India. Trademarks belong to their respective owners. All rights reserved © 2024 Info Edge (India) Ltd.

Follow us
  • Youtube
  • Instagram
  • LinkedIn
  • Facebook
  • Twitter