
- C++ Basics
- C++ Home
- C++ Overview
- C++ Environment Setup
- C++ Basic Syntax
- C++ Comments
- C++ Data Types
- C++ Variable Types
- C++ Variable Scope
- C++ Constants/Literals
- C++ Modifier Types
- C++ Storage Classes
- C++ Operators
- C++ Loop Types
- C++ Decision Making
- C++ Functions
- C++ Numbers
- C++ Arrays
- C++ Strings
- C++ Pointers
- C++ References
- C++ Date & Time
- C++ Basic Input/Output
- C++ Data Structures
- C++ Object Oriented
- C++ Classes & Objects
- C++ Inheritance
- C++ Overloading
- C++ Polymorphism
- C++ Abstraction
- C++ Encapsulation
- C++ Interfaces
Maximum subarray size, such that all subarrays of that size have sum less than k in C++ program
In this problem, we are given an array arr[] consisting of n positive integers and an integer k. Our task is to create a program to find the Maximum subarray size, such that all subarrays of that size have sum less than k.
Problem Description − we need to find the largest size of a subarray, such that all subarray created of the size from the elements of the array will have the sum of elements less than or equal to k.
Let’s take an example to understand the problem,
Input
arr[n] = {4, 1, 3, 2}, k = 9
Output
3
Explanation
All subarrays of size 3 and their sum −
{4, 1, 3} = 8 {1, 3, 2} = 6 The sum of all subarrays of size 3 is less than or equal to k.
Solution Approach
A simple solution to the problem is by finding the subarray which can have a size greater than k. For this, we will create a prefix sum which denotes the sum of elements till the given index. For this prefix sum, we will find the maximum result which is less than k and the index of this will be our result. Here, we have used the fact that if the prefix sum is greater than k for any size, and all the rest have sum less, then all subarray of size −1 length will have sum less than k.
Example
Program to illustrate the working of our solution,
#include<iostream> using namespace std; int calcSubArraySize(int arr[], int n, int k){ int prefixSum[n + 1]; prefixSum[0] = 0; for (int i = 0; i < n; i++) prefixSum[i + 1] = prefixSum[i] + arr[i]; // Searching size int maxLen = −1; int start = 1, end = n; int mid, i; while (start <= end){ int mid = (start + end) / 2; for (i = mid; i <= n; i++){ if (prefixSum[i] − prefixSum[i − mid] > k) break; } if (i == n + 1){ start = mid + 1; maxLen = mid; } else end = mid − 1; } return maxLen; } int main(){ int arr[] = {4, 1, 2, 3}; int n = sizeof(arr) / sizeof(arr[0]); int k = 9; cout<<"The maximum subarray size, such that all subarrays of that size have sum less than k is "<<calcSubArraySize(arr, n, k); return 0; }
Output
This method is efficient but a better approach can be made to solve the problem,
In this approach, we will use the sliding Window method for finding the sum of the subarray. Starting by taking all elements we will find the length till which the sum remains above k. And then return the length − 1 which is the maximum size of the subarray for which the sum of all subarrays is less than or equal to 0.
Example
Program to illustrate the working of our solution,
#include <iostream> using namespace std; int calcSubArraySizeSW(int arr[], int n, int k){ int maxLen = n; int subArraySum = 0; int start = 0; for (int end = 0; end < n; end++){ subArraySum += arr[end]; while (subArraySum > k) { subArraySum −= arr[start]; start++; maxLen = min(maxLen, end − start + 1); if (subArraySum == 0) break; } if (subArraySum == 0) { maxLen = −1; break; } } return maxLen; } int main(){ int arr[] = { 4, 1, 3, 2, 6 }; int k = 12; int n = sizeof(arr)/ sizeof(arr[0]); cout<<"The maximum subarray size, such that all subarrays of that size have sum less than k is "<<calcSubArraySizeSW(arr, n, k); return 0; }
Output
The maximum subarray size, such that all subarrays of that size have sum less than k is 4
- Related Articles
- Maximum subarray size, such that all subarrays of that size have sum less than k in C++
- Maximum Size Subarray Sum Equals k in C++
- Maximum of all Subarrays of size k using set in C++ STL
- Find maximum (or minimum) sum of a subarray of size k in C++
- Maximum length of subarray such that sum of the subarray is even in C++
- Maximum product from array such that frequency sum of all repeating elements in product is less than or equal to 2 * k in C++
- Maximum Unique Element in every subarray of size K in c++
- Maximum sum two non-overlapping subarrays of given size in C++
- Maximum sum subarray such that start and end values are same in C++ Program
- Max sum of M non-overlapping subarrays of size K in C++
- Find the Number of subarrays having sum less than K using C++
- Subarray Product Less Than K in C++
- Maximum sum subarray such that start and end values are same in C++
- Minimum Size Subarray Sum in C++
- Count all possible groups of size 2 or 3 that have sum as multiple of 3 in C++
