xxxxxxxxxx
function maximumSum(arr,n,k)
{
//we added the sum o first k elements
//let k =4 then k-1 would be upto 0-3 element i.e 4 elements
max_sum =0;
for(let i=0;i<=k-1;i++)
{
max_sum = max_sum+arr[i];
}
current_sum = max_sum;
//Now we added the sum after k elements till size of array
//Now we started the loop with k i.e 5th element till end of the array 9 element(i=8)
for(let i=k;i<n;i++)
{
//we are substracting previous i-th element from the total sum and adding the i-th element to get current sum
//
current_sum = current_sum - arr[i-k]+arr[i];
//Now comparing maximum sum
max_sum = Math.max(current_sum,max_sum);
}
return max_sum;
}
xxxxxxxxxx
// A sliding window is a sub-list that runs over an underlying collection.
// I.e., if you have an array like:
let array = [a b c d e f g h]
// a sliding window of size 3 would run over it like:
// [ a b c ]
// [ b c d ]
// [ c d e ]
// [ d e f ]
// [ e f g ]
// [ f g h ]
xxxxxxxxxx
const getLargestSumOfFiveConsecutiveElements = (arr) => {
let currSum = getSum(arr, 0, 4);
let largestSum = currSum;
for (let i = 1; i <= arr.length - 5; i++) {
currSum -= arr[i - 1]; // subtract element to the left of curr window
currSum += arr[i + 4]; // add last element in curr window
largestSum = Math.max(largestSum, currSum);
}
return largestSum;
};
const getSum = (arr, start, end) => {
let sum = 0;
for (let i = start; i <= end; i++) {
sum += arr[i];
}
return sum;
};
xxxxxxxxxx
/*
1. Fixed-Size Sliding Window
Problem:
Suppose the problem gives us an array of length n and a number k.
The problem asks us to find the maximum sum of k consecutive elements inside the array.
*/
let array = [-1, 2, -3, 4, -5, 6, -7, 8]
let k = 3
let max = null
/*
Naive approach
The time complexity is O(n^2).
In the worst case if k = n / 2, then time complexity is: n^2/4
*/
for (let i = 0; i < array.length - k + 1; i++) {
let sum = 0
for (let j = 0; j < k; j++) {
sum += array[i + j]
}
if (max === null) {
max = sum
} else if (max < sum) {
max = sum
}
}
/*
Sliding window algorithm,
time complexity is: k + -1 + n - k + 1 = n
*/
let sum = 0
for (let i = 0; i < k; i++) { sum += array[i] }
for (let i = 1; i < array.length - k + 1; i++) {
if (max === null) {
max = sum
}
sum = sum - array[i - 1] + array[i + k - 1]
if (max < sum) {
max = sum
}
}
console.log(max) // [Log]: 7
xxxxxxxxxx
def sliding_window(sequence, window_size):
X = []
y = []
for i in range(len(sequence) - window_size):
X.append(sequence[i:i + window_size])
y.append(sequence[i + window_size])
return X, y
xxxxxxxxxx
function minSubArrayLen(arr, target) {
let minLength = Infinity
let sum = 0
let left = 0
let right = 0
while (left < arr.length) {
if (sum >= target) {
// store the current minimal length
minLength = Math.min(minLength, (right - left))
// shrink the window:
// (1) subtract the value at left idx
// (2) move the left panel one index further to the right
sum -= arr[left]
left++
} else if (sum < target && right < arr.length) {
// expand the window:
// (1) sum up the current value
// (2) move the right panel one index further to the right
sum += arr[right]
right++
} else {
break
}
}
return minLength === Infinity ? 0 : minLength
}
xxxxxxxxxx
// O(n*k) solution for finding maximum sum of
// a subarray of size k
#include <bits/stdc++.h>
using namespace std;
// Returns maximum sum in a subarray of size k.
int maxSum(int arr[], int n, int k)
{
// Initialize result
int max_sum = INT_MIN;
// Consider all blocks starting with i.
for (int i = 0; i < n - k + 1; i++) {
int current_sum = 0;
for (int j = 0; j < k; j++)
current_sum = current_sum + arr[i + j];
// Update result if required.
max_sum = max(current_sum, max_sum);
}
return max_sum;
}
// Driver code
int main()
{
int arr[] = { 1, 4, 2, 10, 2, 3, 1, 0, 20 };
int k = 4;
int n = sizeof(arr) / sizeof(arr[0]);
cout << maxSum(arr, n, k);
return 0;
}
// This code is contributed by Aditya Kumar (adityakumar129)