Given an array **arr[]** of length **N** and an integer **K**, the task is the find the maximum sum subarray with sum less than **K**.

**Note:** If K is less than minimum element, then return INT_MIN.

**Examples:**

Input:arr[] = {-1, 2, 2}, K = 4Output:3Explanation:

The subarray with maximum sum which is less than 4 is {-1, 2, 2}.

The subarray {2, 2} has maximum sum = 4, but it is not less than 4.

Input:arr[] = {5, -2, 6, 3, -5}, K =15Output:12Explanation:

The subarray with maximum sum which is less than 15 is {5, -2, 6, 3}.

**Efficient Approach:** Sum of subarray [i, j] is given by **cumulative sum till j – cumulative sum till i** of array. Now the problem reduces to finding two indexes i and j such that i < j and **cum[j] – cum[i]** are as close to **K** but lesser than it.

To solve this, iterate the array from left to right. Put the cumulative sum of i values that you have encountered till now into a set. When you are processing cum[j] what you need to retrieve from the set is the smallest number in the set which is bigger than **cum[j] – K**. This can be done in O(logN) using upper_bound on the set.

**Below is the implementation of the above approach:**

`// C++ program to find maximum sum ` `// subarray less than K ` ` ` `#include <bits/stdc++.h> ` `using` `namespace` `std; `
` ` `// Function to maximum required sum < K ` `int` `maxSubarraySum(` `int` `arr[], ` `int` `N, ` `int` `K) `
`{ ` ` ` ` ` `// Hash to lookup for value (cum_sum - K) `
` ` `set<` `int` `> cum_set; `
` ` `cum_set.insert(0); `
` ` ` ` `int` `max_sum = INT_MIN, cSum = 0; `
` ` ` ` `for` `(` `int` `i = 0; i < N; i++) { `
` ` ` ` `// getting cummulative sum from [0 to i] `
` ` `cSum += arr[i]; `
` ` ` ` `// lookup for upperbound `
` ` `// of (cSum-K) in hash `
` ` `set<` `int` `>::iterator sit `
` ` `= cum_set.upper_bound(cSum - K); `
` ` ` ` `// check if upper_bound `
` ` `// of (cSum-K) exists `
` ` `// then update max sum `
` ` `if` `(sit != cum_set.end()) `
` ` ` ` `max_sum = max(max_sum, `
` ` `cSum - *sit); `
` ` ` ` `// insert cummulative value in hash `
` ` `cum_set.insert(cSum); `
` ` `} `
` ` ` ` `// return maximum sum `
` ` `// lesser than K `
` ` `return` `max_sum; `
`} ` ` ` `// Driver code ` `int` `main() `
`{ ` ` ` ` ` `// initialise the array `
` ` `int` `arr[] = { 5, -2, 6, 3, -5 }; `
` ` ` ` `// initialise the vaue of K `
` ` `int` `K = 15; `
` ` ` ` `// size of array `
` ` `int` `N = ` `sizeof` `(arr) / ` `sizeof` `(arr[0]); `
` ` ` ` `cout << maxSubarraySum(arr, N, K); `
` ` ` ` `return` `0; `
`} ` |

*chevron_right*

*filter_none*

`# python3 program to find maximum sum ` `# subarray less than K ` `import` `sys,bisect `
` ` `# Function to maximum required sum < K ` `def` `maxSubarraySum(arr,N,K): `
` ` `# Hash to lookup for value (cum_sum - K) `
` ` `cum_set ` `=` `set` `() `
` ` `cum_set.add(` `0` `) `
` ` ` ` `max_sum ` `=` `12`
` ` `cSum ` `=` `0`
` ` ` ` `for` `i ` `in` `range` `(N): `
` ` `# getting cummulative sum from [0 to i] `
` ` `cSum ` `+` `=` `arr[i] `
` ` ` ` `# check if upper_bound `
` ` `# of (cSum-K) exists `
` ` `# then update max sum `
` ` `x ` `=` `5`
` ` `if` `x ` `in` `cum_set: `
` ` `max_sum ` `=` `max` `(max_sum,cSum ` `-` `x) `
` ` ` ` `# insert cummulative value in hash `
` ` `cum_set.add(cSum) `
` ` ` ` `# return maximum sum `
` ` `# lesser than K `
` ` `return` `max_sum `
` ` `# Driver code ` `if` `__name__ ` `=` `=` `'__main__'` `: `
` ` `# initialise the array `
` ` `arr ` `=` `[` `5` `, ` `-` `2` `, ` `6` `, ` `3` `, ` `-` `5` `] `
` ` ` ` `# initialise the vaue of K `
` ` `K ` `=` `15`
` ` ` ` `# size of array `
` ` `N ` `=` `len` `(arr) `
` ` ` ` `print` `(maxSubarraySum(arr, N, K)) `
` ` `# This code is contributed by Surendra_Gangwar ` |

*chevron_right*

*filter_none*

**Output:**

12

**Time Complexity:** O(N*Log(N))

**Similar article:** Maximum sum subarray having sum less than or equal to given sum using Sliding Window

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the **DSA Self Paced Course** at a student-friendly price and become industry ready.

## Recommended Posts:

- Maximum sum subarray having sum less than or equal to given sum
- Longest subarray with absolute difference between elements less than or equal to K using Heaps
- Largest subset having with sum less than equal to sum of respective indices
- Number of elements less than or equal to a number in a subarray : MO's Algorithm
- Longest subarray having average greater than or equal to x | Set-2
- Find all possible subarrays having product less than or equal to K
- Maximum subarray size, such that all subarrays of that size have sum less than k
- First subarray having sum at least half the maximum sum of any subarray of size K
- Length of longest subarray in which elements greater than K are more than elements not greater than K
- Node having maximum number of nodes less than its value in its subtree
- Find maximum sum array of length less than or equal to m
- Maximum product from array such that frequency sum of all repeating elements in product is less than or equal to 2 * k
- Sum of elements in 1st array such that number of elements less than or equal to them in 2nd array is maximum
- Longest subarray having average greater than or equal to x
- Find maximum number of elements such that their absolute difference is less than or equal to 1
- Count pairs having Bitwise XOR less than K from given array
- Smallest subarray from a given Array with sum greater than or equal to K | Set 2
- Maximum length of subarray such that all elements are equal in the subarray
- Number of subarrays having sum less than K
- Count of subsets having sum of min and max element less than K

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.