Skip to content
Related Articles

Related Articles

Improve Article

Number of substrings with odd decimal value in a binary string

  • Difficulty Level : Easy
  • Last Updated : 16 Jul, 2021

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 zero’s. 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++




// CPP program to count substrings
// with odd decimal value
#include<iostream>
using namespace std;
 
// function to count number of substrings
// with odd decimal representation
int countSubstr(string s)
{  
    int n = s.length();
     
    // auxiliary array to store count
    // of 1's before ith index
    int auxArr[n] = {0};
     
    if (s[0] == '1')
        auxArr[0] = 1;
     
    // store  count of 1's before
    // i-th  index
    for (int i=1; i<n; i++)
    {
        if (s[i] == '1')
            auxArr[i] = auxArr[i-1]+1;
        else
            auxArr[i] = auxArr[i-1];
    }
     
    // variable to store answer
    int count = 0;
     
    // traverse the string reversely to
    // calculate number of odd substrings
    // before i-th index
    for (int i=n-1; i>=0; i--)   
        if (s[i] == '1')
            count += auxArr[i];   
     
    return count;
}
 
// Driver code
int main()
{
    string s = "1101";   
    cout << countSubstr(s);   
    return 0;
}

Java




// Java program to count substrings
// with odd decimal value
import java.io.*;
import java.util.*;
 
class GFG {
    
// function to count number of substrings
// with odd decimal representation
static int countSubstr(String s)
{
    int n = s.length();
     
    // auxiliary array to store count
    // of 1's before ith index
    int[] auxArr=new int[n];
     
    if (s.charAt(0) == '1')
        auxArr[0] = 1;
     
    // store count of 1's before
    // i-th index
    for (int i=1; i<n; i++)
    {
        if (s.charAt(i) == '1')
            auxArr[i] = auxArr[i-1]+1;
        else
            auxArr[i] = auxArr[i-1];
    }
     
    // variable to store answer
    int count = 0;
     
    // traverse the string reversely to
    // calculate number of odd substrings
    // before i-th index
    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));
     
    }
}
 
// This code is contributed by Gitanjali.

Python3




# python program to count substrings
# with odd decimal value
import math
 
# function to count number of substrings
# with odd decimal representation
def countSubstr( s):
 
    n = len(s)
     
    # auxiliary array to store count
    # of 1's before ith index
    auxArr= [0 for i in range(n)]
     
    if (s[0] == '1'):
        auxArr[0] = 1
     
    # store count of 1's before
    # i-th index
    for i in range(0,n):
         
      if (s[i] == '1'):
            auxArr[i] = auxArr[i-1]+1
      else:
            auxArr[i] = auxArr[i-1]
     
     
    # variable to store answer
    count = 0
     
    # traverse the string reversely to
    # calculate number of odd substrings
    # before i-th index
    for i in range(n-1,-1,-1):
        if (s[i] == '1'):
            count += auxArr[i]
     
    return count
# Driver method
s = "1101"
print (countSubstr(s))
 
# This code is contributed by Gitanjali.

C#




// C# program to count substrings
// with odd decimal value
using System;
 
class GFG {
     
// Function to count number of substrings
// with odd decimal representation
static int countSubstr(string s)
{
    int n = s.Length;
     
    // auxiliary array to store count
    // of 1's before ith index
    int[] auxArr = new int[n];
     
    if (s[0] == '1')
        auxArr[0] = 1;
     
    // store count of 1's before
    // i-th index
    for (int i = 1; i < n; i++)
    {
        if (s[i] == '1')
            auxArr[i] = auxArr[i - 1] + 1;
        else
            auxArr[i] = auxArr[i - 1];
    }
     
    // variable to store answer
    int count = 0;
     
    // Traverse the string reversely to
    // calculate number of odd substrings
    // before i-th index
    for (int i = n - 1; i >= 0; i--)
        if (s[i] == '1')
            count += auxArr[i];
     
    return count;
}
 
// Driver Code
public static void Main () {
    string s = "1101";
    Console.WriteLine(countSubstr(s));
    }
}
 
// This code is contributed by vt_m.

PHP




<?php
// PHP program to count
// substrings with odd
// decimal value
 
// function to count number
// of substrings with odd
// decimal representation
function countSubstr($s)
{
    $n = strlen($s);
     
    // auxiliary array to
    // store count of 1's
    // before ith index
    $auxArr = array();
     
    if ($s[0] == '1')
        $auxArr[0] = 1;
     
    // store count of 1's
    // before i-th index
    for ($i = 1; $i < $n; $i++)
    {
        if ($s[$i] == '1')
            $auxArr[$i] = $auxArr[$i - 1] + 1;
        else
            $auxArr[$i] = $auxArr[$i - 1];
    }
     
    // variable to
    // store answer
    $count = 0;
     
    // traverse the string
    // reversely to calculate
    // number of odd substrings
    // before i-th index
    for ($i = $n - 1; $i >= 0; $i--)
        if ($s[$i] == '1')
            $count += $auxArr[$i];
     
    return $count;
}
 
// Driver code
$s = "1101";
echo countSubstr($s);
 
// This code is contributed by aj_36
?>

Javascript




<script>
 
// Javascript program to count substrings
// with odd decimal value
 
// Function to count number of substrings
// with odd decimal representation
function countSubstr(s)
{
    let n = s.length;
 
    // auxiliary array to store count
    // of 1's before ith index
    let auxArr = new Array(n);
 
    if (s[0] == '1')
        auxArr[0] = 1;
 
    // Store count of 1's before
    // i-th index
    for(let i = 1; i < n; i++)
    {
        if (s[i] == '1')
            auxArr[i] = auxArr[i - 1] + 1;
        else
            auxArr[i] = auxArr[i - 1];
    }
 
    // Variable to store answer
    let count = 0;
 
    // Traverse the string reversely to
    // calculate number of odd substrings
    // before i-th index
    for(let i = n - 1; i >= 0; i--)
        if (s[i] == '1')
            count += auxArr[i];
 
    return count;
}
 
// Driver code
let s = "1101";
 
document.write(countSubstr(s));
 
// This code is contributed by rameshtravel07
 
</script>

Output : 

6

Time Complexity: O(n) 
Auxiliary Space: O(n)
 

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.  To complete your preparation from learning a language to DS Algo and many more,  please refer Complete Interview Preparation Course.

In case you wish to attend live classes with experts, please refer DSA Live Classes for Working Professionals and Competitive Programming Live for Students.




My Personal Notes arrow_drop_up
Recommended Articles
Page :