1 and store it in index_of_1.n and store it in index_of_n.1 to the beginning: swaps_for_1 = index_of_1.n to the end: swaps_for_n = (n - 1) - index_of_n.index_of_1 is greater than index_of_n, subtract one from the total swaps since one swap will move both 1 and n.