Hi, I am not able to understand how the teacher is solving this question. the explanation is not that good please help.
Longest Subarray with sum k
@abhinavakhil55
consider the given example:- 1, -1, 5, -2, 3 and k=3.
here the prefix sum array will be:- 1, 0, 5, 3, 6
so we need to find the longest subarray with sum =3.
so we will start i=0 to end of array and at every index ‘i’ we will check the following cases:-
(i) pre[i] == k, if yes then store the length of sub array from 0 to i.
(ii) if pre[i] - k, is present in our map then also do the same.
here the map contains the values for indexes less that current index ‘i’.
We will take maximum of case (i) and (ii), at every index ‘i’.
I will also suggest you watch the previous video, Longest Subarray with zero sum then again with this video, then it will be much easy to understand for you.
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.