Count natural numbers whose all permutation are greater than that number in C++

C++Server Side ProgrammingProgramming

We are given a natural number let’s say, num and the task is to calculate the count of all those natural numbers whose all permutations are greater than that number.

We are working with the following conditions

  • The data should be natural numbers only

  • All the possible permutations or arrangement of a natural number should be equal or greater than the given number. For example, the number is 20

    • Consider all the numbers till 20 starting from 1 i.e. 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20

    • Now check those numbers whose arrangement or permutation is equaled or greater than the given number i.e. 20. Numbers are 1, 2, 3, 4, 5, 6, 7, 8, 9, 11=11, 12<21, 13<31, 14<41, 15<51, 16<61, 17<71, 18<81, 19<91. So the count will be 18.

Input − num = 10

Output − count is 9

Explanation − numbers 1, 2, 3, 4, 5, 6, 7, 8, 9 are the numbers that are equals to the number when arranged in any manner.

Input − num = 13

Output − count is 12

Explanation − numbers 1, 2, 3, 4, 5, 6, 7, 8, 9, 11, 12<21, 13<31 are the numbers that are equals to greater than the number when arranged in any manner.

Approach used in the below program is as follows

  • Input the value of number num

  • Set the max_size to 9 as there will always be at least 9 numbers that will have permutation equals to or greater than the number itself.

  • Start the loop from 0 till max_size

  • Inside the loop, create a list type variable and check if i is less than or equals to num IF yes then insert i to the list and increment the count by 1

  • Traverse the list from the end till the beginning and start another loop from the first element till 9.

  • Check if temp <= num then push temp at the front in the list and increase the count by 1

  • Return the count

  • Print the result.

Example

 Live Demo

#include<bits/stdc++.h>
using namespace std;
//function to Count natural numbers whose
//all permutation are greater than that number
void count(int num){
   int count = 0;
   int max_size = 9;
   for (int i = 1; i <= max_size; i++){
      list<int> lists;
      if (i <= num){
         //insert element at the end of the list
         lists.push_back(i);
         count = count + 1;
      }
      //iterator starts from the last of the list
      for(auto iter = lists.end(); iter != lists.begin(); ++iter){
         int first_ele = lists.front();
         lists.pop_front();
         for (int next = first_ele%10; next <= 9; next++){
            int temp = first_ele*10 + next;
            if (temp <= num){
               lists.push_front(temp);
               count++;
            }
         }
      }
   }
   cout<<"count of num "<<num <<" is "<<count<<endl;
}
int main(){
   count(1);
   count(9);
   count(7);
   count(0);
   count(12);
   return 0;
}

Output

If we run the above code we will get the following output −

count of num 1 is 1
count of num 9 is 9
count of num 7 is 7
count of num 0 is 0
count of num 12 is 11
raja
Published on 06-Jun-2020 15:41:07
Advertisements