-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathak_30_1_Max_Sum_Subarray.cpp
More file actions
58 lines (49 loc) · 1.02 KB
/
ak_30_1_Max_Sum_Subarray.cpp
File metadata and controls
58 lines (49 loc) · 1.02 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
// Sliding Window 1. Max Sum Sub Array
#include<bits/stdc++.h>
using namespace std;
#define vi vector<int>
#define pii pair<int,int>
#define vii vector<pair>
#define rep(i,a,b) for(int i=a; i<b; i++)
#define ff first
#define ss second
#define setBits(x) builtin_popcount(x)
#define lli long long int
/*
k = subarray size
x = max of sum of subarray should be less than x
n = size of array ar[]
complexity = O(n)
*/
void maxSubarraySum(int ar[], int n, int k, int x)
{
int sum = 0,ans=0;
for(int i=0; i<k; i++)
sum += ar[i];
if(sum<x)
ans = sum;
for(int i=k; i<n; i++)
{
sum = sum+ar[i]-ar[i-k];
if(sum<x) //if(sum<x && ans<sum)
ans = max(sum, ans);
}
cout<<ans<<" is Max Sub-array Sum\n";
}
int main()
{
int n,x,k;
cin>>n;
int ar[n];
for(int i=0; i<n; i++)
cin>>ar[i];
cin>>k>>x;
maxSubarraySum(ar, n,k,x);
return 0;
}
/*
6
7 5 4 6 8 9
3 20
18 is Max Sub-array Sum
*/