# Count numbers in range such that digits in it and it’s product with q are unequal

Given a range of numbers [l, r] and an integer . The task is to count all such number in the given range such that any digit of the number does not match with any digit in its product with the given number .

Examples:

```Input : l = 10, r = 12, q = 2
Output : 1
10*2 = 20 which has 0 as same digit
12*2 = 24 which as 2 as same digit
11*2 = 22 no same digit

Input : l = 5, r = 15, q = 2
Output : 9
```

## Recommended: Please try your approach on {IDE} first, before moving on to the solution.

The idea is to run a loop from to to generate all numbers in the range and convert each such number and it’s product with q, i.e. to strings using to_string() method and then check if any character in string2 is present in string1 or not using basic string hashing.

Below is the implementation of the above approach:

## C++

 `// C++ program to count numbers in the range [l, r] ` `// such that all of the digits of the number and ` `// it's product with q are unequal ` ` `  `#include ` `using` `namespace` `std; ` ` `  `// Function to check if all of the digits ` `// in a number and it's product with q ` `// are unequal or not ` `bool` `checkIfUnequal(``int` `n, ``int` `q) ` `{ ` `    ``// convert first number into string ` `    ``string s1 = to_string(n); ` `    ``int` `a = { 0 }; ` ` `  `    ``// Insert elements from 1st number ` `    ``// to hash ` `    ``for` `(``int` `i = 0; i < s1.size(); i++) ` `        ``a[s1[i] - ``'0'``]++; ` ` `  `    ``// Calculate corresponding product ` `    ``int` `prod = n * q; ` ` `  `    ``// Convert the product to string ` `    ``string s2 = to_string(prod); ` ` `  `    ``// Using the hash check if any digit of ` `    ``// product matches with the digits of ` `    ``// input number ` `    ``for` `(``int` `i = 0; i < s2.size(); i++) ` `        ``if` `(a[s2[i] - ``'0'``]) ``// if yes, return false ` `            ``return` `false``; ` ` `  `    ``// else, return true ` `    ``return` `true``; ` `} ` ` `  `// Function to count numbers in the range [l, r] ` `// such that all of the digits of the number and ` `// it's product with q are unequal ` `int` `countInRange(``int` `l, ``int` `r, ``int` `q) ` `{ ` `    ``int` `count = 0; ` ` `  `    ``for` `(``int` `i = l; i <= r; i++) { ` `        ``// check for every number between l and r ` `        ``if` `(checkIfUnequal(i, q)) ` `            ``count++; ` `    ``} ` ` `  `    ``return` `count; ` `} ` ` `  `// Driver Code ` `int` `main() ` `{ ` ` `  `    ``int` `l = 10, r = 12, q = 2; ` ` `  `    ``cout << countInRange(l, r, q); ` ` `  `    ``return` `0; ` `} `

## Java

 `// Java program to count numbers in the range [l, r]  ` `// such that all of the digits of the number and  ` `// it's product with q are unequal  ` ` `  `class` `GfG {  ` ` `  `// Function to check if all of the digits  ` `// in a number and it's product with q  ` `// are unequal or not  ` `static` `boolean` `checkIfUnequal(``int` `n, ``int` `q)  ` `{  ` `    ``// convert first number into string  ` `    ``String s1 = Integer.toString(n);  ` `    ``int` `a[] = ``new` `int``[``26``]; ` ` `  `    ``// Insert elements from 1st number  ` `    ``// to hash  ` `    ``for` `(``int` `i = ``0``; i < s1.length(); i++)  ` `        ``a[s1.charAt(i) - ``'0'``]++;  ` ` `  `    ``// Calculate corresponding product  ` `    ``int` `prod = n * q;  ` ` `  `    ``// Convert the product to string  ` `    ``String s2 = Integer.toString(prod);  ` ` `  `    ``// Using the hash check if any digit of  ` `    ``// product matches with the digits of  ` `    ``// input number  ` `    ``for` `(``int` `i = ``0``; i < s2.length(); i++)  ` `        ``if` `(a[s2.charAt(i) - ``'0'``] == ``1``) ``// if yes, return false  ` `            ``return` `false``;  ` ` `  `    ``// else, return true  ` `    ``return` `true``;  ` `}  ` ` `  `// Function to count numbers in the range [l, r]  ` `// such that all of the digits of the number and  ` `// it's product with q are unequal  ` `static` `int` `countInRange(``int` `l, ``int` `r, ``int` `q)  ` `{  ` `    ``int` `count = ``0``;  ` ` `  `    ``for` `(``int` `i = l; i <= r; i++) {  ` `        ``// check for every number between l and r  ` `        ``if` `(checkIfUnequal(i, q))  ` `            ``count++;  ` `    ``}  ` ` `  `    ``return` `count;  ` `}  ` ` `  `// Driver Code  ` `public` `static` `void` `main(String[] args)  ` `{  ` ` `  `    ``int` `l = ``10``, r = ``12``, q = ``2``;  ` ` `  `    ``System.out.println(countInRange(l, r, q));  ` `} ` `}  `

