Skip to content

Commit bfdc3db

Browse files
committed
Changes in tasks.json and added Sliding window: Max subarray sum of size k
1 parent 7a43f5f commit bfdc3db

File tree

5 files changed

+61
-16
lines changed

5 files changed

+61
-16
lines changed

.vscode/tasks.json

+16-16
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,28 @@
11
{
2+
"version": "2.0.0",
23
"tasks": [
34
{
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",
78
"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\""
1311
],
1412
"options": {
15-
"cwd": "C:\\MinGW\\bin"
13+
"cwd": "${fileDirname}"
14+
},
15+
"presentation": {
16+
"reveal": "always",
17+
"panel": "shared",
18+
"clear": true,
19+
"showReuseMessage": false
1620
},
17-
"problemMatcher": [
18-
"$gcc"
19-
],
2021
"group": {
2122
"kind": "build",
2223
"isDefault": true
2324
},
24-
"detail": "Task generated by Debugger."
25+
"problemMatcher": ["$gcc"]
2526
}
26-
],
27-
"version": "2.0.0"
28-
}
27+
]
28+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
7 3
2+
2 1 5 1 3 2 1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
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+
}
Binary file not shown.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
9

0 commit comments

Comments
 (0)