Minimum Swaps to Group All 1's Together 1 - In an array

Problem A swap is defined as taking two distinct positions in an array and swapping the values in them. Given a binary array nums, return the minimum number of swaps required to group all 1’s present in the array together at any location. Examples Example 1: Input: nums = [1,0,1,0,1] Output: 1 Explanation: There are 3 ways to group all 1's together: [1,{0},1,0,{1}] => [1,1,1,0,0] using 1 swap. [1,0,1,{0},{1}] => [{1},{0},1,1,0] => [0,1,1,1,0] using 1 swaps. [{1},0,1,{0},1] => [0,0,1,1,1] using 1 swap. The minimum is 1. ...

This site uses cookies to improve your experience on our website. By using and continuing to navigate this website, you accept this. Privacy Policy