Home » Interview Questions » String Interview Questions » Even sub-string count

Even sub-string count


In the given input string of digits, find the count of substrings which when convert into integer form even.


Input string : 1234

Output : 6
2, 4, 12, 34, 234, 1234 are the substrings which are even.

Time complexity : O(n)


1. Find all even digits even digits in the input string.

2.    Count the number of sub-strings ending with even digits.

3.    Return final count.

C++ Program

#include <bits/stdc++.h>

using namespace std;
int EvenSubStrCount(char input_string[])
    int length = strlen(input_string);
    int count = 0;
    //Traverse input string
    //If current is even
    //count the number of sub-strings wnding with it
    for (int i = 0; i < length; i++)
        int current = input_string[i] - '0';
        if(current % 2 == 0)
            count = count + (i + 1);
    //return final count
    return count;
//Main function
int main()
    char input_string[] = "1234567";
    cout<<"Even sub-string count is: "<<EvenSubStrCount(input_string)<<endl;
    return 0;

Try It


How useful was this post?

Click on a star to rate it!

Average rating / 5. Vote Count

No votes so far! Be the first to rate this post.

As you found this post useful...

Follow us on social media!

We are sorry that this post was not useful for you!

Let us improve this post!

Tell us how we can improve this post?

READ  Integer to Roman
Array Interview Questions
Graph Interview Questions
LinkedList Interview Questions
String Interview Questions
Tree Interview Questions