The Wayback Machine - https://web.archive.org/web/20210720114150/https://www.geeksforgeeks.org/find-binary-string-by-converting-all-01-or-10-to-11-after-m-iterations/
Skip to content
Related Articles

Related Articles

Improve Article

Find Binary string by converting all 01 or 10 to 11 after M iterations

  • Last Updated : 20 Jul, 2021

Given a binary string str[] of size N and an integer M. This binary string can be modified by flipping all the 0’s to 1 which have exactly one 1 as a neighbour. The task is to find the final state of the binary string after M such iterations.
Note: 2≤N≤103, 1≤M≤109

Examples:

Input: str=”01100″, M=1
Output: 11110
Explanation: After First Iteration: 11110

Input: str = “0110100”, M=3
Output: 1110111
Explanation: After First Iteration: 1110110, After Second Iteration: 1110111, After Third Iteration:  Remains the same.

Approach: The solution is based on the observation that the modification can go on for no more than N iterations, because even if in each iteration, atleast one 0 is flipped, then it would go on for maximum N times and, if no zero is flipped in an iteration, then this would mean that the binary string remain in the same state as on the previous step and the simulation is over. Hence, the total number of iterations will be a minimum of N and M. Follow the steps below to solve the problem:

Below is the implementation of the above approach.

C++




// C++ program for the above approach.
#include <bits/stdc++.h>
using namespace std;
  
// Function to find the modified
// binary string after M iterations
void findString(string str, int M)
{
    int N = str.length();
  
    // Set the value of M to the minimum
    // of N or M.
    M = min(M, N);
  
    // Declaration of current string state
    string s1 = "";
  
    // Loop over M iterations
    while (M != 0) {
  
        // Set the current state as null
        // before each iteration
        s1 = "";
  
        for (int i = 0; i < N; i++) {
  
            if (str[i] == '0') {
  
                // Check if this zero has exactly
                // one 1 as neighbour
                if ((str[i - 1] == '1'
                     && str[i + 1] != '1')
                    || (str[i - 1] != '1'
                        && str[i + 1] == '1'))
  
                    // Flip the zero
                    s1 += '1';
  
                else
                    s1 += '0';
            }
            else
                s1 += '1';
        }
  
        // If there is no change,
        // then no need for
        // further iterations.
        if (str == s1)
            break;
  
        // Set the current state
        // as the new previous state
        str = s1;
        M--;
    }
  
    cout << s1;
}
  
// Driver Code
int main()
{
    // Given String
    string str = "0110100";
  
    // Number of Iterations
    int M = 3;
  
    // Function Call
    findString(str, M);
  
    return 0;
}
Output
1110111

Time Complexity: O(min(M, N)*N)
Auxiliary Space: O(1)

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
Recommended Articles
Page :