Bird
0
0
DSA Cprogramming~10 mins

Sliding Window on Arrays in DSA C - Execution Trace

Choose your learning style9 modes available
Concept Flow - Sliding Window on Arrays
Initialize window start and end pointers
Expand window by moving end pointer
Check if window size reached target
Keep expanding
Slide window forward by moving start pointer
Repeat until end reaches array end
The sliding window moves over the array by expanding the end pointer until the window size is reached, then processes the window and slides forward by moving the start pointer.
Execution Sample
DSA C
int arr[] = {1, 3, 5, 2, 8, 7};
int k = 3;
for (int i = 0; i <= 6 - k; i++) {
    int sum = 0;
    for (int j = i; j < i + k; j++) {
        sum += arr[j];
    }
    printf("%d ", sum);
}
Calculates sum of every subarray of size 3 using a sliding window approach.
Execution Table
StepOperationWindow StartWindow EndWindow ElementsSum ComputedVisual State
1Initialize window02[1, 3, 5]0Window covers indices 0 to 2
2Calculate sum02[1, 3, 5]9Sum = 1+3+5 = 9
3Slide window forward13[3, 5, 2]0Window moves to indices 1 to 3
4Calculate sum13[3, 5, 2]10Sum = 3+5+2 = 10
5Slide window forward24[5, 2, 8]0Window moves to indices 2 to 4
6Calculate sum24[5, 2, 8]15Sum = 5+2+8 = 15
7Slide window forward35[2, 8, 7]0Window moves to indices 3 to 5
8Calculate sum35[2, 8, 7]17Sum = 2+8+7 = 17
9End----Window end reached array end, stop
💡 Window end pointer reached the last index of the array, no more windows of size k possible.
Variable Tracker
VariableStartAfter Step 2After Step 4After Step 6After Step 8Final
window_start001233
window_end223455
sum0910151717
Key Moments - 3 Insights
Why do we move the window start pointer after processing the window?
After calculating the sum for the current window (see steps 2,4,6,8), we move the start pointer forward to slide the window and consider the next subarray. This keeps the window size constant.
Why does the loop stop when window_end reaches the last index?
Because the window size is fixed (k=3), once the window end pointer reaches the last element, no further full windows of size k can be formed. This is shown in step 9 where execution stops.
Why do we reset sum to 0 before calculating each window's sum?
Sum is reset before each calculation (steps 2,4,6,8) to avoid adding previous window sums repeatedly. This ensures each window's sum is calculated fresh from its elements.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution table, what is the sum computed at step 6?
A15
B17
C10
D9
💡 Hint
Check the 'Sum Computed' column at step 6 in the execution_table.
At which step does the window start pointer first move to index 2?
AStep 3
BStep 4
CStep 5
DStep 6
💡 Hint
Look at the 'Window Start' column in the execution_table to see when it changes to 2.
If the window size k was 2 instead of 3, how would the number of steps change?
AFewer steps because windows are bigger
BMore steps because more windows fit
CSame number of steps
DNo steps because window size is invalid
💡 Hint
Consider how many subarrays of size k fit in the array length (6) and check execution_table for k=3.
Concept Snapshot
Sliding Window on Arrays:
- Use two pointers: start and end
- Expand end to grow window until size k
- Process window (e.g., sum elements)
- Slide window by moving start forward
- Repeat until end reaches array end
- Efficient for fixed-size subarray problems
Full Transcript
Sliding window on arrays uses two pointers to create a moving window over the array. We start with the window covering the first k elements. We calculate the sum of these elements. Then we slide the window forward by moving the start pointer one step and the end pointer one step, keeping the window size constant. We repeat this process until the end pointer reaches the last element of the array. This method helps efficiently process all subarrays of size k without recalculating sums from scratch each time.