Dada una string binaria str de longitud N y dos enteros A y B tales que 0 ≤ A < B < n . La tarea es contar el número mínimo de operaciones en la string tal que dé 10 A como resto cuando se divide por 10 B . Una operación significa cambiar 1 a 0 o 0 a 1 .
Ejemplos:
Entrada: str = “1001011001”, A = 3, B = 6
Salida: 2
La string después de 2 operaciones es 1001001000.
1001001000 % 10 6 = 10 3Entrada: str = “11010100101”, A = 1, B = 5
Salida: 3
Enfoque: para que el número dé 10 A como resto cuando se divide por 10 B , los últimos dígitos B de la string deben ser 0 , excepto el dígito en (A + 1) enésima posición desde el último, que debe ser 1 . Por lo tanto, verifique los últimos dígitos B de la string para la condición anterior y aumente el conteo en 1 por cada discrepancia de dígitos.
A continuación se muestra la implementación del enfoque anterior:
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; }
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
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
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
Javascript
<script> // Javascript 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 function findCount(s, n, a, b) { // Initialize result var res = 0; // Loop through last b digits for(var 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 var str = "1001011001"; var N = str.length; var A = 3, B = 6; document.write(findCount(str, N, A, B)); // This code is contributed by itsok </script>
2
Complejidad de tiempo: O(N)
Espacio Auxiliar: O(1)
Publicación traducida automáticamente
Artículo escrito por rupesh_rao y traducido por Barcelona Geeks. The original can be accessed here. Licence: CCBY-SA