Subarray Sum Equals K

question–>


code–>

not passinfg all testcases help

Bro edit krke de do apna code, or by using ide.codingblocks.com your choice

Are you using prefix sum with map @sheikhhaji18?

yes i am using prefix sum with map, and finding the frequency of pre-k and adding it to my ans;

Have updated this in your code

for(int i=0;i<n;i++){
            pre+=nums[i];
            ans+=m[pre-k];
            m[pre]++;
        }

Just do this it will pass all test case.

map on itself is initialized with zero ?

There’s no need of checking that statement, you are talking about that if statement where you are checking value of pre in map right?

yes i was checking if it is present or not ,if not present the value becomes one ,if the same element is encountered again it will incremented; okay thanks it passed all testcases;but why this was not working

I will suggest you to dry run for a random test case in the code you gave it to me where it fails (take {3,5,9,1,3,5,2,0,4} with target 7 } you will get the intuition where were you wrong.

Map does that own it’s on. If there’s no value present it will make a new node to and update value of it own it’s own.

okay i will try @mr.encoder

I hope I’ve cleared your doubt. I ask you to please rate your experience here
Your feedback is very important. It helps us improve our platform and hence provide you
the learning experience you deserve.

On the off chance, you still have some questions or not find the answers satisfactory, you may reopen
the doubt.