- Trending Categories
- Data Structure
- Networking
- RDBMS
- Operating System
- Java
- iOS
- HTML
- CSS
- Android
- Python
- C Programming
- C++
- C#
- MongoDB
- MySQL
- Javascript
- PHP

- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who

Given two numbers start and end as range variables and an integer q as input. The goal is to find the numbers within a range such that the number and its product with q have no common digits.

If the number is 5 and q is 3 then the product will be 15. Both 5 and 15 have a common digit 5.

If the number is 2 and q is 5 then the product will be 10. Both 2 and 10 have no common digit.

**For Example**

**Input -** start = 5, end = 10, q = 2

**Output -** Count of numbers in range such that digits in it and its product with q are unequal are: 5

**Explanation -** Numbers will be:

- 5 ( 5 * 2 = 10 )
- 6 ( 6 * 2 = 12 )
- 7 ( 7 * 2 = 14 )
- 8 ( 8 * 2 = 16 )
- 9 ( 9 * 2 = 18 )

**Input -** start = 20, end = 25, q = 5

**Output -** Count of numbers in range such that digits in it and it's product with q are unequal are: 2

**Explanation -** Numbers will be:

- 22 ( 22 * 5 = 110 )
- 23 ( 23 * 5 = 115 )

In this approach we will traverse from start to end and convert each number and its product with q to strings. Now create an array arr[26] which will store counts of characters of current number. Now traverse string of product (current number * q) and if any character of that string has non-zero value in arr[] then it is common so return 0. Otherwise return 1.

- Take range variables and a value q.
- Function check(int i, int q) takes a number i and q and returns 1 if digits of i and q are unequal or not common.
- Convert i to string using str = to_string(i).
- Convert product ( temp=q*i ) to sting using str_2 = to_string(temp).
- Take frequency array arr[26] = { 0 } for counts of characters of str.
- Traverse str using for loop and update frequencies using arr[str[j] - '0']++.
- Traverse str_2 using a for loop and check if any arr[str_2[j] - '0'] is non-zero, if yes then its common. Return 0.
- Otherwise return 1.
- Function unequal(int start, int end, int q) takes range variables and q and returns the count of numbers in range such that digits in it and it's product with q are unequal.
- Take the initial count as 0.
- Traverse numbers using for loop from i-start to i=end.
- Using check(i, q) find if number i and its product with q have no digits common. If yes then increment count.
- At the end return count as result.

#include <bits/stdc++.h> using namespace std; int check(int i, int q) { string str = to_string(i); int length = str.size(); int arr[26] = { 0 }; int temp = i * q; string str_2 = to_string(temp); int length_2 = str_2.size(); for (int j = 0; j < length; j++) { arr[str[j] - '0']++; } for (int j = 0; j < length_2; j++) { if (arr[str_2[j] - '0']) { return 0; } } return 1; } int unequal(int start, int end, int q) { int count = 0; for (int i = start; i <= end; i++) { if (check(i, q)) { count++; } } return count; } int main() { int start = 20, end = 40, q = 4; cout << "Count of numbers in range such that digits in it and it's product with q are unequal are: " << unequal(start, end, q); return 0; }

If we run the above code it will generate the following output −

Count of numbers in range such that digits in it and it's product with q are unequal are: 1

- Related Questions & Answers
- Count pairs (p, q) such that p occurs in array at least q times and q occurs at least p times in C++
- Count numbers in range that are divisible by all of its non-zero digits in C++
- Count Numbers with N digits which consists of odd number of 0's in C++
- Count Numbers with N digits which consists of even number of 0's in C++
- Count Numbers with Unique Digits in C++
- Count numbers with same first and last digits in C++
- Add N digits to A such that it is divisible by B after each addition in C++?
- Count ordered pairs of positive numbers such that their sum is S and XOR is K in C++
- Count numbers in a range that are divisible by all array elements in C++
- Print numbers with digits 0 and 1 only such that their sum is N in C Program.
- Find an array element such that all elements are divisible by it using c++
- Count of Numbers such that difference between the number and sum of its digits not less than L in C++
- Find smallest number K such that K % p = 0 and q % K = 0 in C++
- Convert a number of length N such that it contains any one digit at least 'K' times in C++
- Add N digits to A such that it is divisible by B after each addition?

Advertisements