diff --git a/CPP/Array/array_subarrayEqualsK.cpp b/CPP/Array/array_subarrayEqualsK.cpp new file mode 100644 index 0000000..94c8eb9 --- /dev/null +++ b/CPP/Array/array_subarrayEqualsK.cpp @@ -0,0 +1,34 @@ +#include + +using namespace std; + +class Solution { +public: + int subarraySum(vector& nums, int k) { + int total = 0; + int cnt = 0; + unordered_map mapp; + + mapp[0] = 1; + + for (int i=0; i < nums.size();i++){ + total = total + nums[i]; + if (mapp.find(total-k) != mapp.end()){ + cnt = cnt + mapp[total-k]; + } + mapp[total]++; + } + + return cnt; + } +}; + +int main(){ + Solution s; + + vector inp = {1,2,3,1,2,3}; + cout << s.subarraySum(inp,3); + + return 0; + +} \ No newline at end of file