-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathprefixSum1.cpp
More file actions
59 lines (39 loc) · 987 Bytes
/
Copy pathprefixSum1.cpp
File metadata and controls
59 lines (39 loc) · 987 Bytes
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
59
#include<bits/stdc++.h>
using namespace std;
int maxSum(int arr[],int n, int k){
int start = 0;
int end = k;
int currentValue = 0;
for (int i=start; i<end; i++){
currentValue += arr[i];
}
int maxValue = currentValue;
while (end<n){
currentValue -= arr[start++];
currentValue += arr[end++];
maxValue = max(maxValue,currentValue);
}
return maxValue;
}
int minSum(int arr[],int n,int target){
int minValue = n;
int start=0,end=0;
int currentValue = 0;
while (end<n){
currentValue += arr[end++];
while (currentValue>target){
minValue = min(minValue,end-start);
currentValue -= arr[start++];
}
}
return minValue;
}
int main(){
int n = 10;
int arr[] = {1, 11, 100, 1, 0, 200, 3, 2, 1, 250};
int target = 280;
int ans = maxSum(arr,n,target);
int m = minSum(arr,n,target);
cout<<m<<endl;
return 0;
}