Count Zeroes Surrounded by the Same Digit

The program must accept an integer N as the input. The task is to count the number of zeroes in the number which are surrounded by the same digit on both sides and print the count. A zero at the start or end of the number is not surrounded by two sides, hence they are not considered.

Boundary Condition(s):
0 <= N <= 10^9

Input Format:
The first line contains the integer N.

Output Format:
The first line contains the count of zeroes surrounded by the same digit.

Example Input/Output 1:
Input:
10020203

Output:
1

Explanation:
The only zero surrounded by the same digit is the first zero (between the two 1’s).

Example Input/Output 2:
Input:
20205

Output:
0

Explanation:
There are no zeroes surrounded by the same digit on both sides.

n = input().strip()
count = sum(1 for i in range(1, len(n) - 1) if n[i] == '0' and n[i-1] == n[i+1])
print(count)
#include<stdio.h>
#include<string.h>

int main() {
    char n[12];
    scanf("%s", n);
    int len = strlen(n), count = 0;
    for(int i = 1; i < len-1; i++) {
        if(n[i] == '0' && n[i-1] == n[i+1]) count++;
    }
    printf("%d", count);
    return 0;
}
#include <iostream>
#include <string>
using namespace std;

int main() {
    string n;
    cin >> n;
    int count = 0;
    for(int i = 1; i < n.size()-1; i++) {
        if(n[i] == '0' && n[i-1] == n[i+1]) count++;
    }
    cout << count;
    return 0;
}
import java.util.Scanner;

public class ZeroesSurrounded {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        String n = sc.next();
        int count = 0;
        for(int i = 1; i < n.length() - 1; i++) {
            if(n.charAt(i) == '0' && n.charAt(i-1) == n.charAt(i+1)) count++;
        }
        System.out.println(count);
    }
}

Leave a Reply

Your email address will not be published. Required fields are marked *

More posts. You may also be interested in.