Count of Numbers in a Range where digit d occurs exactly K times in C++

C++Server Side ProgrammingProgramming

We are given with an integer range starting from a variable let's say start till the variable end and a variable k and d. The task is to calculate the count of digits d in a range such that d occurs exactly the k times.

For Example

Input - int start = 10, int end = 100, d = 4 and K = 2

Output - Count of Numbers in a Range where digit d occurs exactly K times are: 1

Explanation - The range is starting from 10 to 100. So, the possible numbers with digit d i.e. 4 occurs exactly k i.e. 2 times is in 44 therefore the count is 1.

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

Output - Count of Numbers in a Range where digit d occurs exactly K times are: 1

Explanation - The range is starting from 10 to 100. So, the possible numbers with digit d i.e. 4 occurs exactly k i.e. 1 time is in 16, 26, 36, 46, 56, 76, 86 and 96 therefore the count is 8. We will not consider 66 as 6 is occurring more than k times.

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 k 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_total function-:
    • Check IF place equals to size of a vector then check IF temp = k then return 1 or return 0.
    • Check IF arr[place][temp][val][rem] not equals to -1 then return the value at arr[place][temp][val][rem].
    • Declare a variable count to store the result.
    • Declare a variable temp_2 and set it to 9 IF val equals 1 ELSE set it with vec[place].
    • Start loop FOR from i to 0 till the temp_2 and check IF i equals d then check IF d note equals to 0 OR d is 0 AND rem = 1 then increment the total by 1 
    • 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;

const int MAX = 20;
int arr[MAX][MAX][2][2];
int d, K;

int set_total(int place, int temp, int val, int rem, vector < int > vec) {
   if (place == vec.size()) {
      if (temp == K) {
         return 1;
      }
      return 0;
   }
   if (arr[place][temp][val][rem] != -1) {
      return arr[place][temp][val][rem];
   }
   int count = 0;
   int temp_2 = (val ? 9 : vec[place]);

   for (int i = 0; i <= temp_2; i++) {
      int total = temp;
      if (i == d) {
         if (d != 0 || (!d && rem)) {
            total++;
         }
      }
      int total_2 = val;
      if (i < vec[place]) {
         total_2 = 1;
      }
      count += set_total(place + 1, total, total_2, rem || (i != 0), vec);
   }
   return arr[place][temp][val][rem] = count;
}

int occurrence_d(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, 0, vec);
}
int main() {
   int start = 10;
   int end = 100;
   d = 4, K = 2;
   int count = occurrence_d(end) - occurrence_d(start - 1);
   cout << "Count of Numbers in a Range where digit d occurs exactly K times are: " << count;
   return 0;
}

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

Output

Count of Numbers in a Range where digit d occurs exactly K times are: 1
raja
Published on 29-Jan-2021 08:30:35
Advertisements