✅ c++ || Prefix sum || Detailed Explaination✅
Intuition \uD83D\uDE80\nThe intuition behind this code is to use a 2D prefix sum algorithm to efficiently calculate the range add queries on a 2D grid.\n\nThe c
# Intuition \uD83D\uDE80\nThe intuition behind this code is to use a 2D prefix sum algorithm to efficiently calculate the range add queries on a 2D grid.\n\nThe code first initializes a 2D array "ansArray" with all elements as 0. Then it iterates through the input "queries" vector, which contains information about the ranges (r1, c1) and (r2, c2) that need to be incremented in the 2D grid. The code increments the value at position (r1, c1) in the "ansArray" and then checks for cases where indices r2+1 and c2+1 are less than n and update the ansArray accordingly.\n\nThen the code uses prefix sum algorithm to update the values in the ansArray by iterating through the array twice, first for column and then for row.\n\nFinally, the code creates a new 2D array "res" with the same dimensions as "ansArray" and copies the values of "ansArray" into "res". The method returns "res" as the final answer, which contains the updated grid after all the range add queries have been applied.\n\nThe benefit of using prefix sum algorithm is that it allows to perform multiple queries and update the grid in a time complexity of O(n) where n is the number of elements in the grid.\n\n# Code \uD83D\uDE80\n```\nclass Solution {\npublic:\n    vector<vector<int>> rangeAddQueries(int n, vector<vector<int>>& queries) {\n            vector<vector<int>> ansArray(n, vector<int>(n, 0));\n        \n            for (auto &it : queries) {\n                int r1 = it[0], c1 = it[1], r2 = it[2], c2 = it[3];\n                ansArray[r1][c1]++;\n                \n                if (r2 + 1 < n && c2 + 1 < n) {\n                    ansArray[r2 + 1][c2 + 1]++;\n                }\n                if (r2 + 1 < n) {\n                    ansArray[r2 + 1][c1]--;\n                }\n                if (c2 + 1 < n) {\n                    ansArray[r1][c2 + 1]--;\n                }\n            }\n        \n            for (int i = 0; i < n; i++) {\n                for (int j = 1; j < n; j++) {\n                    ansArray[i][j] += ansArray[i][j - 1];\n                }\n                \n            }\n      \n            for (int i = 1; i < n; i++) {\n                for (int j = 0; j < n; j++) {\n                    ansArray[i][j] += ansArray[i - 1][j];\n                }\n            }\n        \n            return ansArray;\n    }\n};\n```
['C++']