Min Adj Swaps to Group Red Balls - Company-specific OAs / Microsoft OA

https://algo.monster/problems/min_adj_swaps_to_group_red_balls

Hi,
Can you explain the rationale behind the formula “reds[end_ptr] - reds[start_ptr] - end_ptr + start_ptr”?

Thanks.