Data Structures19 sections · 729 units
Open in Course

The Subarray Problem

Contiguous elements

New problem type: find some property of contiguous subarrays. Maybe the maximum sum, or the longest with distinct elements, or the shortest with sum at least kk.

Brute force: enumerate all subarrays. There are O(n2)O(n^2) of them, and processing each takes O(n)O(n). That's O(n3)O(n^3). Sliding window reduces this to O(n)O(n) for many problems.