> For the complete documentation index, see [llms.txt](https://coding-9.gitbook.io/untitled/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://coding-9.gitbook.io/untitled/array/easy/longest-sub-array-with-sum-k.md).

# Longest Sub-Array with Sum K

## Problem Statement

\
Given an array containing **N** integers and an integer **K**., Your task is to find the length of the longest Sub-Array with the sum of the elements equal to the given value **K**.

&#x20;

**Example 1:**\
&#x20;

<pre><code><strong>Input :
</strong>A[] = {10, 5, 2, 7, 1, 9}
K = 15
<strong>Output : 4
</strong><strong>Explanation:
</strong><strong>The sub-array is {5, 2, 7, 1}.
</strong></code></pre>

**Example 2:**

<pre><code><strong>Input : 
</strong>A[] = {-1, 2, 3}
K = 6
<strong>Output : 0
</strong><strong>Explanation: 
</strong>There is no such sub-array with sum 6.
</code></pre>

**Your Task:**\
This is a function problem. The input is already taken care of by the driver code. You only need to complete the function **lenOfLongSubarr()** that takes an array **(A)**, sizeOfArray **(n)**,  sum **(K)**&#x61;nd **returns** the required length of the longest Sub-Array. The driver code takes care of the printing.

**Expected Time Complexity:** O(N).\
**Expected Auxiliary Space:** O(N).

&#x20;

**Constraints:**\
1<=N<=105\
-105<=A\[i], K<=105

## Intuition

```
Carry prefix sum
Store each element in map 

And check if Prefix sum - k exists in map
Update length accordingly

"Check out the solution in this: Prefix sum for K=0  and using running sum we calculate the answer

Zero is put in advance to find eg- sum=15 and K=15 then sum-K = 0 ;
To find this difference in hash

As an when we 
index  -1|  0   1   2    3   4   5 
arr[i]   0  |10   5   2   7    1   9
sum   0  | 10 15 17 24 25 34

Everytime we encounter the sum we get last index 
check for eg
at index 4 25-15 10 we fall on last sum

"
```

### Links

<https://practice.geeksforgeeks.org/problems/longest-sub-array-with-sum-k0809/1>

### Video Links

<https://www.youtube.com/watch?v=yDeNqw_dAU0>

### Approach 1:

```
```

{% code title="C++" lineNumbers="true" %}

```cpp
class Solution{
    public:
    int lenOfLongSubarr(int A[],  int N, int K) 
    { 
        map<int,int>mp;
        int sum=0;
        mp[0]=-1;   // for case of -2 1 1
        int l=0;            //prefix sum   -2 -1 0  but to see 0 in hash we have to put it first          
        
        // 10 5  2  7  1  9
        // 10 15 17 24 25 34
        
        for(int i=0;i<N;i++){
            sum+=A[i];
            
            if(mp.find(sum-K)!=mp.end()){
               l=max(l,i-mp[sum-K]);
            }
            
            if(mp.find(sum)==mp.end())
            {
                mp[sum]=i;
            }
            
        }
        
        return l;
        
    } 


};
```

{% endcode %}

### Approach 2:

```
```

{% code title="C++" lineNumbers="true" %}

```cpp
```

{% endcode %}

### Approach 3:

```
```

{% code title="C++" lineNumbers="true" %}

```cpp
```

{% endcode %}

### Approach 4:

```
```

{% code title="C++" lineNumbers="true" %}

```cpp
```

{% endcode %}

### Similar Problems

###


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter, and the optional `goal` query parameter:

```
GET https://coding-9.gitbook.io/untitled/array/easy/longest-sub-array-with-sum-k.md?ask=<question>&goal=<endgoal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is optional and describes the broader end goal you are ultimately trying to accomplish on behalf of the user. GitBook uses it to tailor the answer towards what is most useful for that goal.

The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
