Number of sub-strings in a given binary string divisible by 2 Last Updated : 25 Aug, 2021 Comments Improve Suggest changes Like Article Like Report Given binary string str of length N, the task is to find the count of substrings of str which are divisible by 2. Leading zeros in a substring are allowed. Examples: Input: str = "101" Output: 2 "0" and "10" are the only substrings which are divisible by 2. Input: str = "10010" Output: 10 Naive approach: A naive approach will be to generate all possible substrings and check if they are divisible by 2. The time complexity for this will be O(N3). Efficient approach: It can be observed that any binary number is divisible by 2 only if it ends with a 0. Now, the task is to just count the number of substrings ending with 0. So, for every index i such that str[i] = '0', find the number of substrings ending at i. This value is equal to (i + 1) (0-based indexing). Thus, the final answer will be equal to the summation of (i + 1) for all i such that str[i] = '0'. Below is the implementation of the above approach: C++ // C++ implementation of the approach #include <bits/stdc++.h> using namespace std; // Function to return the count // of the required substrings int countSubStr(string str, int len) { // To store the final answer int ans = 0; // Loop to find the answer for (int i = 0; i < len; i++) { // Condition to update the answer if (str[i] == '0') ans += (i + 1); } return ans; } // Driver code int main() { string str = "10010"; int len = str.length(); cout << countSubStr(str, len); return 0; } Java // Java implementation of the approach class GFG { // Function to return the count // of the required substrings static int countSubStr(String str, int len) { // To store the final answer int ans = 0; // Loop to find the answer for (int i = 0; i < len; i++) { // Condition to update the answer if (str.charAt(i) == '0') ans += (i + 1); } return ans; } // Driver code public static void main (String[] args) { String str = "10010"; int len = str.length(); System.out.println(countSubStr(str, len)); } } // This code is contributed by AnkitRai01 Python3 # Python3 implementation of the approach # Function to return the count # of the required substrings def countSubStr(strr, lenn): # To store the final answer ans = 0 # Loop to find the answer for i in range(lenn): # Condition to update the answer if (strr[i] == '0'): ans += (i + 1) return ans # Driver code strr = "10010" lenn = len(strr) print(countSubStr(strr, lenn)) # This code is contributed by Mohit Kumar C# // C# implementation of the approach using System; class GFG { // Function to return the count // of the required substrings static int countSubStr(string str, int len) { // To store the final answer int ans = 0; // Loop to find the answer for (int i = 0; i < len; i++) { // Condition to update the answer if (str[i] == '0') ans += (i + 1); } return ans; } // Driver code public static void Main () { string str = "10010"; int len = str.Length; Console.WriteLine(countSubStr(str, len)); } } // This code is contributed by AnkitRai01 JavaScript <script> // Javascript implementation of the approach // Function to return the count // of the required substrings function countSubStr(str, len) { // To store the final answer var ans = 0; // Loop to find the answer for (var i = 0; i < len; i++) { // Condition to update the answer if (str[i] == '0') ans += (i + 1); } return ans; } // Driver code var str = "10010"; var len = str.length; document.write( countSubStr(str, len)); </script> Output: 10 Time Complexity: O(N), N = String length Auxiliary Space: O(1) Comment More infoAdvertise with us Next Article Number of sub-strings in a given binary string divisible by 2 D DivyanshuShekhar1 Follow Improve Article Tags : Strings Algorithms Mathematical DSA binary-string substring +2 More Practice Tags : AlgorithmsMathematicalStrings Similar Reads Number of subsequences in a given binary string divisible by 2 Given binary string str of length N, the task is to find the count of subsequences of str which are divisible by 2. Leading zeros in a sub-sequence are allowed. Examples: Input: str = "101" Output: 2 "0" and "10" are the only subsequences which are divisible by 2.Input: str = "10010" Output: 22 Naiv 4 min read Largest sub-string of a binary string divisible by 2 Given binary string str of length N, the task is to find the longest sub-string divisible by 2. If no such sub-string exists then print -1. Examples: Input: str = "11100011" Output: 111000 Largest sub-string divisible by 2 is "111000".Input: str = "1111" Output: -1 There is no sub-string of the give 3 min read Number of substrings divisible by 4 in a string of integers Given a string consisting of integers 0 to 9. The task is to count the number of substrings which when converted into integer are divisible by 4. Substring may contain leading zeroes. Examples: Input : "124" Output : 4 Substrings divisible by 4 are "12", "4", "24", "124" . Input : "04" Output : 3 Su 10 min read Number of substrings divisible by 6 in a string of integers Given a string consisting of integers 0 to 9. The task is to count the number of substrings which when convert into integer are divisible by 6. Substring does not contain leading zeroes. Examples: Input : s = "606". Output : 5 Substrings "6", "0", "6", "60", "606" are divisible by 6. Input : s = "48 9 min read Number of sub-sequences of non-zero length of a binary string divisible by 3 Given a binary string S of length N, the task is to find the number of sub-sequences of non-zero length which are divisible by 3. Leading zeros in the sub-sequences are allowed.Examples: Input: S = "1001" Output: 5 "11", "1001", "0", "0" and "00" are the only subsequences divisible by 3.Input: S = " 5 min read Number of subsequences in a string divisible by n Given a string consisting of digits 0-9, count the number of subsequences in it divisible by m.Examples: Input : str = "1234", n = 4Output : 4The subsequences 4, 12, 24 and 124 are divisible by 4. Input : str = "330", n = 6Output : 4The subsequences 30, 30, 330 and 0 are divisible by n.Input : str = 11 min read Longest sub-sequence of a binary string divisible by 3 Given a binary string S of length N, the task is to find the length of the longest sub-sequence in it which is divisible by 3. Leading zeros in the sub-sequences are allowed.Examples:  Input: S = "1001" Output: 4 The longest sub-sequence divisible by 3 is "1001". 1001 = 9 which is divisible by 3.In 9 min read Maximum splits in binary string such that each substring is divisible by given odd number Given binary string str, the task is to calculate the maximum possible splits possible to make each substring divisible by a given odd number K.Examples: Input: str = "110111001", K = 9 Output: 2 Explanation: The two possible substrings are "11011" and "1001". The equivalent decimal values are 27 an 5 min read Find the minimum number of distinct Substrings formed by 0s in Binary String Given two integers N and M, the task is to output the minimum number of different substrings that can be formed using the Binary String of length N having M number of set bits. Note: Two substrings let say S[x, y] and S[a, b] are different. If either x != a or y != b. Examples: Input: N = 3, M = 1Ou 6 min read Like