Input : arr[] = [0, 0, 1, 0, 1, 0, 1, 1]
Output : 3
Explanation:
1st swap : [0, 0, 1, 0, 0, 1, 1, 1]
2nd swap : [0, 0, 0, 1, 0, 1, 1, 1]
3rd swap : [0, 0, 0, 0, 1, 1, 1, 1]
Input : arr[]= [0, 0, 0, 1, 1]
Output : 0
Explanation: Array is already sorted.
Taking example of arr[] = [0, 0, 1, 0, 1, 0, 1, 1]:
- Position 7: Element is 1, zero count = 0, swaps = 0
- Position 6: Element is 1, zero count = 0, swaps = 0
- Position 5: Element is 0, zero count = 1, swaps = 0
- Position 4: Element is 1, zero count = 1, swaps = 1
- Position 3: Element is 0, zero count = 2, swaps = 1
- Position 2: Element is 1, zero count = 2, swaps = 3
- Position 1: Element is 0, zero count = 3, swaps = 3
- Position 0: Element is 0, zero count = 4, swaps = 3 + 0 = 3