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 : Substrings with odd decimal
representation are:
{1, 1, 1, 11, 101, 1011}
Brute force Approach: The simplest approach to solve above problem is to generate all possible substrings of the given string and convert them to decimal and check if the decimal representation is odd or not. You may refer to this article for binary to decimal conversion.
Time Complexity: O(n*n)
Efficient approach: An efficient approach is to observe that if the last digit of a binary number is 1 then it is odd otherwise it is even. So our problem now is reduced to check all substrings with value at last index as 1. We can easily solve this problem in single traversal by traversing from the end. If the value of i-th index in the string is 1 then there is i odd substrings before this index. But this also includes strings with leading zeroes. So to handle this we can take an auxiliary array to keep count of number of 1’s before ith index. We count only pairs of 1s.
Below is the implementation of this approach:
C++
#include<iostream>
using namespace std;
int countSubstr(string s)
{
int n = s.length();
int auxArr[n] = {0};
if (s[0] == '1' )
auxArr[0] = 1;
for ( int i=1; i<n; i++)
{
if (s[i] == '1' )
auxArr[i] = auxArr[i-1]+1;
else
auxArr[i] = auxArr[i-1];
}
int count = 0;
for ( int i=n-1; i>=0; i--)
if (s[i] == '1' )
count += auxArr[i];
return count;
}
int main()
{
string s = "1101" ;
cout << countSubstr(s);
return 0;
}
|
Java
import java.io.*;
import java.util.*;
class GFG {
static int countSubstr(String s)
{
int n = s.length();
int [] auxArr= new int [n];
if (s.charAt( 0 ) == '1' )
auxArr[ 0 ] = 1 ;
for ( int i= 1 ; i<n; i++)
{
if (s.charAt(i) == '1' )
auxArr[i] = auxArr[i- 1 ]+ 1 ;
else
auxArr[i] = auxArr[i- 1 ];
}
int count = 0 ;
for ( int i=n- 1 ; i>= 0 ; i--)
if (s.charAt(i) == '1' )
count += auxArr[i];
return count;
}
public static void main (String[] args) {
String s = "1101" ;
System.out.println(countSubstr(s));
}
}
|
Python3
import math
def countSubstr( s):
n = len (s)
auxArr = [ 0 for i in range (n)]
if (s[ 0 ] = = '1' ):
auxArr[ 0 ] = 1
for i in range ( 0 ,n):
if (s[i] = = '1' ):
auxArr[i] = auxArr[i - 1 ] + 1
else :
auxArr[i] = auxArr[i - 1 ]
count = 0
for i in range (n - 1 , - 1 , - 1 ):
if (s[i] = = '1' ):
count + = auxArr[i]
return count
s = "1101"
print (countSubstr(s))
|
C#
using System;
class GFG {
static int countSubstr( string s)
{
int n = s.Length;
int [] auxArr = new int [n];
if (s[0] == '1' )
auxArr[0] = 1;
for ( int i = 1; i < n; i++)
{
if (s[i] == '1' )
auxArr[i] = auxArr[i - 1] + 1;
else
auxArr[i] = auxArr[i - 1];
}
int count = 0;
for ( int i = n - 1; i >= 0; i--)
if (s[i] == '1' )
count += auxArr[i];
return count;
}
public static void Main () {
string s = "1101" ;
Console.WriteLine(countSubstr(s));
}
}
|
PHP
<?php
function countSubstr( $s )
{
$n = strlen ( $s );
$auxArr = array ();
if ( $s [0] == '1' )
$auxArr [0] = 1;
for ( $i = 1; $i < $n ; $i ++)
{
if ( $s [ $i ] == '1' )
$auxArr [ $i ] = $auxArr [ $i - 1] + 1;
else
$auxArr [ $i ] = $auxArr [ $i - 1];
}
$count = 0;
for ( $i = $n - 1; $i >= 0; $i --)
if ( $s [ $i ] == '1' )
$count += $auxArr [ $i ];
return $count ;
}
$s = "1101" ;
echo countSubstr( $s );
?>
|
Javascript
<script>
function countSubstr(s)
{
let n = s.length;
let auxArr = new Array(n);
if (s[0] == '1 ')
auxArr[0] = 1;
// Store count of 1' s before
for (let i = 1; i < n; i++)
{
if (s[i] == '1' )
auxArr[i] = auxArr[i - 1] + 1;
else
auxArr[i] = auxArr[i - 1];
}
let count = 0;
for (let i = n - 1; i >= 0; i--)
if (s[i] == '1' )
count += auxArr[i];
return count;
}
let s = "1101" ;
document.write(countSubstr(s));
</script>
|
Time Complexity: O(n)
Auxiliary Space: O(n)
Last Updated :
22 Nov, 2022
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...