![]() |
VOOZH | about |
Given a binary circular array arr[] of size N, the task is to find the minimum swaps to group all 0s together in the array.
Examples:
Input: arr[] = {1, 0, 1, 0, 0, 1, 1}
Output: 1
Explanation: Here are a few of the ways to group all the 0's together:
- {1, 0, 0, 1, 1} using 1 swap.
- {1, 0, 0, 1, 1} using 1 swap.
- {0, 0, 1, 1, 1, 1, 0} using 2 swaps (using the circular property of the array).
There is no way to group all 0's together with 0 swaps. Thus, the minimum number of swaps required is 1.
Input: arr[] = {0, 0, 1, 1, 0}
Output: 0
Explanation: All the 0's are already grouped together due to the circular property of the array.
Thus, the minimum number of swaps required is 0.
Approach: The task can be solved using the sliding window technique. Follow the below steps to solve the problem:
Below is the implementation of the above approach.
1
Time Complexity: O(N)
Auxiliary Space: O(1)