Number of substrings with odd decimal value in a binary string | Function to count number of substrings with odd decimal representation ; auxiliary array to store count of 1 's before ith index ; Store count of 1 's before  i-th index ; Variable to store answer ; Traverse the string reversely to calculate number of odd substrings before i - th index ; Driver code
function countSubstr ( s ) { let n = s . length ; let auxArr = new Array ( n ) ; if ( s [ 0 ] == ' ' ) auxArr [ 0 ] = 1 ; for ( let i = 1 ; i < n ; i ++ ) { if ( s [ i ] == ' ' ) 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 ] == ' ' ) count += auxArr [ i ] ; return count ; } let s = " " ; document . write ( countSubstr ( s ) ) ;