Dado un número N. La tarea es contar el número mínimo de dígitos que se eliminarán del número para que no haya dos dígitos consecutivos iguales.
Ejemplos :
Entrada : N = 11344
Salida : 2
Explicación : elimine el dígito 1 del segundo lugar y el 4 del final para que el número se convierta en 134. Por lo tanto, no hay dos dígitos consecutivos iguales. Por lo tanto, la respuesta es 2.
Entrada : N = 55553
Salida : 3
Explicación : elimine el dígito 5 del segundo, tercer y cuarto lugar para que el número se convierta en 53. Por lo tanto, no hay dos dígitos consecutivos iguales. Por lo tanto la respuesta es 3.
El problema se puede resolver fácilmente si solo contamos el número de pares consecutivos de dígitos iguales. Ese sería el número mínimo de dígitos a eliminar del número dado para que no haya dos dígitos consecutivos iguales.
A continuación se muestra la implementación del enfoque anterior:
C++
// CPP program to count the minimum number // of digits to be removed from a number so that // no two consecutive digits are same #include <bits/stdc++.h> using namespace std; // Function to count the minimum number of digits // to remove from a number so that no two // consecutive digits are same. int countConsecutive(int n) { // convert the number to string string s = to_string(n); // initialize counting variable int count = 0; for (int i = 0; i < s.size() - 1; i++) if (s[i] == s[i + 1]) // check if two consecutive digits are same count++; return count; } // Driver code int main() { int n = 44522255; cout << countConsecutive(n); return 0; }
Java
// Java program to count the minimum // number of digits to be removed // from a number so that no two // consecutive digits are same import java.lang.*; import java.util.*; class GFG { // Function to count the minimum number // of digits to remove from a number so // that no two consecutive digits are same. static int countConsecutive(int n) { // convert the number to string String s = Integer.toString(n); // initialize counting variable int count = 0; for (int i = 0; i < s.length() - 1; i++) // check if two consecutive // digits are same if (s.charAt(i) == s.charAt(i + 1)) count++; return count; } // Driver code public static void main(String args[]) { int n = 44522255; System.out.println(countConsecutive(n)); } } // This code is contributed // by Akanksha Rai(Abby_akku)
Python 3
# Python 3 program to count the # minimum number of digits to be # removed from a number so that # no two consecutive digits are same # Function to count the minimum # number of digits to remove from # a number so that no two consecutive # digits are same. def countConsecutive(n): # convert the number to string s = str(n) # initialize counting variable count = 0 for i in range(len(s) - 1): # check if two consecutive # digits are same if (s[i] == s[i + 1]): count += 1 return count # Driver code if __name__ == "__main__": n = 44522255 print( countConsecutive(n)) # This code is contributed # by ChitraNayal
C#
// C# program to count the minimum // number of digits to be removed // from a number so that no two // consecutive digits are same using System; class GFG { // Function to count the minimum number // of digits to remove from a number so // that no two consecutive digits are same. static int countConsecutive(int n) { // convert the number to string string s = n.ToString(); // initialize counting variable int count = 0; for (int i = 0; i < s.Length - 1; i++) // check if two consecutive // digits are same if (s[i] == s[i + 1]) count++; return count; } // Driver code public static void Main() { int n = 44522255; Console.Write(countConsecutive(n)); } } // This code is contributed // by Akanksha Rai(Abby_akku)
PHP
<?php // PHP program to count the minimum // number of digits to be removed // from a number so that no two // consecutive digits are same // Function to count the minimum number // of digits to remove from a number so // that no two consecutive digits are same. function countConsecutive($n) { // convert the number to string $s = (string)($n); $len = strlen($s); // initialize counting variable $count = 0; for ($i = 0; $i < $len - 1; $i++) // check if two consecutive // digits are same if ($s[$i] == $s[$i + 1]) $count++; return $count; } // Driver code $n = 44522255; echo countConsecutive($n); // This code is contributed // by Akanksha Rai(Abby_akku) ?>
Javascript
<script> // Javascript program to count the minimum number // of digits to be removed from a number so that // no two consecutive digits are same // Function to count the minimum number of digits // to remove from a number so that no two // consecutive digits are same. function countConsecutive(n) { // convert the number to string var s = n.toString(); // initialize counting variable var count = 0; for (var i = 0; i < s.length - 1; i++) // check if two consecutive digits are same if (s[i] == s[i + 1]) count++; return count; } // Driver code var n = 44522255; document.write( countConsecutive(n)); </script>
4
Complejidad temporal: O(n)
Espacio auxiliar: O(logn)
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