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

• Last Updated : 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=3
Output: 2
Explanation:
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=1
Output: 6

Approach: This problem can be solved using a greedy approach. To solve this problem, follow the below steps:

1. Create a variable cnt, to store the minimum number of moves required. Initialize it with 0.
2. 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’.
3. After the loop ends, return cnt which will be the required answer.

Below is the implementation of the above approach:

## C++

 `// C++ code for the above approach``#include ``using` `namespace` `std;` `// Function to find the minimum``// operations required to convert``// the binary string str to all 1s``int` `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 Code``int` `main()``{``    ``string str = ``"0010"``;``    ``int` `K = 3;``    ``cout << minMoves(str, K);``}`

## Java

 `// Java code for the above approach``class` `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`

## Python3

 `# Python code for the above approach` `# Function to find the minimum``# operations required to convert``# the binary string str to all 1s``def` `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 Code``str` `=` `"0010"``K ``=` `3``print``(minMoves(``str``, K))` `# This code is contributed by Saurabh Jaiswal`

## C#

 `// C# code for the above approach``using` `System;` `class` `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[i] == ``'0'``) {``            ``i += K;``            ``cnt += 1;``        ``}` `        ``// If element is '1'``        ``else` `{``            ``i++;``        ``}``    ``}` `    ``return` `cnt;``}` `// Driver Code``public` `static` `void` `Main()``{``    ``string` `str = ``"0010"``;``    ``int` `K = 3;``    ``Console.Write(minMoves(str, K));``}``}` `// This code is contributed by Samim Hossain Mondal.`

## Javascript

 ``

Output
`2`

Time Complexity: O(N)
Auxiliary Space: O(1)

My Personal Notes arrow_drop_up