Skip to content

Commit 2e2361e

Browse files
committed
feat: implement sliding window algorithms for fixed and dynamic sizes with tests
1 parent 55ff0ad commit 2e2361e

4 files changed

+93
-0
lines changed

Diff for: Sliding-Windows/LongestSubarrayWithSumAtMost.js

+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
/**
2+
* Function to find the longest subarray with a sum <= target.
3+
*
4+
* @param {number[]} arr - The input array of numbers.
5+
* @param {number} target - The target sum for the dynamic window.
6+
* @returns {number} - The length of the longest subarray with a sum <= target.
7+
*/
8+
export function longestSubarrayWithSumAtMost(arr, target) {
9+
let maxLength = 0
10+
let windowSum = 0
11+
let left = 0
12+
for (let right = 0; right < arr.length; right++) {
13+
windowSum += arr[right]
14+
while (windowSum > target) {
15+
windowSum -= arr[left]
16+
left++
17+
}
18+
maxLength = Math.max(maxLength, right - left + 1)
19+
}
20+
return maxLength
21+
}

Diff for: Sliding-Windows/MaxSumSubarrayFixed.js

+26
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
/**
2+
* Function to find the maximum sum of a subarray of fixed size k.
3+
*
4+
* @param {number[]} arr - The input array of numbers.
5+
* @param {number} k - The fixed size of the subarray.
6+
* @returns {number} - The maximum sum of any subarray of size k.
7+
* @throws {RangeError} - If k is larger than the array length or less than 1.
8+
*/
9+
export function maxSumSubarrayFixed(arr, k) {
10+
if (k > arr.length || k < 1) {
11+
throw new RangeError(
12+
'Subarray size k must be between 1 and the length of the array'
13+
)
14+
}
15+
let maxSum = 0
16+
let windowSum = 0
17+
for (let i = 0; i < k; i++) {
18+
windowSum += arr[i]
19+
}
20+
maxSum = windowSum
21+
for (let i = k; i < arr.length; i++) {
22+
windowSum += arr[i] - arr[i - k]
23+
maxSum = Math.max(maxSum, windowSum)
24+
}
25+
return maxSum
26+
}
+24
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
import { longestSubarrayWithSumAtMost } from '../LongestSubarrayWithSumAtMost'
2+
3+
describe('Dynamic-size Sliding Window - longestSubarrayWithSumAtMost', () => {
4+
it('should return the longest subarray length with sum <= target', () => {
5+
const arr = [1, 2, 3, 4, 5]
6+
const target = 7
7+
const result = longestSubarrayWithSumAtMost(arr, target)
8+
expect(result).toBe(3)
9+
})
10+
11+
it('should return the full array length if the entire sum is within the target', () => {
12+
const arr = [1, 1, 1, 1]
13+
const target = 4
14+
const result = longestSubarrayWithSumAtMost(arr, target)
15+
expect(result).toBe(4)
16+
})
17+
18+
it('should return 0 if no subarray meets the sum condition', () => {
19+
const arr = [5, 6, 7]
20+
const target = 3
21+
const result = longestSubarrayWithSumAtMost(arr, target)
22+
expect(result).toBe(0)
23+
})
24+
})

Diff for: Sliding-Windows/test/MaxSumSubarrayFixed.test.js

+22
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
import { maxSumSubarrayFixed } from '../MaxSumSubarrayFixed'
2+
3+
describe('Fixed-size Sliding Window - maxSumSubarrayFixed', () => {
4+
it('should return the maximum sum of a subarray of size k', () => {
5+
const arr = [2, 1, 5, 1, 3, 2]
6+
const k = 3
7+
const result = maxSumSubarrayFixed(arr, k)
8+
expect(result).toBe(9)
9+
})
10+
11+
it('should throw a RangeError if k is larger than the array length', () => {
12+
const arr = [2, 1, 5]
13+
const k = 4
14+
expect(() => maxSumSubarrayFixed(arr, k)).toThrow(RangeError)
15+
})
16+
17+
it('should throw a RangeError if k is less than 1', () => {
18+
const arr = [2, 1, 5]
19+
const k = 0
20+
expect(() => maxSumSubarrayFixed(arr, k)).toThrow(RangeError)
21+
})
22+
})

0 commit comments

Comments
 (0)