Number of substrings with odd decimal value in a binary string
Last Updated :
22 Nov, 2022
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++
// 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>
Time Complexity: O(n)
Auxiliary Space: O(n)
Similar Reads
Split the binary string into substrings with equal number of 0s and 1s Given a binary string str of length N, the task is to find the maximum count of consecutive substrings str can be divided into such that all the substrings are balanced i.e. they have equal number of 0s and 1s. If it is not possible to split str satisfying the conditions then print -1.Example: Input
8 min read
Counting even decimal value substrings in a binary string 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 : 101Output : 2Explanation : Substring are : 1, 10, 101, 0, 01, 1 In decimal form : 1, 1, 3
9 min read
Number of even substrings in a string of digits Given a string of digits 0 - 9. The task is to count a number of substrings which when converting into integer form an even number. Examples : Input : str = "1234".Output : 6"2", "4", "12", "34", "234", "1234" are 6 substring which are even.Input : str = "154".Output : 3Input : str = "15".Output : 0
9 min read
Rotations of a Binary String with Odd Value Given a binary string. We are allowed to do circular rotation of the string without changing the relative order of the bits in the string. For Example, all possible circular rotation of string "011001" are: 101100 010110 001011 100101 110010 We are required to tell total number of distinct odd decim
3 min read
Generate Binary String with equal number of 01 and 10 Subsequence Given an integer N (N > 2), the task is to generate a binary string of size N that consists of equal numbers of "10" & "01" subsequences and also the string should contain at least one '0' and one '1' Note: If multiple such strings exist, print any. Examples: Input: 4Output: 0110Explanation :
7 min read
Number of sub-strings in a given binary string divisible by 2 Given binary string str of length N, the task is to find the count of substrings of str which are divisible by 2. Leading zeros in a substring are allowed. Examples: Input: str = "101" Output: 2 "0" and "10" are the only substrings which are divisible by 2. Input: str = "10010" Output: 10 Naive appr
4 min read
Number of alternating substrings from a given Binary String Given a binary string of size N, the task is to count the number of alternating substrings that are present in the string S. Examples: Input: S = "0010"Output: 7Explanation: All the substring of the string S are: {"0", "00", "001", "0010", "0", "01", "010", "1", "10", "0"}Strings that are alternatin
13 min read
Count of substrings that start and end with 1 in given Binary String Given a binary string, count the number of substrings that start and end with 1. Examples: Input: "00100101"Output: 3Explanation: three substrings are "1001", "100101" and "101" Input: "1001"Output: 1Explanation: one substring "1001" Recommended PracticeCount SubstringsTry It!Count of substrings tha
12 min read
XOR of all substrings of a given Binary String Given a binary string str of size N, the task is to calculate the bitwise XOR of all substrings of str. Examples: Input: str = "11"Output: 11Explanation: The substrings of "11" are: 1, 1, and 11.Their XOR = 1 â 1 â 11 = 11 Input: str = "110"Output: 111Explanation: The substrings of 110 are: 1, 1, 0,
6 min read
Maximum splits in binary string such that each substring is divisible by given odd number Given binary string str, the task is to calculate the maximum possible splits possible to make each substring divisible by a given odd number K.Examples: Input: str = "110111001", K = 9 Output: 2 Explanation: The two possible substrings are "11011" and "1001". The equivalent decimal values are 27 an
5 min read