Given a binary circular array nums, you may perform swaps of any two elements in nums. Return the minimum number of swaps needed to make all values equal to 1 appear in a single contiguous block (considering the circular wrap around).
Note: Since nums is circular, a contiguous block of 1s may wrap from the end of the array to the beginning.
Constraints:
Input: nums = [0,1,0,1,1,0,0]-List[int]
Output: 1-int
Explanation: There are 3 ones in total, so we want a circular window of length 3 containing as many 1s as possible. The best such window contains 2 ones (and 1 zero), so we need 1 swap to replace that zero with a one from outside the window.
Input: nums = [1,1,0,0,1]-List[int]
Output: 0-int
Explanation: There are 3 ones in total. Considering circular wrap-around, the window formed by indices [4,0,1] is [1,1,1], which is already a contiguous block of ones in the circular sense, so no swaps are needed.
Input: nums = [1,0,1,0,1,0]-List[int]
Output: 1-int
Explanation: There are 3 ones in total, so we use a window of size 3. The maximum number of ones in any circular window of length 3 is 2, so each best window has 1 zero inside it. Swapping that zero with a one outside the window groups all ones together in 1 swap.
Input: nums = [0,0,0,1,0]-List[int]
Output: 0-int
Explanation: There is 1 one in total, and a single 1 is already a contiguous block of length 1 anywhere in the circle, so no swaps are required.
Input: nums = [1,1,1,1]-List[int]
Output: 0-int
Explanation: All elements are 1, so they already form one contiguous block (the whole array), and the minimum number of swaps is 0.
Now, let’s take a moment to make sure you’ve correctly understood the problem. The quiz below helps you check if you’re solving the correct problem: