Counting even decimal value substrings in a binary string | generate all substring in arr [ 0. . n - 1 ] ; store the count ; Pick starting point ; Pick ending point ; substring between current starting and ending points ; increment power of 2 by one ; Driver program
function evenDecimalValue ( str , n ) { var result = 0 ; for ( var i = 0 ; i < n ; i ++ ) { for ( var j = i ; j < n ; j ++ ) { var decimalValue = 0 ; var powerOf2 = 1 ; for ( var k = i ; k <= j ; k ++ ) { decimalValue += ( ( str [ k ] - ' ' ) * powerOf2 ) ; powerOf2 *= 2 ; } if ( decimalValue % 2 == 0 ) result ++ ; } } return result ; } var str = " " ; var n = 5 ; document . write ( evenDecimalValue ( str , n ) ) ;