Related Articles

# Number of ways to split a binary number such that every part is divisible by 2

• Last Updated : 04 Aug, 2021

Given a binary string S, the task is to find the number of ways to split it into parts such that every part is divisible by 2.

Examples:

Input: S = “100”
Output:
There are two ways to split the string:
{“10”, “0”} and {“100”}
Input: S = “110”
Output:

Approach: One observation is that the string can only be split after a 0. Thus, count the number of zeros in the string. Let’s call this count c_zero. Assuming the case when the string is even, for every 0 except for the rightmost one, there are two choices i.e. either cut the string after that zero or don’t. Thus, the final answer becomes 2(c_zero – 1) for even strings.
The case, where the string can’t be split is the case when it ends at a 1. Thus, for odd strings answer will always be zero as the last split part will always be odd.

Below is the implementation of the above approach:

## C++

 `// C++ implementation of the approach``#include ``using` `namespace` `std;` `#define maxN 20``#define maxM 64` `// Function to return the required count``int` `cntSplits(string s)``{``    ``// If the splitting is not possible``    ``if` `(s[s.size() - 1] == ``'1'``)``        ``return` `0;` `    ``// To store the count of zeroes``    ``int` `c_zero = 0;` `    ``// Counting the number of zeroes``    ``for` `(``int` `i = 0; i < s.size(); i++)``        ``c_zero += (s[i] == ``'0'``);` `    ``// Return the final answer``    ``return` `(``int``)``pow``(2, c_zero - 1);``}` `// Driver code``int` `main()``{``    ``string s = ``"10010"``;` `    ``cout << cntSplits(s);` `    ``return` `0;``}`

## Java

 `// Java implementation of the approach``class` `GFG``{` `static` `int` `maxN = ``20``;``static` `int` `maxM = ``64``;` `// Function to return the required count``static` `int` `cntSplits(String s)``{``    ``// If the splitting is not possible``    ``if` `(s.charAt(s.length() - ``1``) == ``'1'``)``        ``return` `0``;` `    ``// To store the count of zeroes``    ``int` `c_zero = ``0``;` `    ``// Counting the number of zeroes``    ``for` `(``int` `i = ``0``; i < s.length(); i++)``        ``c_zero += (s.charAt(i) == ``'0'``) ? ``1` `: ``0``;` `    ``// Return the final answer``    ``return` `(``int``)Math.pow(``2``, c_zero - ``1``);``}` `// Driver code``public` `static` `void` `main(String []args)``{``    ``String s = ``"10010"``;` `    ``System.out.println(cntSplits(s));``}``}` `// This code is contributed by PrinciRaj1992`

## Python3

 `# Python3 implementation of the approach` `# Function to return the required count``def` `cntSplits(s) :` `    ``# If the splitting is not possible``    ``if` `(s[``len``(s) ``-` `1``] ``=``=` `'1'``) :``        ``return` `0``;` `    ``# To store the count of zeroes``    ``c_zero ``=` `0``;` `    ``# Counting the number of zeroes``    ``for` `i ``in` `range``(``len``(s)) :``        ``c_zero ``+``=` `(s[i] ``=``=` `'0'``);` `    ``# Return the final answer``    ``return` `int``(``pow``(``2``, c_zero ``-` `1``));` `# Driver code``if` `__name__ ``=``=` `"__main__"` `:` `    ``s ``=` `"10010"``;` `    ``print``(cntSplits(s));``    ` `# This code is contributed by AnkitRai01`

## C#

 `// C# implementation of the approach``using` `System;``                    ` `class` `GFG``{` `static` `int` `maxN = 20;``static` `int` `maxM = 64;` `// Function to return the required count``static` `int` `cntSplits(String s)``{``    ``// If the splitting is not possible``    ``if` `(s[s.Length - 1] == ``'1'``)``        ``return` `0;` `    ``// To store the count of zeroes``    ``int` `c_zero = 0;` `    ``// Counting the number of zeroes``    ``for` `(``int` `i = 0; i < s.Length; i++)``        ``c_zero += (s[i] == ``'0'``) ? 1 : 0;` `    ``// Return the final answer``    ``return` `(``int``)Math.Pow(2, c_zero - 1);``}` `// Driver code``public` `static` `void` `Main(String []args)``{``    ``String s = ``"10010"``;` `    ``Console.WriteLine(cntSplits(s));``}``}` `// This code is contributed by 29AjayKumar`

## Javascript

 ``
Output:
`4`

Time Complexity: O(N)
Auxiliary Space: O(1)

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.  To complete your preparation from learning a language to DS Algo and many more,  please refer Complete Interview Preparation Course.

In case you wish to attend live classes with experts, please refer DSA Live Classes for Working Professionals and Competitive Programming Live for Students.

My Personal Notes arrow_drop_up