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 : Substrigs with odd decimal 
              representation are:
              {1, 1, 101}

Input : 1101
Output : 6
Explanation : Substrigs 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++

filter_none

edit
close

play_arrow

link
brightness_4
code

// 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;
}

chevron_right


Java

filter_none

edit
close

play_arrow

link
brightness_4
code

// 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.

chevron_right


Python3

filter_none

edit
close

play_arrow

link
brightness_4
code

# 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.

chevron_right


C#

filter_none

edit
close

play_arrow

link
brightness_4
code

// 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.

chevron_right


PHP

filter_none

edit
close

play_arrow

link
brightness_4
code

<?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
?>

chevron_right



Output :

6

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



My Personal Notes arrow_drop_up

Senior Technical Content Engineer | GeeksforGeeks

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.



Improved By : jit_t



Article Tags :
Practice Tags :


Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.