Number of ways to split a binary number such that every part is divisible by 2
Last Updated :
15 Mar, 2023
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: 2
There are two ways to split the string:
{“10”, “0”} and {“100”}
Input: S = “110”
Output: 1
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++
#include <bits/stdc++.h>
using namespace std;
#define maxN 20
#define maxM 64
int cntSplits(string s)
{
if (s[s.size() - 1] == '1' )
return 0;
int c_zero = 0;
for ( int i = 0; i < s.size(); i++)
c_zero += (s[i] == '0' );
return ( int ) pow (2, c_zero - 1);
}
int main()
{
string s = "10010" ;
cout << cntSplits(s);
return 0;
}
|
Java
class GFG
{
static int maxN = 20 ;
static int maxM = 64 ;
static int cntSplits(String s)
{
if (s.charAt(s.length() - 1 ) == '1' )
return 0 ;
int c_zero = 0 ;
for ( int i = 0 ; i < s.length(); i++)
c_zero += (s.charAt(i) == '0' ) ? 1 : 0 ;
return ( int )Math.pow( 2 , c_zero - 1 );
}
public static void main(String []args)
{
String s = "10010" ;
System.out.println(cntSplits(s));
}
}
|
Python3
def cntSplits(s) :
if (s[ len (s) - 1 ] = = '1' ) :
return 0 ;
c_zero = 0 ;
for i in range ( len (s)) :
c_zero + = (s[i] = = '0' );
return int ( pow ( 2 , c_zero - 1 ));
if __name__ = = "__main__" :
s = "10010" ;
print (cntSplits(s));
|
C#
using System;
class GFG
{
static int maxN = 20;
static int maxM = 64;
static int cntSplits(String s)
{
if (s[s.Length - 1] == '1' )
return 0;
int c_zero = 0;
for ( int i = 0; i < s.Length; i++)
c_zero += (s[i] == '0' ) ? 1 : 0;
return ( int )Math.Pow(2, c_zero - 1);
}
public static void Main(String []args)
{
String s = "10010" ;
Console.WriteLine(cntSplits(s));
}
}
|
Javascript
function cntSplits(s) {
if (s[s.length - 1] == '1' ) {
return 0;
}
let c_zero = 0;
for (let i = 0; i < s.length; i++) {
c_zero += (s[i] == '0' );
}
return Math.pow(2, c_zero - 1);
}
let s = "10010" ;
console.log(cntSplits(s));
|
Time Complexity: O(N)
Auxiliary Space: O(1)
Efficient approach :
This approach made complexity O(1) by replacing the for loop which counts the number of zeroes with a single statement that calculates the number of zeroes by using the built-in string method ‘replaceAll()’ to remove all the ones and then finding the length of the remaining string, which would be the number of zeroes. Then, instead of using the pow function to calculate 2 raised to the power of c_zero-1, we can use a bit shift operator (c_zero-1) << 1, which will also give the same result but with a constant time complexity of O(1)
C++
#include <iostream>
#include <string>
using namespace std;
const int maxN = 20;
const int maxM = 64;
int cntSplits(string s) {
if (s[s.length() - 1] == '1' )
return 0;
int c_zero = 0;
for ( int i = 0; i < s.length(); i++) {
if (s[i] == '0' ) {
c_zero++;
}
}
return (c_zero-1) << 1;
}
int main() {
string s = "10010" ;
cout << cntSplits(s) << endl;
return 0;
}
|
Java
class GFG
{
static int maxN = 20 ;
static int maxM = 64 ;
static int cntSplits(String s)
{
if (s.charAt(s.length() - 1 ) == '1' )
return 0 ;
int c_zero = s.length() - s.replaceAll( "0" , "" ).length();
return (c_zero- 1 ) << 1 ;
}
public static void main(String []args)
{
String s = "10010" ;
System.out.println(cntSplits(s));
}
}
|
Python3
class GFG:
maxN = 20
maxM = 64
@staticmethod
def cntSplits(s):
if s[ - 1 ] = = '1' :
return 0
c_zero = len (s) - len (s.replace( "0" , ""))
return (c_zero - 1 ) << 1
if __name__ = = '__main__' :
s = "10010"
print (GFG.cntSplits(s))
|
C#
using System;
class GFG {
static int maxN = 20;
static int maxM = 64;
static int cntSplits(String s)
{
if (s[s.Length - 1] == '1' )
return 0;
int c_zero = 0;
for ( int i = 0; i < s.Length; i++) {
if (s[i] == '0' ) {
c_zero++;
}
}
return (c_zero - 1) << 1;
}
public static void Main(String[] args)
{
String s = "10010" ;
Console.WriteLine(cntSplits(s));
}
}
|
Javascript
class GFG {
static maxN = 20;
static maxM = 64;
static cntSplits(s) {
if (s[s.length - 1] == "1" ) {
return 0;
}
let c_zero = (s.match(/0/g) || []).length;
return (c_zero - 1) << 1;
}
}
let s = "10010" ;
console.log(GFG.cntSplits(s));
|
Time complexity : O(1)
Share your thoughts in the comments
Please Login to comment...