Count numbers from given range having odd digits at odd places and even digits at even places | Java program to implement the above approach ; 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
import java . util . * ; import java . util . Vector ; import java . util . Collections ; class GFG { static int getPower ( int p ) { int res = 1 ; while ( p > 0 ) { res *= 5 ; p -- ; } return res ; } static int countNumbersUtil ( int N ) { int count = 0 ; Vector < Integer > digits = new Vector < Integer > ( ) ; while ( N > 0 ) { digits . add ( N % 10 ) ; N /= 10 ; } Collections . reverse ( digits ) ; int D = digits . size ( ) ; for ( int i = 1 ; i <= D ; i ++ ) { int res = getPower ( i ) ; if ( i == D ) { for ( int p = 1 ; p <= D ; p ++ ) { int x = digits . get ( p - 1 ) ; int tmp = 0 ; if ( p % 2 == 0 ) { tmp = ( 5 - ( x / 2 + 1 ) ) * getPower ( D - p ) ; } else { tmp = ( 5 - ( x + 1 ) / 2 ) * getPower ( D - p ) ; } res -= tmp ; if ( p % 2 != x % 2 ) { break ; } } } count += res ; } return count ; } static void countNumbers ( int L , int R ) { System . out . println ( countNumbersUtil ( R ) - countNumbersUtil ( L - 1 ) ) ; } public static void main ( String args [ ] ) { int L = 128 , R = 162 ; countNumbers ( L , R ) ; } }