CMIMC 2017 Computer Science Problem 8

We have a collection of 1720 balls, half of which are black and half of which are white, aligned in a straight line. Our task is to make the balls alternating in color along the line. The following greedy algorithm accomplishes that task:

1: \ \textbf{FOR } i \textbf{IN } [2,3, \ldots, 2 n]
2: \ \quad \textbf{IF} balls i-1 and i have the same color:
3: \ \quad \quad j \leftarrow smallest index greater than i for which balls i-1 and j have different colors
4: \ \quad \quad swap balls i and j

Given a configuration C, let \hat{\sigma}(C) denote the number of swaps the greedy algorithm takes, and let \sigma(C) denote the minimum number of swaps actually necessary to perform the task. Find the maximum value over all configurations C of \hat{\sigma}(C)-\sigma(C).