✅✅Easy C++ | Using Prefix Sum | Well explained and commented
Approach\n Describe your approach to solving the problem. \n1) Start with the range (l,r) as given in queries.\n2) Increment l++ and decrement r--.\n3) Now, whe
# Approach\n<!-- Describe your approach to solving the problem. -->\n1) Start with the range (l,r) as given in queries.\n2) Increment l++ and decrement r--.\n3) Now, when we will take prefix sum, then all elements in range of (l,r) will be converted, except the last column.\n4) Then pop the last column by removing the elements of last column.\n\n# Complexity\n- Time complexity : O(n^2)\n- Space complexity : O(n^2) \n- Extra Space complexity : O(1)\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\n\n# Code\n```\nclass Solution {\npublic:\n    vector<vector<int>> rangeAddQueries(int n, vector<vector<int>>& q) {\n        int m=q.size();\n        int i1,j1,i2,j2;\n        vector<vector<int>> ans(n, vector<int>(n+1));\n\n        for(int i=0; i<m; i++){\n            //finding the range i.e. (l,r)\n            i1=q[i][0];\n            j1=q[i][1];\n            i2=q[i][2];\n            j2=q[i][3];\n            //we are starting with a range (l,r)\n            //queries is a range given in form of (l,r)\n            //so method is: l=1 and r+1=-1\n            //when we take prefix sum, the elements in range will be added with 1 giving the desired result\n            for(int row=i1; row<=i2; row++){\n                ans[row][j1]++;\n                ans[row][j2+1]--;\n            }\n        }\n\n        //prefix sum for whole matrix in each row\n        for(int i=0; i<n; i++){\n            for(int j=1; j<n; j++){\n                //adding j-1 to sum, so start j with 1\n                ans[i][j]+=ans[i][j-1];\n            }\n            \n            ans[i].pop_back(); //pop the elements of last row for n*n matrix\n        }\n\n        return ans;\n    }\n};\n```
['Prefix Sum', 'C++']