Count numbers from given range having odd digits at odd places and even digits at even places | Function to calculate 5 ^ p ; Stores the result ; Multiply 5 p times ; Return the result ; Function to count all numbers upto N having odd digits at odd places and even digits at even places ; Stores the count ; Stores the digits of N ; Insert the digits of N ; Reverse the vector to arrange the digits from first to last ; Stores count of digits of n ; Stores the count of numbers with i digits ; If the last digit is reached , subtract numbers eceeding range ; Iterate over all the places ; Stores the digit in the pth place ; Stores the count of numbers having a digit greater than x in the p - th position ; Calculate the count of numbers exceeding the range if p is even ; Calculate the count of numbers exceeding the range if p is odd ; Subtract the count of numbers exceeding the range from total count ; If the parity of p and the parity of x are not same ; Add count of numbers having i digits and satisfies the given conditions ; Return the total count of numbers till n ; Function to calculate the count of numbers from given range having odd digits places and even digits at even places ; Count of numbers in range [ L , R ] = Count of numbers till R - ; Count of numbers till ( L - 1 ) ; Driver Code
function getPower ( p ) { var res = 1 ; while ( p -- ) { res *= 5 ; } return res ; } function countNumbersUtil ( N ) { var count = 0 ; var digits = [ ] ; while ( N ) { digits . push ( N % 10 ) ; N = parseInt ( N / 10 ) ; } digits . reverse ( ) ; var D = digits . length ; for ( var i = 1 ; i <= D ; i ++ ) { var res = getPower ( i ) ; if ( i == D ) { for ( var p = 1 ; p <= D ; p ++ ) { var x = digits [ p - 1 ] ; var tmp = 0 ; if ( p % 2 == 0 ) { tmp = ( 5 - parseInt ( x / ( 2 + 1 ) ) ) * getPower ( D - p ) ; } else { tmp = ( 5 - parseInt ( ( x + 1 ) / 2 ) ) * getPower ( D - p ) ; } res -= tmp ; if ( p % 2 != x % 2 ) { break ; } } } count += res ; } return count ; } function countNumbers ( L , R ) { document . write ( ( countNumbersUtil ( R ) - countNumbersUtil ( L - 1 ) ) + " " ) ; } var L = 128 , R = 162 ; countNumbers ( L , R ) ;