Open In App

Minimum number of digits to be removed so that no two consecutive digits are same

Improve
Improve
Like Article
Like
Save
Share
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) 



Last Updated : 07 Jul, 2022
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads