File tree 5 files changed +61
-16
lines changed
Algorithms/Sliding Window/Max subarr sum of size k
5 files changed +61
-16
lines changed Original file line number Diff line number Diff line change 1
1
{
2
+ "version" : " 2.0.0" ,
2
3
"tasks" : [
3
4
{
4
- "type " : " cppbuild " ,
5
- "label " : " C/C++: gcc.exe build active file " ,
6
- "command" : " C: \\ MinGW \\ bin \\ gcc .exe" ,
5
+ "label " : " Compile and Run " ,
6
+ "type " : " shell " ,
7
+ "command" : " cmd .exe" ,
7
8
"args" : [
8
- " -fdiagnostics-color=always" ,
9
- " -g" ,
10
- " ${file}" ,
11
- " -o" ,
12
- " ${fileDirname}\\ ${fileBasenameNoExtension}.exe"
9
+ " /C" ,
10
+ " echo Compiling ${fileBasename}... && g++ \" ${file}\" -o \" ${fileDirname}/${fileBasenameNoExtension}.exe\" && echo Running program... && \" ${fileDirname}/${fileBasenameNoExtension}.exe\" < \" ${fileDirname}/input.txt\" > \" ${fileDirname}/output.txt\" && echo Output saved to output.txt && type \" ${fileDirname}/output.txt\" "
13
11
],
14
12
"options" : {
15
- "cwd" : " C:\\ MinGW\\ bin"
13
+ "cwd" : " ${fileDirname}"
14
+ },
15
+ "presentation" : {
16
+ "reveal" : " always" ,
17
+ "panel" : " shared" ,
18
+ "clear" : true ,
19
+ "showReuseMessage" : false
16
20
},
17
- "problemMatcher" : [
18
- " $gcc"
19
- ],
20
21
"group" : {
21
22
"kind" : " build" ,
22
23
"isDefault" : true
23
24
},
24
- "detail " : " Task generated by Debugger. "
25
+ "problemMatcher " : [ " $gcc " ]
25
26
}
26
- ],
27
- "version" : " 2.0.0"
28
- }
27
+ ]
28
+ }
Original file line number Diff line number Diff line change
1
+ 7 3
2
+ 2 1 5 1 3 2 1
Original file line number Diff line number Diff line change
1
+ /* Problem Statement:
2
+ Maximum Sum Subarray of Size K
3
+ Given an integer array arr of size n and an integer k, find the maximum sum of any contiguous subarray of size k.
4
+ */
5
+
6
+ #include < bits/stdc++.h>
7
+ using namespace std ;
8
+
9
+ void maxSumSubarray (int arr[], int n, int k){
10
+ int i = 0 , j = 0 ;
11
+ int sum = 0 ;
12
+ int mx = INT_MIN;
13
+ while (j < n){
14
+ sum += arr[j];
15
+ // Achieve window size
16
+ if ((j - i + 1 ) < k){
17
+ j++;
18
+ }else if ((j - i + 1 ) == k){ // Maintain window size
19
+ mx = max (mx, sum);
20
+ sum -= arr[i];
21
+ i++;
22
+ j++;
23
+ }
24
+ }
25
+
26
+ cout << mx << endl;
27
+ }
28
+
29
+ int main (){
30
+ int n, k;
31
+ cin >> n >> k;
32
+
33
+ int arr[n];
34
+
35
+ for (int i = 0 ; i < n; i++){
36
+ cin >> arr[i];
37
+ }
38
+
39
+ maxSumSubarray (arr, n, k);
40
+
41
+ return 0 ;
42
+ }
Original file line number Diff line number Diff line change
1
+ 9
You can’t perform that action at this time.
0 commit comments