# Maximum number of splits of a binary number

Given a binary string **S**, the task is to find the maximum number of parts that you can split it into such that every part is divisible by **2**. If the string can’t be split satisfying the given conditions then print **-1**.

**Examples:**

Input:S = “100”

Output:2

The splits are as follows:

“10” ans “0”.

Input:S = “110”

Output:1

**Approach:** This problem can be solved greedily, start from the left end and put a cut at an index **j** such that **j** is the smallest index for which sub-string upto **j** is divisible by **2**. Now, continue this step with the rest of the left-over string. It is also known that any binary number ending with a **0** is divisible by **2**. Thus, put a cut after each and every zero and the answer will be equal to the number of zeros in the string. The only case where the answer is not possible is when the given string is odd i.e. no matter how cuts are made on the string, the last split part will always be odd.

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 required count ` `int` `cntSplits(string s) ` `{ ` ` ` `// If the splitting is not possible ` ` ` `if` `(s[s.size() - 1] == ` `'1'` `) ` ` ` `return` `-1; ` ` ` ` ` `// To store the final ans ` ` ` `int` `ans = 0; ` ` ` ` ` `// Counting the number of zeros ` ` ` `for` `(` `int` `i = 0; i < s.size(); i++) ` ` ` `ans += (s[i] == ` `'0'` `); ` ` ` ` ` `// Return the final answer ` ` ` `return` `ans; ` `} ` ` ` `// Driver code ` `int` `main() ` `{ ` ` ` `string s = ` `"10010"` `; ` ` ` ` ` `cout << cntSplits(s); ` ` ` ` ` `return` `0; ` `} ` |

*chevron_right*

*filter_none*

## Java

`// Java implementation of the approach ` `class` `GFG ` `{ ` ` ` `// 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` `-` `1` `; ` ` ` ` ` `// To store the final ans ` ` ` `int` `ans = ` `0` `; ` ` ` ` ` `// Counting the number of zeros ` ` ` `for` `(` `int` `i = ` `0` `; i < s.length(); i++) ` ` ` `ans += (s.charAt(i) == ` `'0'` `) ? ` `1` `: ` `0` `; ` ` ` ` ` `// Return the final answer ` ` ` `return` `ans; ` `} ` ` ` `// Driver code ` `public` `static` `void` `main(String []args) ` `{ ` ` ` `String s = ` `"10010"` `; ` ` ` ` ` `System.out.println(cntSplits(s)); ` `} ` `} ` ` ` `// This code is contributed by 29AjayKumar ` |

*chevron_right*

*filter_none*

## 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` `-` `1` `; ` ` ` ` ` `# To store the count of zeroes ` ` ` `ans ` `=` `0` `; ` ` ` ` ` `# Counting the number of zeroes ` ` ` `for` `i ` `in` `range` `(` `len` `(s)) : ` ` ` `ans ` `+` `=` `(s[i] ` `=` `=` `'0'` `); ` ` ` ` ` `# Return the final answer ` ` ` `return` `ans ; ` ` ` `# Driver code ` `if` `__name__ ` `=` `=` `"__main__"` `: ` ` ` ` ` `s ` `=` `"10010"` `; ` ` ` ` ` `print` `(cntSplits(s)); ` ` ` `# This code is contributed by AnkitRai01 ` |

*chevron_right*

*filter_none*

## C#

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

*chevron_right*

*filter_none*

**Output:**

3

Don’t stop now and take your learning to the next level. Learn all the important concepts of Data Structures and Algorithms with the help of the most trusted course: **DSA Self Paced**. Become industry ready at a student-friendly price.

## Recommended Posts:

- Maximum splits in binary string such that each substring is divisible by given odd number
- Minimum splits required to convert a number into prime segments
- Remove one bit from a binary number to get maximum value
- Find the maximum possible Binary Number from given string
- Minimum splits in a binary string such that every substring is a power of 4 or 6.
- Maximum number of consecutive 1's in binary representation of all the array elements
- Maximum number of set bits count in a K-size substring of a Binary String
- Count number of trailing zeros in Binary representation of a number using Bitset
- Check if the binary representation of a number has equal number of 0s and 1s in blocks
- Number of ways to split a binary number such that every part is divisible by 2
- Find the largest number smaller than integer N with maximum number of set bits
- Largest number dividing maximum number of elements in the array
- Number of leading zeros in binary representation of a given number
- Minimum number of Binary strings to represent a Number
- Number of steps required to convert a binary number to one
- Represent a number as a sum of maximum possible number of Prime Numbers
- Binary representation of next greater number with same number of 1's and 0's
- Maximum number of prime factors a number can have with exactly x factors
- Binary representation of next number
- 1's and 2's complement of a Binary Number

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.