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 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*

## Java

`// Java implementation of the approach ` `import` `java.util.*; ` ` ` `class` `GFG ` `{ ` ` ` ` ` `// Function to return the minimum number ` ` ` `// of operations on a binary string such that ` ` ` `// it gives 10^A as remainder when divided by 10^B ` ` ` `static` `int` `findCount(String s, ` `int` `n, ` `int` `a, ` `int` `b) ` ` ` `{ ` ` ` `// Initialize result ` ` ` `int` `res = ` `0` `; ` ` ` `char` `[]s1 = s.toCharArray(); ` ` ` ` ` `// Loop through last b digits ` ` ` `for` `(` `int` `i = ` `0` `; i < b; i++) ` ` ` `{ ` ` ` ` ` `if` `(i == a) ` ` ` `{ ` ` ` `if` `(s1[n - i - ` `1` `] != ` `'1'` `) ` ` ` `res += ` `1` `; ` ` ` `} ` ` ` `else` ` ` `{ ` ` ` `if` `(s1[n - i - ` `1` `] != ` `'0'` `) ` ` ` `res += ` `1` `; ` ` ` `} ` ` ` ` ` `} ` ` ` ` ` `return` `res; ` ` ` `} ` ` ` ` ` `// Driver code ` ` ` `static` `public` `void` `main (String []args) ` ` ` `{ ` ` ` ` ` `String str = ` `"1001011001"` `; ` ` ` `int` `N = str.length() ; ` ` ` `int` `A = ` `3` `, B = ` `6` `; ` ` ` ` ` `System.out.println(findCount(str, N, A, B)); ` ` ` ` ` `} ` `} ` ` ` `// This code is contributed by ChitraNayal ` |

*chevron_right*

*filter_none*

## Python3

`# Python 3 implementation of the approach ` ` ` `# Function to return 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*

## C#

`// C# implementation of the approach ` `using` `System; ` ` ` `class` `GFG ` `{ ` ` ` ` ` `// Function to return the minimum number ` ` ` `// of operations on a binary string such that ` ` ` `// it gives 10^A as remainder when divided by 10^B ` ` ` `static` `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) ` ` ` `{ ` ` ` `if` `(s[n - i - 1] != ` `'1'` `) ` ` ` `res += 1; ` ` ` `} ` ` ` `else` ` ` `{ ` ` ` `if` `(s[n - i - 1] != ` `'0'` `) ` ` ` `res += 1 ; ` ` ` `} ` ` ` ` ` `} ` ` ` ` ` `return` `res; ` ` ` `} ` ` ` ` ` `// Driver code ` ` ` `static` `public` `void` `Main () ` ` ` `{ ` ` ` ` ` `string` `str = ` `"1001011001"` `; ` ` ` `int` `N = str.Length ; ` ` ` `int` `A = 3, B = 6; ` ` ` ` ` `Console.WriteLine(findCount(str, N, A, B)); ` ` ` ` ` `} ` `} ` ` ` `// This code is contributed by AnkitRai01 ` |

*chevron_right*

*filter_none*

**Output:**

2

## Recommended Posts:

- Count subarrays such that remainder after dividing sum of elements by K gives count of elements
- Largest number by which given 3 numbers should be divided such that they leaves same remainder
- Count of integers obtained by replacing ? in the given string that give remainder 5 when divided by 13
- Program to find remainder when large number is divided by 11
- Program to find remainder when large number is divided by r
- Find the remainder when First digit of a number is divided by its Last digit
- Largest number less than or equal to Z that leaves a remainder X when divided by Y
- Generate Array whose sum of all K-size subarrays divided by N leaves remainder X
- Maximum possible remainder when an element is divided by other element in the array
- Find the remainder when N is divided by 4 using Bitwise AND operator
- Check if the remainder of N-1 factorial when divided by N is N-1 or not
- Smallest subarray whose product leaves remainder K when divided by size of the array
- Minimum integer such that it leaves a remainder 1 on dividing with any element from the range [2, N]
- String which when repeated exactly K times gives a permutation of S
- Minimum Cuts can be made in the Chessboard such that it is not divided into 2 parts
- Check if there exists a prime number which gives Y after being repeatedly subtracted from X
- Find minimum number to be divided to make a number a perfect square
- Number of K's such that the given array can be divided into two sets satisfying the given conditions
- All pairs whose xor gives unique prime
- Smallest subarray which upon repetition gives the original array

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.