- Trending Categories
- Data Structure
- Networking
- RDBMS
- Operating System
- Java
- iOS
- HTML
- CSS
- Android
- Python
- C Programming
- C++
- C#
- MongoDB
- MySQL
- Javascript
- PHP

- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who

# Maximum sum subarray having sum less than or equal to given sums in C++

In this problem, we are given an array and a sum. Our task is to create a program that will find the maximum sum subarray having a sum less than or equal to given sums in c++.

We have to find the subarray of any length less than or equal to n whose sum is less than or equal to the given sum.

Let’s take an example to understand the problem,

**Input** − array = {3, 5, 1, 8, 2, 9}, sum = 25

**Output** − 25

**Explanation** − The subarrays with sum less than or equal to 25 are {5, 1, 8, 2, 9}

One simple method to find the maximum sum subarray is by iterating over the array and find the sum of all subarray and then finding the nearest or equal sum. But this method will have time complexity of O(n*n) as two loops are required.

A more efficient method to solve this problem is by using the *sliding window* method. In which we will check the current sum with the max sum and add or subtract elements to the window based on the comparison.

## Example

Program to illustrate the working of our solution,

#include <iostream> using namespace std; int findMax(int a, int b){ if(a>b) return a; return b; } int maxSumsubarray(int arr[], int n, int maxSum){ int sum = arr[0], overallMax = 0, start = 0; for (int i = 1; i < n; i++) { if (sum <= maxSum) overallMax = findMax(overallMax, sum); while (sum + arr[i] > maxSum && start < i) { sum -= arr[start]; start++; } sum += arr[i]; } if (sum <= maxSum) overallMax = findMax(overallMax, sum); return overallMax; } int main(){ int arr[] = {3, 1, 4, 7, 2, 9, 5}; int n = sizeof(arr) / sizeof(arr[0]); int sum = 20; cout<<"The maximum sum of subarray with sum less than or equal to "<<sum<<" is "<<maxSumsubarray(arr, n, sum); return 0; }

## Output

The maximum sum of subarray with sum less than or equal to 20 is 18

- Related Questions & Answers
- Find maximum sum array of length less than or equal to m in C++
- Maximum Side Length of a Square with Sum Less than or Equal to Threshold in C++
- Largest subarray having sum greater than k in C++
- Print triplets with sum less than or equal to k in C Program
- Number of elements less than or equal to a given number in a given subarray in C++
- Longest Continuous Subarray With Absolute Diff Less Than or Equal to Limit in C++
- Maximum Subarray Sum in a given Range in C++
- Maximum subarray size, such that all subarrays of that size have sum less than k in C++
- Count numbers (smaller than or equal to N) with given digit sum in C++
- Find the Number of subarrays having sum less than K using C++
- Maximum Sum Circular Subarray in C++
- Maximum sum bitonic subarray in C++
- Maximum circular subarray sum in C++
- Two Sum Less Than K in Python
- Maximum subarray size, such that all subarrays of that size have sum less than k in C++ program