# Longest subarray having sum K | Set 2

Given an array arr[] of size N containing integers. The task is to find the length of the longest sub-array having sum equal to the given value K.
Examples:

Input: arr[] = {2, 3, 4, 2, 1, 1}, K = 10
Output:
Explanation:
The subarray {3, 4, 2, 1} gives summation as 10.

Input: arr[] = {6, 8, 14, 9, 4, 11, 10}, K = 13
Output:
Explanation:
The subarray {9, 4} gives summation as 13.

Time Complexity: O(N2)
Auxiliary Space: O(1)

Efficient Approach: The idea is to use Binary Search to find the subarray of maximum length having sum K. Below are the steps:

1. Create a prefix sum array(say pref[]) from the given array arr[].
2. For each element in the prefix array pref[] do Binary Search:
• Initialize ans, start and end variable as -1, 0 and N respectively.
• Find the middle index(say mid).
• If pref[mid] – val ≤ K then update the start variable to mid + 1 and ans to mid.
• Else update the end variable to mid – 1.
3. Return the value of ans from the above binary search.
4. If current subarray length is less than (ans – i), then update the maximum length to (ans – i).

Below is the implementation of the above approach:

## C++

 `// C++ program for the above approach ` `#include ` `using` `namespace` `std; ` ` `  `// To store the prefix sum array ` `vector<``int``> v; ` ` `  `// Function for searching the ` `// lower bound of the subarray ` `int` `bin(``int` `val, ``int` `k, ``int` `n) ` `{ ` `    ``int` `lo = 0; ` `    ``int` `hi = n; ` `    ``int` `mid; ` `    ``int` `ans = -1; ` ` `  `    ``// Iterate until low less ` `    ``// than equal to high ` `    ``while` `(lo <= hi) { ` `        ``mid = lo + (hi - lo) / 2; ` ` `  `        ``// For each mid finding sum ` `        ``// of sub array less than ` `        ``// or equal to k ` `        ``if` `(v[mid] - val <= k) { ` `            ``lo = mid + 1; ` `            ``ans = mid; ` `        ``} ` `        ``else` `            ``hi = mid - 1; ` `    ``} ` ` `  `    ``// Return the final answer ` `    ``return` `ans; ` `} ` ` `  `// Function to find the length of ` `// subarray with sum K ` `void` `findSubarraySumK(``int` `arr[], ` `                      ``int` `N, ``int` `K) ` `{ ` ` `  `    ``// Initialize sum to 0 ` `    ``int` `sum = 0; ` `    ``v.push_back(0); ` ` `  `    ``// Push the prefix sum of the ` `    ``// array arr[] in prefix[] ` `    ``for` `(``int` `i = 0; i < N; i++) { ` ` `  `        ``sum += arr[i]; ` `        ``v.push_back(sum); ` `    ``} ` ` `  `    ``int` `l = 0, ans = 0, r; ` ` `  `    ``for` `(``int` `i = 0; i < K; i++) { ` ` `  `        ``// Search r for each i ` `        ``r = bin(v[i], K, N); ` ` `  `        ``// Update ans ` `        ``ans = max(ans, r - i); ` `    ``} ` ` `  `    ``// Print the length of subarray ` `    ``// found in the array ` `    ``cout << ans; ` `} ` ` `  `// Driver Code ` `int` `main() ` `{ ` `    ``// Given array arr[] ` `    ``int` `arr[] = { 6, 8, 14, 9, 4, 11, 10 }; ` ` `  `    ``int` `N = ``sizeof``(arr) / ``sizeof``(arr); ` ` `  `    ``// Given sum K ` `    ``int` `K = 13; ` ` `  `    ``// Function Call ` `    ``findSubarraySumK(arr, N, K); ` ` `  `    ``return` `0; ` `} `