## Python3

 `# Python 3 program to count numbers in  ` `# the range [l, r] such that all of the  ` `# digits of the number and it's product ` `# with q are unequal ` ` `  `# Function to check if all of the digits ` `# in a number and it's product with q ` `# are unequal or not ` `def` `checkIfUnequal(n, q): ` `     `  `    ``# convert first number into string ` `    ``s1 ``=` `str``(n) ` `    ``a ``=` `[``0` `for` `i ``in` `range``(``26``)] ` ` `  `    ``# Insert elements from 1st number ` `    ``# to hash ` `    ``for` `i ``in` `range``(``0``, ``len``(s1), ``1``): ` `        ``a[``ord``(s1[i]) ``-` `ord``(``'0'``)] ``+``=` `1` ` `  `    ``# Calculate corresponding product ` `    ``prod ``=` `n ``*` `q ` ` `  `    ``# Convert the product to string ` `    ``s2 ``=` `str``(prod) ` ` `  `    ``# Using the hash check if any digit of ` `    ``# product matches with the digits of ` `    ``# input number ` `    ``for` `i ``in` `range``(``0``, ``len``(s2), ``1``): ` `         `  `        ``# if yes, return false ` `        ``if` `(a[``ord``(s2[i]) ``-` `ord``(``'0'``)]): ` `            ``return` `False` ` `  `    ``# else, return true ` `    ``return` `True` ` `  `# Function to count numbers in the range [l, r] ` `# such that all of the digits of the number and ` `# it's product with q are unequal ` `def` `countInRange(l, r, q): ` `    ``count ``=` `0` ` `  `    ``for` `i ``in` `range``(l, r ``+` `1``, ``1``): ` `         `  `        ``# check for every number between l and r ` `        ``if` `(checkIfUnequal(i, q)): ` `            ``count ``+``=` `1` `     `  `    ``return` `count ` ` `  `# Driver Code ` `if` `__name__ ``=``=` `'__main__'``: ` `    ``l ``=` `10` `    ``r ``=` `12` `    ``q ``=` `2` ` `  `    ``print``(countInRange(l, r, q)) ` ` `  `# This code is contributed by ` `# Sahil_Shelangia `

## C#

 `// C# program to count numbers in the range [l, r]  ` `// such that all of the digits of the number and  ` `// it's product with q are unequal  ` `using` `System; ` ` `  `class` `GfG  ` `{  ` `     `  `    ``// Function to check if all of the digits  ` `    ``// in a number and it's product with q  ` `    ``// are unequal or not  ` `    ``static` `bool` `checkIfUnequal(``int` `n, ``int` `q)  ` `    ``{  ` `         `  `        ``// convert first number into string  ` `        ``string` `s1 = n.ToString();  ` `        ``int` `[]a = ``new` `int``; ` `     `  `        ``// Insert elements from 1st number  ` `        ``// to hash  ` `        ``for` `(``int` `i = 0; i < s1.Length; i++)  ` `            ``a[s1[i] - ``'0'``]++;  ` `     `  `        ``// Calculate corresponding product  ` `        ``int` `prod = n * q;  ` `     `  `        ``// Convert the product to string  ` `        ``string` `s2 = prod.ToString();  ` `     `  `        ``// Using the hash check if any digit of  ` `        ``// product matches with the digits of  ` `        ``// input number  ` `        ``for` `(``int` `i = 0; i < s2.Length; i++)  ` `            ``if` `(a[s2[i] - ``'0'``] == 1) ``// if yes, return false  ` `                ``return` `false``;  ` `     `  `        ``// else, return true  ` `        ``return` `true``;  ` `    ``}  ` `     `  `    ``// Function to count numbers in the range [l, r]  ` `    ``// such that all of the digits of the number and  ` `    ``// it's product with q are unequal  ` `    ``static` `int` `countInRange(``int` `l, ``int` `r, ``int` `q)  ` `    ``{  ` `        ``int` `count = 0;  ` `     `  `        ``for` `(``int` `i = l; i <= r; i++)  ` `        ``{  ` `             `  `            ``// check for every number between l and r  ` `            ``if` `(checkIfUnequal(i, q))  ` `                ``count++;  ` `        ``}  ` `     `  `        ``return` `count;  ` `    ``}  ` `     `  `    ``// Driver Code  ` `    ``public` `static` `void` `Main()  ` `    ``{  ` `     `  `        ``int` `l = 10, r = 12, q = 2;  ` `     `  `        ``Console.WriteLine(countInRange(l, r, q));  ` `    ``} ` `}  ` ` `  `// This code is contributed bt Archana_kumari `

## PHP

 ` `

Output:

```1
```

My Personal Notes arrow_drop_up Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.