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++
// C++ code to generate all possible substring
// and count even decimal value substring.
#include <bits/stdc++.h>
using namespace std;
// generate all substring in arr[0..n-1]
int evenDecimalValue(string str, int n)
{
// store the count
int result = 0;
// Pick starting point
for (int i = 0; i < n; i++) {
// Pick ending point
for (int j = i; j < n; j++) {
int decimalValue = 0;
int powerOf2 = 1;
// substring between current starting
// and ending points
for (int k = i; k <= j; k++) {
decimalValue += ((str[k] - '0') * powerOf2);
// increment power of 2 by one
powerOf2 *= 2;
}
if (decimalValue % 2 == 0)
result++;
}
}
return result;
}
// Driver program
int main()
{
string str = "10010";
int n = 5;
cout << evenDecimalValue(str, n) << endl;
return 0;
}
Java
// Java Program to count all even
// decimal value substring .
import java.io.*;
class GFG
{
// generate all substring in arr[0..n-1]
static int evenDecimalValue(String str, int n)
{
// store the count
int result = 0;
// Pick starting point
for (int i = 0; i < n; i++)
{
// Pick ending point
for (int j = i; j < n; j++)
{
int decimalValue = 0;
int powerOf2 = 1;
// substring between current
// starting and ending points
for (int k = i; k <= j; k++)
{
decimalValue += ((str.charAt(k) -
'0') * powerOf2);
// increment power of 2 by one
powerOf2 *= 2;
}
if (decimalValue % 2 == 0)
result++;
}
}
return result;
}
// Driver code
public static void main (String[] args)
{
String str = "10010";
int n = 5;
System.out.println(evenDecimalValue(str, n));
}
}
//This code is contributed by Gitanjali.
Python3
# Python3 Program to count all even
# decimal value substring
import math
# Generate all substring in arr[0..n-1]
def evenDecimalValue(str, n) :
# Store the count
result = 0
# Pick starting point
for i in range(0, n) :
# Pick ending point
for j in range(i, n):
decimalValue = 0;
powerOf2 = 1;
# Substring between current
# starting and ending points
for k in range(i, j + 1) :
decimalValue += ((int(str[k])- 0) * powerOf2)
# increment power of 2 by one
powerOf2 *= 2
if (decimalValue % 2 == 0):
result += 1
return result
# Driver code
str = "10010"
n = 5
print (evenDecimalValue(str, n))
# This code is contributed by Gitanjali.
C#
// C# Program to count all even
// decimal value substring .
using System;
class GFG
{
// generate all substring in arr[0..n-1]
static int evenDecimalValue(string str, int n)
{
// store the count
int result = 0;
// Pick starting point
for (int i = 0; i < n; i++)
{
// Pick ending point
for (int j = i; j < n; j++)
{
int decimalValue = 0;
int powerOf2 = 1;
// substring between current
// starting and ending points
for (int k = i; k <= j; k++)
{
decimalValue += ((str[k] -
'0') * powerOf2);
// increment power of 2 by one
powerOf2 *= 2;
}
if (decimalValue % 2 == 0)
result++;
}
}
return result;
}
// Driver code
public static void Main ()
{
String str = "10010";
int n = 5;
Console.WriteLine(evenDecimalValue(str, n));
}
}
// This code is contributed by vt_m.
PHP
<?php
// PHP code to generate all
// possible substring and
// count even decimal value
// substring
// generate all substring
// in arr[0..n-1]
function evenDecimalValue( $str, $n)
{
// store the count
$result = 0;
// Pick starting point
for ( $i = 0; $i < $n; $i++)
{
// Pick ending point
for ($j = $i; $j < $n; $j++)
{
$decimalValue = 0;
$powerOf2 = 1;
// substring between current
// starting and ending points
for ( $k = $i; $k <= $j; $k++)
{
$decimalValue += (($str[$k] - '0') *
$powerOf2);
// increment power of 2 by one
$powerOf2 *= 2;
}
if ($decimalValue % 2 == 0)
$result++;
}
}
return $result;
}
// Driver Code
$str = "10010";
$n = 5;
echo evenDecimalValue($str, $n);
// This code is contributed by anuj_67.
?>
JavaScript
<script>
// javascript code to generate all possible substring
// and count even decimal value substring.
// generate all substring in arr[0..n-1]
function evenDecimalValue(str, n)
{
// store the count
var result = 0;
// Pick starting point
for (var i = 0; i < n; i++) {
// Pick ending point
for (var j = i; j < n; j++) {
var decimalValue = 0;
var powerOf2 = 1;
// substring between current starting
// and ending points
for (var k = i; k <= j; k++) {
decimalValue += ((str[k] - '0') * powerOf2);
// increment power of 2 by one
powerOf2 *= 2;
}
if (decimalValue % 2 == 0)
result++;
}
}
return result;
}
// Driver program
var str = "10010";
var n = 5;
document.write( evenDecimalValue(str, n) );
// This code is contributed by rrrtnx.
</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++
// Program to count all even decimal value substring .
#include <bits/stdc++.h>
using namespace std;
// function return count of even decimal
// value substring
int evenDecimalValue(string str, int n)
{
// store the count of even decimal value substring
int result = 0;
for (int i = 0; i < n; i++) {
// substring started with '0'
if (str[i] == '0') {
// increment result by (n-i)
// because all substring which are generate by
// this character produce even decimal value.
result += (n - i);
}
}
return result;
}
// Driver program
int main()
{
string str = "10010";
int n = 5;
cout << evenDecimalValue(str, n) << endl;
return 0;
}
Java
// Java Program to count all even
// decimal value substring .
import java.io.*;
class GFG
{
// function return count of
// even decimal value substring
static int evenDecimalValue(String str, int n)
{
// store the count of even
// decimal value substring
int result = 0;
for (int i = 0; i < n; i++)
{
// substring started with '0'
if (str.charAt(i) == '0')
{
// increment result by (n-i)
// because all substring which
// are generate by this character
// produce even decimal value.
result += (n - i);
}
}
return result;
}
// Driver code
public static void main(String[] args)
{
String str = "10010";
int n = 5;
System.out.println(evenDecimalValue(str, n));
}
}
// This code is contributed
// by Gitanjali.
Python3
# Python Program to count all even
# decimal value substring
# Function return count of even
# decimal value substring
def evenDecimalValue(str, n) :
# Store the count of even
# decimal value substring
result = 0
for i in range(0, n):
# substring started with '0'
if (str[i] == '0'):
# increment result by (n-i)
# because all substring which are generate by
# this character produce even decimal value.
result += (n - i)
return result
# Driver code
str = "10010"
n = 5
print (evenDecimalValue(str, n))
# This code is contributed by Gitanjali.
C#
// C# Program to count all even
// decimal value substring .
using System;
class GFG
{
// function return count of
// even decimal value substring
static int evenDecimalValue(string str, int n)
{
// store the count of even
// decimal value substring
int result = 0;
for (int i = 0; i < n; i++)
{
// substring started with '0'
if (str[i] == '0')
{
// increment result by (n-i)
// because all substring which
// are generate by this character
// produce even decimal value.
result += (n - i);
}
}
return result;
}
// Driver code
public static void Main()
{
string str = "10010";
int n = 5;
Console.WriteLine(evenDecimalValue(str, n));
}
}
// This code is contributed
// by vt_m.
PHP
<?php
// PHP Program to count all
// even decimal value substring .
// function return count of
// even decimal value substring
function evenDecimalValue($str, $n)
{
// store the count of even
// decimal value substring
$result = 0;
for ($i = 0; $i < $n; $i++)
{
// substring started with '0'
if ($str[$i] == '0')
{
// increment result by (n-i)
// because all substring which
// are generated by this character
// produce even decimal value.
$result += ($n - $i);
}
}
return $result;
}
// Driver Code
$str = "10010";
$n = 5;
echo evenDecimalValue($str, $n) ;
return 0;
// This code is contributed by SanjuTomar.
?>
JavaScript
<script>
// Javascript Program to count all even
// decimal value substring .
// function return count of
// even decimal value substring
function evenDecimalValue(str, n)
{
// store the count of even
// decimal value substring
let result = 0;
for (let i = 0; i < n; i++)
{
// substring started with '0'
if (str[i] == '0')
{
// increment result by (n-i)
// because all substring which
// are generate by this character
// produce even decimal value.
result += (n - i);
}
}
return result;
}
let str = "10010";
let n = 5;
document.write(evenDecimalValue(str, n));
// This code is contributed by divyeshrabadiya07.
</script>
Time Complexity: O(n)
Space complexity: O(1)
Similar Reads
Count Palindromic Substrings in a Binary String Given a binary string S i.e. which consists only of 0's and 1's. Calculate the number of substrings of S which are palindromes. String S contains at most two 1's. Examples: Input: S = "011"Output: 4Explanation: "0", "1", "1" and "11" are the palindromic substrings. Input: S = "0" Output: 1Explanatio
7 min read
Number of substrings with odd decimal value in a binary string Given a binary string containing only 0's and 1's. Write a program to find number of sub-strings of this string whose decimal representation is odd. Examples : Input : 101 Output : 3 Explanation : Substrings with odd decimal representation are: {1, 1, 101} Input : 1101 Output : 6 Explanation : Subst
6 min read
Count N-length Binary Strings consisting of "11" as substring Given a positive integer N, the task is to find the number of binary strings of length N which contains "11" as a substring. Examples: Input: N = 2Output: 1Explanation: The only string of length 2 that has "11" as a substring is "11". Input: N = 12Output: 3719 Approach: The idea is to derive the num
8 min read
Count of substrings that start and end with 1 in given Binary String Given a binary string s, the task is to count all substrings that start and end with the character '1'. A valid substring must have both its first and last characters as '1', and can include one or more number of characters in between.Examples:Input: s = "00100101"Output: 3Explanation: Valid substri
7 min read
Count of substrings of a binary string containing K ones Given a binary string of length N and an integer K, we need to find out how many substrings of this string are exist which contains exactly K ones. Examples: Input : s = â10010â K = 1 Output : 9 The 9 substrings containing one 1 are, â1â, â10â, â100â, â001â, â01â, â1â, â10â, â0010â and â010âRecommen
7 min read
Count of substrings of a Binary string containing only 1s Given a binary string of length N, we need to find out how many substrings of this string contain only 1s. Examples: Input: S = "0110111"Output: 9Explanation:There are 9 substring with only 1's characters. "1" comes 5 times. "11" comes 3 times. "111" comes 1 time. Input: S = "000"Output: 0 The_Appro
6 min read