• Courses
  • Tutorials
  • Jobs
  • Practice
  • Contests
November 07, 2023 |2.6K Views
PROBLEM OF THE DAY : 06/11/2023 | Letters Collection
Description
Discussion

Welcome to the daily solving of our PROBLEM OF THE DAY with Devashish Khare. We will discuss the entire problem step-by-step and work towards developing an optimized solution. This will not only help you brush up on your concepts of Dynamic Programming but also build up problem-solving skills.

In this video, we are given, a situation in which the Postmaster wants to write a program to answer the queries regarding letter collection in a city. A city is represented as a matrix mat of size n*m. Each cell represents a house and contains letters which are denoted by a number in the cell. The program should answer q queries which are of following types:
1 i j : To sum all the letters which are at a 1-hop distance from the cell (i,j) in any direction
2 i j : To sum all the letters which are at a 2-hop distance from the cell (i,j) in any direction 
The queries are given in a 2D matrix queries[][].
In one hop distance postmaster can go to any of [(i-1,j-1), (i-1,j), (i-1,j+1), (i,j-1), (i,j+1), (i+1,j-1), (i+1,j), (i+1,j+1)] from (i,j).

Example :

Input: 
n = 4, m = 5
mat = {{1, 2, 3, 4, 10}, 
      {5, 6, 7, 8, 10}, 
      {9, 1, 3, 4, 10}, 
      {1, 2, 3, 4, 10}}
q = 2
queries = {{1 0 1}, 
          {2 0 1}}
Output: 
22 29

Explaination: 
For the first query sum is 1+5+6+7+3 = 22. 
For the second query sum is 9+1+3+4+8+4 = 29.

Give the problem a try before going through the video. All the best!!!
Problem Link: https://www.geeksforgeeks.org/problems/c-letters-collection4552/1