Developers are creating a utility algorithm to segregate outliers in an array. The prototype involves an array arr of n integers, where m elements are non-zero integers, and the remaining n - m elements are zeros.
In one operation, the algorithm can swap a zero with a non-zero integer.
Task: Determine the minimum number of operations required to:
Consider an array arr = [7, 8, 0, 0, 4, 2, 0, 1, 0] of n = 9 integers. An optimal sequence of swaps is shown. Elements to swap are in bold.
[7, 8, 0, 0, 4, 2, 0, 1, 0] [7, 8, 0, 0, 4, 2, 0, 1, 0] [0, 8, 4, 7, 2, 0, 1, 0] [0, 0, 4, 7, 8, 2, 0, 1, 0] [0, 0, 4, 7, 8, 2, 0, 1, 0] [0, 2, 4, 7, 8, 0, 0, 1, 0] [1, 2, 4, 7, 8, 0, 0, 0, 0]
Return 5, the minimum number of operations required.
Hard