Count numbers in range such that digits in it and it's product with q are unequal in C++


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.

Let us understand with examples.

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 )

Approach used in the below program is as follows

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.

Example

Live Demo

#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 −

Output

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

Updated on: 29-Jan-2021

118 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements