forked from harshitbansal373/c-plus-plus
-
Notifications
You must be signed in to change notification settings - Fork 0
/
maxSum.cpp
48 lines (39 loc) · 949 Bytes
/
maxSum.cpp
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
/*without window sliding technique
Given an array of integers of size ‘n’.
Our aim is to calculate the maximum sum of ‘k’
consecutive elements in the array.
Input : arr[] = {100, 200, 300, 400}
k = 2
Output : 700
Input : arr[] = {1, 4, 2, 10, 23, 3, 1, 0, 20}
k = 4
Output : 39
We get maximum sum by adding subarray {4, 2, 10, 23}
of size 4.
Input : arr[] = {2, 3}
k = 3
Output : Invalid
There is no subarray of size 3 as size of whole
array is 2.
*/
#include <bits/stdc++.h>
using namespace std;
int maxsum(int *a,int n,int k){
int max_sum=INT_MIN;
for(int i=0;i<=n-k;i++){
int current_sum=0;
for(int j=i;j<i+k;j++){
current_sum=current_sum+a[j];
}
max_sum=max(current_sum,max_sum);
}
return max_sum;
}
int main() {
// your code goes here
int arr[] = {1, 4, 2, 10, 23, 3, 1, 0, 20};
int k = 4;
int n = sizeof(arr) / sizeof(arr[0]);
cout<<maxsum(arr,n,k);
return 0;
}