Minimum Swaps to Group All 1's Together

Easy
0/40
Average time to solve is 15m
profile
Contributed by
15 upvotes
Asked in company
Adobe

Problem statement

You are given an array/list ‘ARR’ of size ‘N’. ‘ARR' is binary i.e. it contains only 0s and 1s (ARR[i] = {0, 1}). Your task is to find out the minimum number of swaps required to group all 1s together.

Note: If ‘ARR’ contains only 0’s then print -1.

Example:

Let ‘ARR’ = [ 0, 1, 0, 1]. We can group all 1s together in the following ways: ‘ARR’ =[0, 0, 1, 1] or ‘ARR’ = [0, 1, 1, 0]. 

In this example, we need only 1 swap to group all 1’s together which is the minimum possible. 
Detailed explanation ( Input/output format, Notes, Images )
Input Format:
The first line of input contains an integer ‘T’ which denotes the number of test cases. 

The first line of each test case contains a single integer ‘N’ representing the number of elements in the array/list ‘ARR’.

The next line of each test case contains ‘N’ single space-separated integers (0s and 1s) denoting the elements of  ‘ARR’.
Output Format :
For each test case, return the minimum number of swaps required to group all 1’s together.
Note:
You don't need to print anything, it has already been taken care of. Just implement the given function.
Constraints:
1 <= ‘T’ <= 100
2 <= ‘N’ <= 5000
0 <= ‘ARR[i]’ <= 1

Where ‘ARR[i]’ represents the elements of array/list ‘ARR’. 

Time Limit: 1 sec
Sample Input 1:
2
5
1 0 1 0 1
6
1 1 1 1 1 1
Sample Output 1:
1
0

Explanation for Sample Output 1:

In test case 1, swap ‘ARR[1]’ and ‘ARR[4]’ (0-based indexing). Then ‘ARR’ = [1, 1, 1, 0, 0]. So, the minimum swaps to group all 1s together is 1.

In test case 2, all 1s are already together in 'ARR'. So, we don’t need any swaps. Hence, the minimum swaps to group all 1s together is 0.
Sample Input 2:
2
4 
0 0 0 0
6
1 1 0 0 1 1
Sample Output 2:
-1
 2

Explanation for Sample Output 2:

In test case 1, the number of 1s in 'ARR' is 0. So we return -1.

In test case 2, first, we swap ‘ARR[2]’ and ‘ARR[4]’ (0-based indexing). Now, ‘ARR’ = [1, 1, 1, 0, 0, 1].
Then, swap ‘ARR[3]’ and ‘ARR[5]’. Now, ‘ARR’ = [1, 1, 1, 1, 0, 0]. So, the minimum swaps to group all 1s together is 2.
Hint

Count the number of 1s and 0s, using brute force.

Approaches (2)
Brute-Force approach.

First, we traverse through ‘ARR’ and count the number of 1s presents. We store the count in a 'COUNTONE' variable.  If 'COUNTONE' is 0 then we simply return -1.

 

Else,  we have to find a subarray of length 'COUNTONE', which contains maximum number 1's. We then need to swap all the 0s with 1s present in that subarray of length 'COUNTONE' as it will result in minimum possible swaps.

 

The steps are as follows:

 

  1. We declare a variable ‘COUNTONE’ and ‘MAXNUMONE’ in which we store the total numbers of 1s present in ‘ARR’ and the maximum number of 1s in a subarray of size ‘COUNTONE’, respectively.
  2. If ‘COUNTONE’ is 0:
    • Return -1.
  3. We run a loop for ‘i’ = 0 to ‘N - COUNTONE’.
    • We declare a variable ‘COUNTNUMONE’.
    • We run a loop for ‘j’ = ‘i’ to ‘j’ = ‘i’ + ‘COUNTONE’.
      • If ‘ARR[j]’ == 1
        • ‘COUNTNUMONE’++
    • ‘MAXNUMONE’ = max(‘MAXNUMONE’, ‘COUNTNUMONE’)
  4. Finally, return 'COUNTONE' - ‘MAXNUMONE’.
Time Complexity

O(N ^ 2), Where ‘N’ represents the number of elements in the array/list ‘ARR’.

 

We are using two nested loops for finding the maximum number of 1’s in subarrays of size ‘COUNTONE’ in ‘ARR’. There are ‘N’ - ‘COUNTONE’ + 1 subarrays of length ‘COUNTONE’, and we are iterating every such subarray to find the one with a maximum number of 1s. In the worst case, when ‘COUNTONE’ = N/2, the time complexity is O((N - N/2) * N/2) => O(N^2). Hence, the overall time complexity is O(N ^ 2).

Space Complexity

O(1) 

 

Since we are not using any extra space for finding our resultant answer. Thus the space complexity will be O(1).

Code Solution
(100% EXP penalty)
Minimum Swaps to Group All 1's Together
Full screen
Console