Very Easy Explanation & Solution || Subarray sum equal to K
Before you go with this problem I will suggest you to first solve Subarray Sum Equal to K. This problem is quite modified version of that problem. Upvote if you
Before you go with this problem I will suggest you to first solve [Subarray Sum Equal to K](https://leetcode.com/problems/subarray-sum-equals-k/). This problem is quite modified version of that problem. Upvote if you like it.\n\n\n**Approach**\nFirst we will find the subarrays that contribute to sum equal to target, considering each row.\n\n| a | b | c | ------------------row1 \n| d | e | f  | ------------------row2\n| g | h | i  |-------------------row3\n\n\n\nCombine consecutive rows and find answer again, as submatrix can also combination of two or more rows.\n\n| a          | b           | c        |            ------find for this\n| d+a      | e+b      | f+c     |             -----find for this row\n| g+d+a  | h+e+b | i+f+c   |             ------find for this row\n\nsimilarly,\n\n| d     | e      | f     |                    -----for this row\n| g+d | h+e  | i+f  |                    -----for this row\n\nand,\n| g | h | i  |                                -----for this row\n\n\nadd the answers of all these rows, and have the final answer,\n\n\n**My Solution**\n```\nclass Solution {\npublic:\n    int sumEqualToK(vector<int>& nums, int k)\n    {\n        unordered_map<int,int> mp;\n        mp[0] =1;\n        int sum = 0,ans=0;\n        for(int i=0;i<nums.size();i++)\n        {\n            sum += nums[i];\n            ans += mp[sum-k];   \n            mp[sum]++;\n        }\n        return ans;\n    }\n    \n    int numSubmatrixSumTarget(vector<vector<int>>& matrix, int target) {\n        \n        int count=0;\n        for(int i=0;i<matrix.size();i++)\n        {\n            vector<int> prefSum(matrix[i].size(),0);\n            for(int j=i;j<matrix.size();j++)\n            {\n                for(int k=0;k<matrix[j].size();k++)\n                {\n                    prefSum[k] += matrix[j][k];\n                }\n                count += sumEqualToK(prefSum, target);\n\n            }  \n        }\n        return count;\n        \n    }\n};\n```\n\n\n
['Dynamic Programming', 'C', 'C++']