Skip to content
Interview

Interview

Sliding Window Technique: Maximum Sum Subarray of Size k in O(n)

· Sliding window: max sum subarray of size k | har step pe sirf do kaam

The sliding window technique finds the largest sum of k consecutive numbers without re-adding each window: add the new number, subtract the old one. For [2, 1, 5, 1, 3, 2] with k = 3 the sums are 8, 7, 9, 6, so the max is 9. O(n), not O(n·k).

Interview SheetPractise the patterns behind interview problemsProblems grouped by pattern, each with the idea first and the code in four languages.