Longest subarray with sum k using sliding window

wrong output

Hey @nmaurya760
On line 19 it should be sum=sum-arr[i]; and not arr[j]

I have written sum-=arr[i]

Look again in the code u posted


this is also giving wrong output

Hey @nmaurya760 I just checked your code have many errors
So which algo are you exactly trying to implement ? Tell me that & I will help you in it .
Also tell the complexity you are trying to achieve.

i an trying to use sliding window concept
o(n)

Oh my bad I haven’t read the title properly .
Here I updated your code with corrections and mentioned them in comments
I f you don’t understand something then let me know :https://ide.codingblocks.com/s/325162

Why you have added line 34 and 35
When sum==k
Then we just have to update our length??

When sum is k
we update our length but if we dont add those lines then always sum will remain k and we are stuck in infinite loop.
So hence we remove arr[i] from sum and increment i for next window