## Java

 `// Java program for the above approach ` `import` `java.util.*; ` ` `  `class` `GFG{ ` ` `  `// To store the prefix sum array ` `static` `Vector v = ``new` `Vector(); ` ` `  `// Function for searching the ` `// lower bound of the subarray ` `static` `int` `bin(``int` `val, ``int` `k, ``int` `n) ` `{ ` `    ``int` `lo = ``0``; ` `    ``int` `hi = n; ` `    ``int` `mid; ` `    ``int` `ans = -``1``; ` ` `  `    ``// Iterate until low less ` `    ``// than equal to high ` `    ``while` `(lo <= hi)  ` `    ``{ ` `        ``mid = lo + (hi - lo) / ``2``; ` ` `  `        ``// For each mid finding sum ` `        ``// of sub array less than ` `        ``// or equal to k ` `        ``if` `(v.get(mid) - val <= k)  ` `        ``{ ` `            ``lo = mid + ``1``; ` `            ``ans = mid; ` `        ``} ` `        ``else` `            ``hi = mid - ``1``; ` `    ``} ` ` `  `    ``// Return the final answer ` `    ``return` `ans; ` `} ` ` `  `// Function to find the length of ` `// subarray with sum K ` `static` `void` `findSubarraySumK(``int` `arr[], ` `                             ``int` `N, ``int` `K) ` `{ ` ` `  `    ``// Initialize sum to 0 ` `    ``int` `sum = ``0``; ` `    ``v.add(``0``); ` ` `  `    ``// Push the prefix sum of the ` `    ``// array arr[] in prefix[] ` `    ``for``(``int` `i = ``0``; i < N; i++)  ` `    ``{ ` `        ``sum += arr[i]; ` `        ``v.add(sum); ` `    ``} ` ` `  `    ``int` `l = ``0``, ans = ``0``, r; ` ` `  `    ``for``(``int` `i = ``0``; i < v.size(); i++)  ` `    ``{ ` `         `  `        ``// Search r for each i ` `        ``r = bin(v.get(i), K, N); ` ` `  `        ``// Update ans ` `        ``ans = Math.max(ans, r - i); ` `    ``} ` ` `  `    ``// Print the length of subarray ` `    ``// found in the array ` `    ``System.out.print(ans); ` `} ` ` `  `// Driver Code ` `public` `static` `void` `main(String[] args) ` `{ ` `     `  `    ``// Given array arr[] ` `    ``int` `arr[] = { ``6``, ``8``, ``14``, ``9``, ``4``, ``11``, ``10` `}; ` ` `  `    ``int` `N = arr.length; ` ` `  `    ``// Given sum K ` `    ``int` `K = ``13``; ` ` `  `    ``// Function call ` `    ``findSubarraySumK(arr, N, K); ` `} ` `} ` ` `  `// This code is contributed by gauravrajput1 `

## C#

 `// C# program for the above approach ` `using` `System; ` `using` `System.Collections.Generic; ` ` `  `class` `GFG{ ` ` `  `// To store the prefix sum array ` `static` `List<``int``> v = ``new` `List<``int``>(); ` ` `  `// Function for searching the ` `// lower bound of the subarray ` `static` `int` `bin(``int` `val, ``int` `k, ``int` `n) ` `{ ` `    ``int` `lo = 0; ` `    ``int` `hi = n; ` `    ``int` `mid; ` `    ``int` `ans = -1; ` ` `  `    ``// Iterate until low less ` `    ``// than equal to high ` `    ``while` `(lo <= hi)  ` `    ``{ ` `        ``mid = lo + (hi - lo) / 2; ` ` `  `        ``// For each mid finding sum ` `        ``// of sub array less than ` `        ``// or equal to k ` `        ``if` `(v[mid] - val <= k)  ` `        ``{ ` `            ``lo = mid + 1; ` `            ``ans = mid; ` `        ``} ` `        ``else` `            ``hi = mid - 1; ` `    ``} ` ` `  `    ``// Return the final answer ` `    ``return` `ans; ` `} ` ` `  `// Function to find the length of ` `// subarray with sum K ` `static` `void` `findSubarraySumK(``int` `[]arr, ` `                             ``int` `N, ``int` `K) ` `{ ` ` `  `    ``// Initialize sum to 0 ` `    ``int` `sum = 0; ` `    ``v.Add(0); ` ` `  `    ``// Push the prefix sum of the ` `    ``// array []arr in prefix[] ` `    ``for``(``int` `i = 0; i < N; i++)  ` `    ``{ ` `        ``sum += arr[i]; ` `        ``v.Add(sum); ` `    ``} ` ` `  `    ``int` `ans = 0, r; ` ` `  `    ``for``(``int` `i = 0; i < v.Count; i++)  ` `    ``{ ` `         `  `        ``// Search r for each i ` `        ``r = bin(v[i], K, N); ` ` `  `        ``// Update ans ` `        ``ans = Math.Max(ans, r - i); ` `    ``} ` ` `  `    ``// Print the length of subarray ` `    ``// found in the array ` `    ``Console.Write(ans); ` `} ` ` `  `// Driver Code ` `public` `static` `void` `Main(String[] args) ` `{ ` `     `  `    ``// Given array []arr ` `    ``int` `[]arr = { 6, 8, 14, 9, 4, 11, 10 }; ` ` `  `    ``int` `N = arr.Length; ` ` `  `    ``// Given sum K ` `    ``int` `K = 13; ` ` `  `    ``// Function call ` `    ``findSubarraySumK(arr, N, K); ` `} ` `} ` ` `  `// This code is contributed by gauravrajput1`

Output:

```2
```

Time Complexity: O(N*log2N)
Auxiliary Space: O(N) My Personal Notes arrow_drop_up Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.

Improved By : GauravRajput1

4

Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.