Sliding window algorithm