//
// main.cpp
// Sliding Window
//
// Created by Himanshu on 18/09/21.
//
#include <iostream>
#include <climits>
using namespace std;
const int N = 7;
int solve (int A[], int k) {
int maxSum = INT_MIN, currSum = 0;
for (int i=0; i<k; i++) {
currSum += A[i];
}
maxSum = max(maxSum, currSum);
for (int i=k; i<N; i++) {
currSum += A[i] - A[i-k];
maxSum = max(maxSum, currSum);
}
return maxSum;
}
int main() {
int A[N] = {5, 3, 4, 6, 8, 11, 20};
int k = 7;
cout<<"Maximum subset sum: "<<solve(A, k)<<endl;
k = 4;
cout<<"Maximum subset sum: "<<solve(A, k)<<endl;
}
Ly8KLy8gIG1haW4uY3BwCi8vICBTbGlkaW5nIFdpbmRvdwovLwovLyAgQ3JlYXRlZCBieSBIaW1hbnNodSBvbiAxOC8wOS8yMS4KLy8KCgojaW5jbHVkZSA8aW9zdHJlYW0+CiNpbmNsdWRlIDxjbGltaXRzPgp1c2luZyBuYW1lc3BhY2Ugc3RkOwpjb25zdCBpbnQgTiA9IDc7CgppbnQgc29sdmUgKGludCBBW10sIGludCBrKSB7CgogICAgaW50IG1heFN1bSA9IElOVF9NSU4sIGN1cnJTdW0gPSAwOwogICAgCiAgICBmb3IgKGludCBpPTA7IGk8azsgaSsrKSB7CiAgICAgICAgY3VyclN1bSArPSBBW2ldOwogICAgfQogICAgCiAgICBtYXhTdW0gPSBtYXgobWF4U3VtLCBjdXJyU3VtKTsKICAgIGZvciAoaW50IGk9azsgaTxOOyBpKyspIHsKICAgICAgICBjdXJyU3VtICs9IEFbaV0gLSBBW2kta107CiAgICAgICAgbWF4U3VtID0gbWF4KG1heFN1bSwgY3VyclN1bSk7CiAgICB9CgogICAgcmV0dXJuIG1heFN1bTsKfQogCmludCBtYWluKCkgewogICAgaW50IEFbTl0gPSB7NSwgMywgNCwgNiwgOCwgMTEsIDIwfTsKICAgIGludCBrID0gNzsKICAgIAogICAgY291dDw8Ik1heGltdW0gc3Vic2V0IHN1bTogIjw8c29sdmUoQSwgayk8PGVuZGw7CiAgICAKICAgIGsgPSA0OwogICAgY291dDw8Ik1heGltdW0gc3Vic2V0IHN1bTogIjw8c29sdmUoQSwgayk8PGVuZGw7CiAgICAKfQo=