Longest Subarray With Sum K

Problem Statement

Find longest subarray Length having k Sum

Intuition

Approach 1: (Pos + Neg)
Map + Hashing

Intuition is
        1 2 3 6
Prefix  1 3 6 12
            |  |        
At each stage I check for sum-k
Meaning 12-6 is Required sum=6
Hence We know that subarray has sum = 6

We hash the sum at each step , But not hashing the same sum again,
Might lead to shorter sum if we update



Approach 2: For Pos only

Sliding window for sum = k


```cpp
/*
 Problem is When sum increases we decrease the window,
    But the problem is that sum can be decreased later by some negative value later

    We are not counting that

    eg->

    -1 0 1 1) -1
    As we reach high here we decrease the window, but see that

    if we want k = 0
*/
```

https://www.codingninjas.com/studio/problems/longest-subarray-with-sum-k_5713505?utm_source=striver&utm_medium=website&utm_campaign=a_zcoursetuf&leftPanelTabValue=PROBLEM

https://www.youtube.com/watch?v=frf7qxiN2qU&ab_channel=takeUforward

Approach 1:

Approach 2:

Approach 3:

Approach 4:

Similar Problems

Last updated