Chocolate Pickup Problem

Ninja has a 'GRID' of size 'R' x 'C'. Each cell of the grid contains some chocolates. Ninja has two friends, Alice and Bob, and he wants to collect as many chocolates as possible with their help.

Alice starts at the top-left corner (0, 0), and Bob starts at the top-right corner (0, C-1). From their current positions, they can move to the cells directly below, to their left-diagonal below, or their right-diagonal below. Chocolates collected from any cell will become zero, and if both stop at the same cell, only one will collect the chocolates.

Your task is to find the maximum number of chocolates that can be collected by following the movement rules.

Input:

The first line contains an integer 'T', the number of test cases.
For each test case: 
- The first line contains two integers 'R' and 'C'.
- The next 'R' lines each contain 'C' integers, representing the grid.

Output:

Output the maximum number of chocolates that can be collected for each test case, on a new line.

Example:

Input:
'R' = 3, 'C' = 4 
'GRID' = [[2, 3, 1, 2], [3, 4, 2, 2], [5, 6, 3, 5]]
Output:
21
Explanation:

Alice follows the path (0,0) -> (1,1) -> (2,1) and collects 2 + 4 + 6 = 12 chocolates.
Bob follows the path (0,3) -> (1,3) -> (2,3) and collects 2 + 2 + 5 = 9 chocolates.
The total chocolates collected is 21.

Constraints:

  • 1 <= 'T' <= 10
  • 2 <= 'R', 'C' <= 50
  • 0 <= 'GRID[i][j]' <= 102
  • Time Limit: 1 sec
AnswerBot
4mo

Find the maximum number of chocolates that can be collected by two friends moving in a grid.

  • Start from the top-left and top-right corners, move diagonally down to collect chocolates

  • Calculate the total...read more

Help your peers!
Select
Add answer anonymously...

HashedIn by Deloitte Software Developer interview questions & answers

A Software Developer was asked 2mo agoQ. What is the database design for Instagram?
A Software Developer was asked 11mo agoQ. Implement a swap method.
A Software Developer was asked 11mo agoQ. Given a matrix, write code to find the transpose of the matrix.

Popular interview questions of Software Developer

A Software Developer was asked 2mo agoQ1. What is the database design for Instagram?
A Software Developer was asked 11mo agoQ2. Implement a swap method.
A Software Developer was asked 11mo agoQ3. Given a matrix, write code to find the transpose of the matrix.
HashedIn by Deloitte Software Developer Interview Questions
Stay ahead in your career. Get AmbitionBox app
play-icon
play-icon
qr-code
Trusted by over 1.5 Crore job seekers to find their right fit company
80 L+

Reviews

10L+

Interviews

4 Cr+

Salaries

1.5 Cr+

Users

Contribute to help millions

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

Follow Us
  • Youtube
  • Instagram
  • LinkedIn
  • Facebook
  • Twitter
Profile Image
Hello, Guest
AmbitionBox Employee Choice Awards 2025
Winners announced!
awards-icon
Contribute to help millions!
Write a review
Write a review
Share interview
Share interview
Contribute salary
Contribute salary
Add office photos
Add office photos
Add office benefits
Add office benefits