Counting even decimal value substrings in a binary string
Last Updated :
16 Oct, 2022
Given a binary string of size N. Count all substring that have even decimal value considering binary to decimal conversion from left to right (For example a substring “1011” is treated as 13)
Examples :
Input : 101
Output : 2
Explanation :
Substring are : 1, 10, 101, 0, 01, 1
In decimal form : 1, 1, 3, 0, 2, 1
There are only 2 even decimal value substring.
Input : 10010
Output : 8
Simple Solution is to one-by-one generate all sub-string and compute their decimal values. At least, return count of even decimal value substring.
Below is the implementation of above idea.
C++
#include <bits/stdc++.h>
using namespace std;
int evenDecimalValue(string str, int n)
{
int result = 0;
for ( int i = 0; i < n; i++) {
for ( int j = i; j < n; j++) {
int decimalValue = 0;
int powerOf2 = 1;
for ( int k = i; k <= j; k++) {
decimalValue += ((str[k] - '0' ) * powerOf2);
powerOf2 *= 2;
}
if (decimalValue % 2 == 0)
result++;
}
}
return result;
}
int main()
{
string str = "10010" ;
int n = 5;
cout << evenDecimalValue(str, n) << endl;
return 0;
}
|
Java
import java.io.*;
class GFG
{
static int evenDecimalValue(String str, int n)
{
int result = 0 ;
for ( int i = 0 ; i < n; i++)
{
for ( int j = i; j < n; j++)
{
int decimalValue = 0 ;
int powerOf2 = 1 ;
for ( int k = i; k <= j; k++)
{
decimalValue += ((str.charAt(k) -
'0' ) * powerOf2);
powerOf2 *= 2 ;
}
if (decimalValue % 2 == 0 )
result++;
}
}
return result;
}
public static void main (String[] args)
{
String str = "10010" ;
int n = 5 ;
System.out.println(evenDecimalValue(str, n));
}
}
|
Python3
import math
def evenDecimalValue( str , n) :
result = 0
for i in range ( 0 , n) :
for j in range (i, n):
decimalValue = 0 ;
powerOf2 = 1 ;
for k in range (i, j + 1 ) :
decimalValue + = (( int ( str [k]) - 0 ) * powerOf2)
powerOf2 * = 2
if (decimalValue % 2 = = 0 ):
result + = 1
return result
str = "10010"
n = 5
print (evenDecimalValue( str , n))
|
C#
using System;
class GFG
{
static int evenDecimalValue( string str, int n)
{
int result = 0;
for ( int i = 0; i < n; i++)
{
for ( int j = i; j < n; j++)
{
int decimalValue = 0;
int powerOf2 = 1;
for ( int k = i; k <= j; k++)
{
decimalValue += ((str[k] -
'0' ) * powerOf2);
powerOf2 *= 2;
}
if (decimalValue % 2 == 0)
result++;
}
}
return result;
}
public static void Main ()
{
String str = "10010" ;
int n = 5;
Console.WriteLine(evenDecimalValue(str, n));
}
}
|
PHP
<?php
function evenDecimalValue( $str , $n )
{
$result = 0;
for ( $i = 0; $i < $n ; $i ++)
{
for ( $j = $i ; $j < $n ; $j ++)
{
$decimalValue = 0;
$powerOf2 = 1;
for ( $k = $i ; $k <= $j ; $k ++)
{
$decimalValue += (( $str [ $k ] - '0' ) *
$powerOf2 );
$powerOf2 *= 2;
}
if ( $decimalValue % 2 == 0)
$result ++;
}
}
return $result ;
}
$str = "10010" ;
$n = 5;
echo evenDecimalValue( $str , $n );
?>
|
Javascript
<script>
function evenDecimalValue(str, n)
{
var result = 0;
for ( var i = 0; i < n; i++) {
for ( var j = i; j < n; j++) {
var decimalValue = 0;
var powerOf2 = 1;
for ( var k = i; k <= j; k++) {
decimalValue += ((str[k] - '0' ) * powerOf2);
powerOf2 *= 2;
}
if (decimalValue % 2 == 0)
result++;
}
}
return result;
}
var str = "10010" ;
var n = 5;
document.write( evenDecimalValue(str, n) );
</script>
|
Time Complexity: O(n3).
Auxiliary Space: O(1)
Efficient solution is based on the fact that substring whose starting value is ‘0’ always produce even decimal value. so we simply traverse a loop from left to right and count all substring whose starting value is zero.
Below is the implementation of above idea.
C++
#include <bits/stdc++.h>
using namespace std;
int evenDecimalValue(string str, int n)
{
int result = 0;
for ( int i = 0; i < n; i++) {
if (str[i] == '0' ) {
result += (n - i);
}
}
return result;
}
int main()
{
string str = "10010" ;
int n = 5;
cout << evenDecimalValue(str, n) << endl;
return 0;
}
|
Java
import java.io.*;
class GFG
{
static int evenDecimalValue(String str, int n)
{
int result = 0 ;
for ( int i = 0 ; i < n; i++)
{
if (str.charAt(i) == '0' )
{
result += (n - i);
}
}
return result;
}
public static void main(String[] args)
{
String str = "10010" ;
int n = 5 ;
System.out.println(evenDecimalValue(str, n));
}
}
|
Python3
def evenDecimalValue( str , n) :
result = 0
for i in range ( 0 , n):
if ( str [i] = = '0' ):
result + = (n - i)
return result
str = "10010"
n = 5
print (evenDecimalValue( str , n))
|
C#
using System;
class GFG
{
static int evenDecimalValue( string str, int n)
{
int result = 0;
for ( int i = 0; i < n; i++)
{
if (str[i] == '0' )
{
result += (n - i);
}
}
return result;
}
public static void Main()
{
string str = "10010" ;
int n = 5;
Console.WriteLine(evenDecimalValue(str, n));
}
}
|
PHP
<?php
function evenDecimalValue( $str , $n )
{
$result = 0;
for ( $i = 0; $i < $n ; $i ++)
{
if ( $str [ $i ] == '0' )
{
$result += ( $n - $i );
}
}
return $result ;
}
$str = "10010" ;
$n = 5;
echo evenDecimalValue( $str , $n ) ;
return 0;
?>
|
Javascript
<script>
function evenDecimalValue(str, n)
{
let result = 0;
for (let i = 0; i < n; i++)
{
if (str[i] == '0' )
{
result += (n - i);
}
}
return result;
}
let str = "10010" ;
let n = 5;
document.write(evenDecimalValue(str, n));
</script>
|
Time Complexity: O(n)
Space complexity: O(1)
Share your thoughts in the comments
Please Login to comment...