# Minimum number of operations on a binary string such that it gives 10^A as remainder when divided by 10^B

Given a binary string **str** of length **N** and two integers **A** and **B** such that **0 ≤ A < B < n**. The task is to count the minimum number of operations on the string such that it gives 10^{A} as remainder when divided by 10^{B}. An operation means changing **1** to **0** or **0** to **1**.

**Examples:**

Input:str = “1001011001”, A = 3, B = 6

Output:2

The string after 2 operations is 1001001000.

1001001000 % 10^{6}= 10^{3}

Input:str = “11010100101”, A = 1, B = 5

Output:3

**Approach:** In order for the number to give **10 ^{A}** as remainder when divided by

**10**, the last

^{B}**B**digits of the string has to be

**0**except the digit at

**(A + 1)**position from the last which should be

^{th}**1**. Therefore, check the last

**B**digits of the string for the above condition and increase the count by 1 for each mismatch of digit.

Below is the implementation of the above approach:

## C++

`// C++ implementation of the approach ` `#include <bits/stdc++.h> ` `using` `namespace` `std; ` ` ` `// Function to return the the minimum number ` `// of operations on a binary string such that ` `// it gives 10^A as remainder when divided by 10^B ` `int` `findCount(string s, ` `int` `n, ` `int` `a, ` `int` `b) ` `{ ` ` ` `// Initialize result ` ` ` `int` `res = 0; ` ` ` ` ` `// Loop through last b digits ` ` ` `for` `(` `int` `i = 0; i < b; i++) { ` ` ` `if` `(i == a) ` ` ` `res += (s[n - i - 1] != ` `'1'` `); ` ` ` `else` ` ` `res += (s[n - i - 1] != ` `'0'` `); ` ` ` `} ` ` ` ` ` `return` `res; ` `} ` ` ` `// Driver code ` `int` `main() ` `{ ` ` ` `string str = ` `"1001011001"` `; ` ` ` `int` `N = str.size(); ` ` ` `int` `A = 3, B = 6; ` ` ` ` ` `cout << findCount(str, N, A, B); ` ` ` ` ` `return` `0; ` `} ` |

*chevron_right*

*filter_none*

## Python3

`# Python 3 implementation of the approach ` ` ` `# Function to return the the minimum number ` `# of operations on a binary string such that ` `# it gives 10^A as remainder when divided by 10^B ` `def` `findCount(s, n, a, b): ` ` ` `# Initialize result ` ` ` `res ` `=` `0` ` ` ` ` `# Loop through last b digits ` ` ` `for` `i ` `in` `range` `(b): ` ` ` `if` `(i ` `=` `=` `a): ` ` ` `res ` `+` `=` `(s[n ` `-` `i ` `-` `1` `] !` `=` `'1'` `) ` ` ` `else` `: ` ` ` `res ` `+` `=` `(s[n ` `-` `i ` `-` `1` `] !` `=` `'0'` `) ` ` ` ` ` `return` `res ` ` ` `# Driver code ` `if` `__name__ ` `=` `=` `'__main__'` `: ` ` ` `str` `=` `"1001011001"` ` ` `N ` `=` `len` `(` `str` `) ` ` ` `A ` `=` `3` ` ` `B ` `=` `6` ` ` ` ` `print` `(findCount(` `str` `, N, A, B)) ` ` ` `# This code is contributed by ` `# Surendra_Gangwar ` |

*chevron_right*

*filter_none*

**Output:**

2

## Recommended Posts:

- Minimum number of operations required to sum to binary string S
- Program to find remainder when large number is divided by r
- Program to find remainder when large number is divided by 11
- Largest number by which given 3 numbers should be divided such that they leaves same remainder
- Minimum number of given operations required to convert a string to another string
- Find minimum number to be divided to make a number a perfect square
- Minimum number of replacements to make the binary string alternating | Set 2
- Minimum number of characters to be removed to make a binary string alternate
- Minimum number of palindromic subsequences to be removed to empty a binary string
- Minimum reduce operations to covert a given string into a palindrome
- Count of operations to make a binary string"ab" free
- Minimum number of operations required to reduce N to 1
- Minimum number operations required to convert n to m | Set-2
- Minimum prime number operations to convert A to B
- Convert a number m to n using minimum number of given operations

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.