Count of Numbers in a Range divisible by m and having digit d in even positions in C++

C++Server Side ProgrammingProgramming

We are given with an integer range, a variable m which is used as divisor and a variable d which is used to check whether the digit 'd' is at even position or not and the task is to calculate the count of those numbers in a range which are divisible by the variable m and have digit d in even positions.

For Example

Input - int start = 20, end = 50, d = 8 and m = 4

Output - Count of Numbers in a Range divisible by m and having digit d in even positions are: 2

Explanation - The range is starting from 20 to 50. So the possible numbers with digit d i.e. 8 are 28, 38 and 48 having 8 at even positions i.e. 2 and the numbers 24 and 48 are divisible by m i.e. 4 therefore the count is 2.

Input - int start = 10, end = 100, d = 6 and m = 2

Output - Count of Numbers in a Range divisible by m and having digit d in even positions are: 8

Explanation - The range is starting from 10 to 100. So the possible numbers with digit d i.e. 6, 16, 26, 36, 46, 56, 66, 76, 86 and 96 having 6 at even positions except the number 6 and 66 which are at odd positions so we will not include this and now we will check the numbers from the list that are divisible by 2 so all the numbers i.e. 16, 26, 36, 46, 56, 76, 86 and 96 are divisible by 2 therefore the count is 8.

Approach used in the below program is as follows

  • Create a range of integer numbers starting from the variable start till the variable end and declare the variable d and m and input the values. Pass the data to the function for further processing.
  • Create a variable of type vector let's say vec.
  • Start loop while till the value which is the value inside the variable start. Now, inside the while push the value as val % 10 to the vector and set val as val / 10. 
  • Call the reverse function in STL by passing vec.begin() and vec.end() as an argument to it.
  • Set the values in the array as -1 using memset.
  • Return the set_total(0, 0, 0, vec) which is a function that will check whether the numbers with even position d and are divisible by m 
  • Inside the set_totalfunction-:
    • Check IF place equals to size of a vector then check IF temp = 0 then return 1 or return 0.
    • Check IF arr[place][temp][val] not equals to -1 then return the value at arr[place][temp][val].
    • Check IF place % 2 = 1 then check IF val = 0 then check IF d greater than vec[place] then return 0
    • Declare the variable temp_2 and set it to 0.
    • Check IF d less than vec[place] then set temp_2 to 1.
    • Declare variable temp_3 and make a recursive call to the set_total() and return arr[place][temp][val] = temp_3
    • Declare a variable count to store the result.
    • Declare a variable set_limit and set it to 9 IF val equals 1 ELSE set it with vec[place].
    • Start loop FOR from i to 0 till the set_limit and check IF i equals d then continue 
    • Declare a variable as temp_2 and set it to val
    • Check IF i less than vec[place] then set temp_2 as 1
    • Set count with the recursive call to the function set_total
    • Return arr[place][temp][val] = count.

Example

Live Demo

#include <bits/stdc++.h>
using namespace std;

int arr[20][20][2];
int d, m;

int set_total(int place, int temp, int val, vector < int > vec) {
   if (place == vec.size()) {
      if (temp == 0) {
         return 1;
      }
      return 0;
   }
   if (arr[place][temp][val] != -1) {
      return arr[place][temp][val];
   }
   if (place % 2) {
      if (val == 0) {
         if (d > vec[place]) {
            return 0;
         }
      }
      int temp_2 = val;
      if (d < vec[place]) {
         temp_2 = 1;
      }
      int temp_3 = set_total(place + 1, (10 * temp + d) % m, temp_2, vec);
      return arr[place][temp][val] = temp_3;
   }
   int count = 0;
   int set_limit = (val ? 9 : vec[place]);
   for (int i = 0; i <= set_limit; i++) {
      if (i == d) {
         continue;
      }
      int temp_2 = val;
      if (i < vec[place]) {
         temp_2 = 1;
      }
      count += set_total(place + 1, (10 * temp + i) % m, temp_2, vec);
   }
   return arr[place][temp][val] = count;
}

int divisible(int val) {
   vector < int > vec;
   while (val) {
      vec.push_back(val % 10);
      val = val / 10;
   }
   reverse(vec.begin(), vec.end());
   memset(arr, -1, sizeof(arr));
   return set_total(0, 0, 0, vec);
}
int main() {
   int start = 20, end = 50;
   d = 8, m = 4;
   int count = divisible(end) - divisible(start);
   cout << "Count of Numbers in a Range divisible by m and having digit d in even positions are: " << count;
   return 0;
}

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

Output

Count of Numbers in a Range divisible by m and having digit d in even positions are: 2
raja
Published on 29-Jan-2021 08:29:44
Advertisements