Minimize operations to make given binary string as all 1s by repeatedly converting K consecutive characters to 1 - GeeksforGeeks

20 Dec, 2021
Given a binary string str of N characters an integer K, the task is to find the minimum moves required to convert all characters of the string to 1’s where at each move, K consecutive characters can be converted to 1.
Example:
Input: str=”0010′′, K=3Output: 2Explanation:Move 1: Select the substring from 0 to 2 and replace it with all 1.Move 2: Select the substring from 1 to 3 and replace it with all 1.
Input: str=”0000010′′, K=1Output: 6
Approach: This problem can be solved using a greedy approach. To solve this problem, follow the below steps:
Create a variable cnt, to store the minimum number of moves required. Initialize it with 0.Traverse the string using a variable i and str[i] = ‘0’, then update i to i + K and increase cnt by 1, because no matter what these characters are, a move is always required to convert the character str[i] to ‘1’.After the loop ends, return cnt which will be the required answer.
Create a variable cnt, to store the minimum number of moves required. Initialize it with 0.
Traverse the string using a variable i and str[i] = ‘0’, then update i to i + K and increase cnt by 1, because no matter what these characters are, a move is always required to convert the character str[i] to ‘1’.
After the loop ends, return cnt which will be the required answer.
Below is the implementation of the above approach:
C++
Java
Python3
C#
Javascript
// C++ code for the above approach#include <bits/stdc++.h>using namespace std; // Function to find the minimum// operations required to convert// the binary string str to all 1sint minMoves(string str, int K){    int N = str.size();     // Variable to store number    // of minimum moves required    int cnt = 0;     int i = 0;     // Loop to traverse str    while (i < N) {         // If element is '0'        if (str[i] == '0') {            i += K;            cnt += 1;        }         // If element is '1'        else {            i++;        }    }     return cnt;} // Driver Codeint main(){    string str = "0010";    int K = 3;    cout << minMoves(str, K);}
// Java code for the above approachclass GFG {     // Function to find the minimum    // operations required to convert    // the binary String str to all 1s    static int minMoves(String str, int K) {        int N = str.length();         // Variable to store number        // of minimum moves required        int cnt = 0;         int i = 0;         // Loop to traverse str        while (i < N) {             // If element is '0'            if (str.charAt(i) == '0') {                i += K;                cnt += 1;            }             // If element is '1'            else {                i++;            }        }         return cnt;    }     // Driver Code    public static void main(String args[]) {        String str = "0010";        int K = 3;        System.out.println(minMoves(str, K));    }} // This code is contributed by Saurabh Jaiswal
# Python code for the above approach # Function to find the minimum# operations required to convert# the binary string str to all 1sdef minMoves(str, K):    N = len(str)     # Variable to store number    # of minimum moves required    cnt = 0     i = 0     # Loop to traverse str    while (i < N):         # If element is '0'        if (str[i] == '0'):            i += K            cnt += 1        # If element is '1'        else:            i += 1     return cnt # Driver Codestr = "0010"K = 3print(minMoves(str, K)) # This code is contributed by Saurabh Jaiswal
// C# code for the above approachusing System; class GFG{   // Function to find the minimum// operations required to convert// the binary string str to all 1sstatic int minMoves(string str, int K){    int N = str.Length;     // Variable to store number    // of minimum moves required    int cnt = 0;     int i = 0;     // Loop to traverse str    while (i < N) {         // If element is '0'        if (str[i] == '0') {            i += K;            cnt += 1;        }         // If element is '1'        else {            i++;        }    }     return cnt;} // Driver Codepublic static void Main(){    string str = "0010";    int K = 3;    Console.Write(minMoves(str, K));}} // This code is contributed by Samim Hossain Mondal.
<script>       // JavaScript code for the above approach         // Function to find the minimum       // operations required to convert       // the binary string str to all 1s       function minMoves(str, K) {           let N = str.length;            // Variable to store number           // of minimum moves required           let cnt = 0;            let i = 0;            // Loop to traverse str           while (i < N) {                // If element is '0'               if (str[i] == '0') {                   i += K;                   cnt += 1;               }                // If element is '1'               else {                   i++;               }           }            return cnt;       }        // Driver Code        let str = "0010";       let K = 3;       document.write(minMoves(str, K));  // This code is contributed by Potta Lokesh   </script>
  
2
 
Time Complexity: O(N)Auxiliary Space: O(1)
 
lokeshpotta20
samim2000
_saurabh_jaiswal
binary-string
Greedy
Strings
Strings
Greedy
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Optimal Page Replacement Algorithm
Program for Best Fit algorithm in Memory Management
Program for First Fit algorithm in Memory Management
Bin Packing Problem (Minimize number of used Bins)
Max Flow Problem Introduction
Write a program to reverse an array or string
Reverse a string in Java
C++ Data Types
Longest Common Subsequence | DP-4
Check for Balanced Brackets in an expression (well-formedness) using Stack