Minimum number of bricks that can be intersected

Given a 2D array arr[][], representing width of bricks of the same height present on a wall, the task is to find the minimum number of bricks that can be intersected by drawing a straight line from the top to the bottom of the wall.Note: A line is said to intersect a brick if it passes through the brick and is non-intersecting if it is touching a brick’s boundary.Examples:Input: arr[][] = {{1, 2, 2, 1}, {3, 1, 2}, {1, 3, 2}, {2, 4}, {3, 1, 2}, {1, 3, 1, 1}} Output: 2Explanation: Considering the top left corner of the 2D array as the origin, the line is drawn at the coordinate x = 4 on the x-axis, such that it crosses the bricks on the 1st and 4th level, resulting in the minimum number of bricks crossed.Input: arr[][] = {{1, 1, 1}}Output: 0Explanation: The line can be drawn at x = 1 or x = 2 coordinate on the x-axis such that it does not cross any brick resulting in the minimum number of bricks crossed.Naive Approach: The simplest approach is to consider straight lines that can be drawn on every possible coordinate on the x-axis, along the width of the wall, considering x = 0 at the top left corner of the wall. Then, calculate the number of bricks inserted in each case and print the minimum count obtained.Time Complexity: O(N * M) where N is the total number of bricks and M is the total width of the wallAuxiliary Space: O(1) Approach: To optimize the above approach, the idea is to store the number of bricks ending at a certain width on the x-axis in a hashmap, and then find the line where the most number of bricks end. After getting this count, subtract it from the total height of the wall to get the minimum number of bricks crossed. Follow the steps below to solve the problem:Initialize a hashmap, M to store the number of bricks ending at a certain width on the x-axis.Traverse the array, arr using the variable i to store row indexInitialize a variable, width as 0 to store the ending position.Store the size of the current row in a variable X.Iterate in the range [0, X-2] using the variable jIncrement the value of width by arr[i][j] and increment the value of width in M by 1.Also, keep track of the most number of bricks ending at a certain width and store it in a variable, res.Subtract the value of res from the overall height of the wall and store it in a variable, ans.Print the value of ans as the result.Below is the implementation of the above approach:C++#include using namespace std;  void leastBricks(vector wall){        unordered_map map;              int res = 0;          for (vector list : wall) {                  int width = 0;                  for (int i = 0; i < list.size() - 1; i++) {                                      width += list[i];                                      map[width]++;                          res = max(res, map[width]);        }    }          cout