Related Articles

# Minimize cost of swapping set bits with unset bits in a given Binary string

• Last Updated : 08 Oct, 2021

Given a binary string S of size N, the task is to find the minimum cost by swapping every set bit with an unset bit such that the cost of swapping pairs of bits at indices i and j is abs(j – i).

Note: A swapped bit can’t be swapped twice and the count of set bit in the given binary string is at most N/2.

Examples:

Input: S = “1010001”
Output: 3
Explanation:
Following the swapping of characters required:

1. Swap characters at indices (0, 1) modifies the string to “0110001” and the cost of this operation is |1 – 0| = 1.
2. Swap characters at indices (2, 3) modifies the string to “0101001” and the cost of this operation is |2 – 1| = 1.
3. Swap characters at indices (6, 7) modifies the string to “0101010” and the cost of this operation is |7 – 6| = 1.

After the above operations, all the set bits is replaced with unset bits and the total cost of operations is 1 + 1 + 1 = 3.

Input: S = “1100”
Output: 4

Approach: The given problem can be solved using Dynamic Programming by storing the indices of set and unset bits in two auxiliary arrays, say A[] and B[], and then find the sum of the difference between array elements A[] with any element of the array B[]. Follow the steps below to solve the given problem:

• Initialize two arrays, say A[] and B[], and store the indices of set and unset bits in it.
• Initialize a 2D array, dp[][] of dimensions K*(N – K) where K is the count of set bit in S such thatdp[i][j] stores the minimum cost of swapping the ith array element A[] with the jth array element B[].
• Now, for each state there are two choices:
1. Swap the ith array element A[] till the (j – 1)th array element B[] as dp[i][j] = dp[i][j – 1].
2. Swap the (i – 1)th array element A[] till the (j – 1)th array element B[] and the ith array element A[] with jth array element B[] and this state can be calculated as dp[i][j] = dp[i – 1][j – 1] + abs(A[i] – B[i]).
• Now, choose the minimum of the above two choices to find the current state as:

dp[i][j] = min(dp[i][j-1], dp[i-1][j-1] + abs(A[i] – B[j]))

• After completing the above steps, print the value of dp[K][N – K] as the resultant minimum number of operations.

Below is the implementation of the above approach:

## C++

 `// C++ program for the above approach` `#include ``using` `namespace` `std;``#define INF 1000000000` `// Function to find the minimum cost``// required to swap every set bit with``// an unset bit``int` `minimumCost(string s)``{``    ``int` `N = s.length();` `    ``// Stores the indices of set and``    ``// unset bits of the string S``    ``vector<``int``> A, B;` `    ``// Traverse the string S``    ``for` `(``int` `i = 0; i < N; i++) {` `        ``// Store the indices``        ``if` `(s[i] == ``'1'``) {``            ``A.push_back(i);``        ``}``        ``else` `{``            ``B.push_back(i);``        ``}``    ``}` `    ``int` `n1 = A.size();``    ``int` `n2 = B.size();` `    ``// Initialize a dp table of size``    ``// n1*n2``    ``int` `dp[n1 + 1][n2 + 1];` `    ``// Initialize all states to 0``    ``memset``(dp, 0, ``sizeof``(dp));` `    ``// Set unreachable states to INF``    ``for` `(``int` `i = 1; i <= n1; i++) {``        ``dp[i] = INF;``    ``}` `    ``// Fill the dp Table according to``    ``// the given recurrence relation``    ``for` `(``int` `i = 1; i <= n1; i++) {``        ``for` `(``int` `j = 1; j <= n2; j++) {` `            ``// Update the value of``            ``// dp[i][j]``            ``dp[i][j] = min(``                ``dp[i][j - 1],``                ``dp[i - 1][j - 1]``                    ``+ ``abs``(A[i - 1] - B[j - 1]));``        ``}``    ``}` `    ``// Return the minimum cost``    ``return` `dp[n1][n2];``}` `// Driver Code``int` `main()``{``    ``string S = ``"1010001"``;``    ``cout << minimumCost(S);` `    ``return` `0;``}`

## Python3

 `# Python program for the above approach``INF ``=` `1000000000``;` `# Function to find the minimum cost``# required to swap every set bit with``# an unset bit``def` `minimumCost(s):``  ``N ``=` `len``(s);` `  ``# Stores the indices of set and``  ``# unset bits of the string S``  ``A ``=` `[]``  ``B ``=` `[]` `  ``# Traverse the string S``  ``for` `i ``in` `range``(``0``, N):``    ` `    ``# Store the indices``    ``if` `(s[i] ``=``=` `"1"``):``      ``A.append(i);``    ``else``:``      ``B.append(i);``    ` `  ``n1 ``=` `len``(A)``  ``n2 ``=` `len``(B)` `  ``# Initialize a dp table of size``  ``# n1*n2``  ``dp ``=` `[[``0` `for` `i ``in` `range``(n2 ``+` `1``)] ``for` `j ``in` `range``(n1 ``+` `1``)]` `  ``# Set unreachable states to INF``  ``for` `i ``in` `range``(``1``, n1 ``+` `1``):``    ``dp[i][``0``] ``=` `INF``  ` `  ``# Fill the dp Table according to``  ``# the given recurrence relation``  ``for` `i ``in` `range``(``1``, n1 ``+` `1``):``    ``for` `j ``in` `range``(``1``, n2 ``+` `1``):``      ` `      ``# Update the value of``      ``# dp[i][j]``      ``dp[i][j] ``=` `min``(``        ``dp[i][j ``-` `1``],``        ``dp[i ``-` `1``][j ``-` `1``] ``+` `abs``(A[i ``-` `1``] ``-` `B[j ``-` `1``])``      ``);``    ` `  ``# Return the minimum cost``  ``return` `dp[n1][n2];` `# Driver Code``S ``=` `"1010001"``;``print``(minimumCost(S));` `# This code is contributed by _saurabh_jaiswal.`

## Javascript

 ``
Output:
`3`

Time Complexity: O(K*(N – K)) where K is the count of set bit in S.
Auxiliary Space: O(K*(N – K))

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.  To complete your preparation from learning a language to DS Algo and many more,  please refer Complete Interview Preparation Course.

In case you wish to attend live classes with experts, please refer DSA Live Classes for Working Professionals and Competitive Programming Live for Students.

My Personal Notes arrow_drop_up