The last window ends at the last index
Updated
The last legal window has R = n-1 and L = n-k. Stop when the next add would use index n. R never exceeds n-1.
What to focus on
If n == k the first window is also the last: L=0 R=n-1.
Worked examples
last of four
a = [1, 2, 3, 4], k=2- n-k = 2
- R = 3 = n-1
- window [3, 4]
Result: last L=2 R=3
last of three
a = [4, 1, 9], k=2- L = 1
- R = 2
- window [1, 9]
Result: last L=1 R=2
no last
a = [7], k=2- n < k
- there is no R = n-1 window of length 2
Result: no window
Try it
Write lastWindow(a, k). a is an array of integers. k is the fixed length. The last window includes a[n-k..n-1] when k ≤ n. R is n-1. Return those bounds, or report no window when k > n. On [1, 2, 3, 4] k=2 → L=2 R=3. On [4, 1, 9] k=2 → L=1 R=2. On [7] k=2 → no window.
a is nonempty. k ≥ 1. When k > n there is no last window.