Minimum number of digits to be removed so that no two consecutive digits are same Last Updated : 07 Jul, 2022 Comments Improve Suggest changes Like Article Like Report Given a number N. The task is to count the minimum number of digits to be removed from the number so that no two consecutive digits are the same.Examples: Input : N = 11344 Output : 2 Explanation : Remove the digit 1 from 2nd place and 4 from the end so that the number becomes 134. Thus no two consecutive digits are same. Hence answer is 2.Input : N = 55553 Output : 3 Explanation : Remove the digit 5 from the 2nd, 3rd and 4th places so that the number becomes 53. Thus no two consecutive digits are same. Hence answer is 3. The problem can be easily solved if we just count the number of consecutive pairs of equal digits. That would be the minimum number of digits to remove from the given number so that no two consecutive digits are the same.Below is the implementation of the above approach: 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> Output: 4 Time Complexity: O(n) Auxiliary Space: O(logn) Comment More infoAdvertise with us Next Article Minimum number of digits to be removed so that no two consecutive digits are same rupesh_rao Follow Improve Article Tags : Competitive Programming C++ Programs DSA number-digits Constructive Algorithms +1 More Similar Reads Minimum number of alternate subsequences required to be removed to empty a Binary String Given a binary string S consisting of N characters, the task is to print the minimum number of operations required to remove all the characters from the given string S by removing a single character or removing any subsequence of alternate characters in each operation. Examples: Input: S = "010101"O 6 min read Reduce number to a single digit by subtracting adjacent digits repeatedly Given a number N, the task is to reduce it to a single-digit number by repeatedly subtracting the adjacent digits. That is, in the first iteration, subtract all the adjacent digits to generate a new number, if this number contains more than one digit, repeat the same process until it becomes a singl 7 min read Minimum N-Digit number required to obtain largest N-digit number after performing given operations Given a positive integer N, the task is to find the minimum N-digit number such that performing the following operations on it in the following order results into the largest N-digit number: Convert the number to its Binary Coded Decimal form.Concatenate all the resulting nibbles to form a binary nu 5 min read Program to delete Nth digit of a Number Given a number num and a number n, the task is to delete this nth digit of the number num, from starting and from end.Examples: Input: num = 1234, n = 3 Output: num_after_deleting_from_starting = 124, num_after_deleting_from_end = 134Input: num = 4516312, n = 2 Output: num_after_deleting_from_starti 15+ min read Longest subarray such that adjacent elements have at least one common digit | Set 1 Given an array of N integers, write a program that prints the length of the longest subarray such that adjacent elements of the subarray have at least one digit in common. Examples: Input : 12 23 45 43 36 97 Output : 3 Explanation: The subarray is 45 43 36 which has 4 common in 45, 43 and 3 common i 11 min read Next Number with distinct digits Given an integer N, the task is to find the next number with distinct digits in it. Examples: Input: N = 20 Output: 21 The next integer with all distinct digits after 20 is 21. Input: N = 2019 Output: 2031 Approach: Count the total number of digits in the number N using the approach discussed in thi 11 min read Minimum number of deletions so that no two consecutive are same Given a string, find minimum number of deletions required so that there will be no two consecutive repeating characters in the string. Examples: Input : AAABBB Output : 4 Explanation : New string should be AB Input : ABABABAB Output : 0 Explanation : There are no consecutive repeating characters. If 4 min read Minimum number of elements to be removed so that pairwise consecutive elements are same Given a string str. The task is to count the minimum number of elements to be removed so that pairwise consecutive elements are same Examples: Input : str = "11344" Output: 1 Remove the digit 3 from 3rd place so that the string becomes 1144. Thus pairwise two consecutive elements are same. Hence ans 4 min read Minimum number to be added to N so that it does not contains digit D Given a positive integer N and a digit D, the task is to find the smallest non-negative number required to be added to the given number such that after addition, the resultant number should not contain the digit D. Example: Input: N = 25, D = 2Output: 5Explanation: The number 30 is the smallest numb 9 min read Minimum number of digits required to be removed to make a number divisible by 4 Given a number N, the task is to count the minimum number of digits to be removed from N to make it divisible by 4. Examples: Input: N = 12367Output: 1Explanation: Removing 7 from the number 1236 make the number divisible by 4. Therefore, the minimum count of digit to be removed is 1. Input: N = 243 9 min read Like