Queries to count numbers from a range which does not contain digit K in their decimal or octal representation | Java program for the above approach ; Function to check if the given digit ' K ' is present in the decimal and octal representations of num or not ; Stores if the digit exists or not ; Iterate till nums is non - zero ; Find the remainder ; If the remainder is K ; Function to count the numbers in the range [ 1 , N ] such that it doesn ' t ▁ ▁ contain ▁ the ▁ digit ▁ ' K ' in its decimal  and octal representation ; Stores count of numbers in the range [ 0 , i ] that contains the digit ' K ' in its octal or decimal representation ; Traverse the range [ 0 , 1e6 + 5 ] ; Check if i contains the digit ' K ' in its decimal or octal representation ; Update pref [ i ] ; Print the answer of queries ; Driver code ; Function Call
import java . util . * ; public class GFG { static boolean contains ( int num , int K , int Base ) { boolean isThere = false ; while ( num > 0 ) { int remainder = num % Base ; if ( remainder == K ) { isThere = true ; } num /= Base ; } return isThere ; } static void count ( int n , int k , Vector < Vector < Integer > > v ) { int [ ] pref = new int [ 1000005 ] ; for ( int i = 1 ; i < 1e6 + 5 ; i ++ ) { boolean present = contains ( i , k , 10 ) || contains ( i , k , 8 ) ; if ( present ) { pref [ i ] += pref [ i - 1 ] + 1 ; } } System . out . print ( ( v . get ( 0 ) . get ( 1 ) - v . get ( 0 ) . get ( 0 ) + 1 - ( pref [ v . get ( 0 ) . get ( 1 ) ] - pref [ v . get ( 0 ) . get ( 0 ) - 1 ] ) ) + " ▁ " ) ; System . out . print ( ( v . get ( 1 ) . get ( 1 ) - v . get ( 1 ) . get ( 0 ) - ( pref [ v . get ( 1 ) . get ( 1 ) ] - pref [ v . get ( 1 ) . get ( 0 ) - 1 ] ) ) + " ▁ " ) ; } public static void main ( String [ ] args ) { int K = 7 ; Vector < Vector < Integer > > Q = new Vector < Vector < Integer > > ( ) ; Q . add ( new Vector < Integer > ( ) ) ; Q . get ( 0 ) . add ( 2 ) ; Q . get ( 0 ) . add ( 5 ) ; Q . add ( new Vector < Integer > ( ) ) ; Q . get ( 1 ) . add ( 1 ) ; Q . get ( 1 ) . add ( 15 ) ; int N = Q . size ( ) ; count ( N , K , Q ) ; } }