- Related Questions & Answers
- Count of subarrays whose maximum element is greater than k in C++
- Count subarrays with all elements greater than K in C++
- Delete all the nodes from the list that are greater than x in C++
- Count smaller values whose XOR with x is greater than x in C++
- Count smaller numbers whose XOR with n produces greater value in C++
- Count the number of pairs that have column sum greater than row sum in C++
- Find permutation of first N natural numbers that satisfies the given condition in C++
- Count all the numbers less than 10^6 whose minimum prime factor is N C++
- Count numbers with difference between number and its digit sum greater than specific value in C++
- Count number of substrings with numeric value greater than X in C++
- Count of elements whose absolute difference with the sum of all the other elements is greater than k in C++
- C# program to check if all the values in a list that are greater than a given value
- Delete all the nodes from the doubly linked list that are greater than a given value in C++
- How to find numbers in an array that are greater than, less than, or equal to a value in java?
- Count numbers in a range that are divisible by all array elements in C++

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

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.

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.

#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; }

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

Advertisements