There are some natural number whose all permutation is greater than or equal to that number eg. 123, whose all the permutation (123, 231, 321) are greater than or equal to 123.

Given a natural number **n**, the task is to count all such number from 1 to n.

**Examples:**

Input : n = 15. Output : 14 1, 2, 3, 4, 5, 6, 7, 8, 9, 11, 12, 13, 14, 15 are the numbers whose all permutation is greater than the number itself. So, output 14. Input : n = 100. Output : 54

A **simple solution **is to run a loop from 1 to n and for every number check if its digits are in non-decreasing order or not.

An **efficient solution** is based on below observations.

Observation 1: From 1 to 9, all number have this property. So, for n <= 9, output n.

Observation 2: The number whose all permutation is greater than or equal to that number have all their digits in increasing order.

The idea is to push all the number from 1 to 9. Now, pop the top element, say **topel** and try to make number whose digits are in increasing order and the first digit is **topel**. To make such numbers, the second digit can be from **topel%10** to 9. If this number is less than **n**, increment the count and push the number in the stack, else ignore.

Below is the implementation of this approach:

## C++

`// C++ program to count the number less than N, ` `// whose all permutation is greater than or equal to the number. ` `#include<bits/stdc++.h> ` `using` `namespace` `std; ` ` ` `// Return the count of the number having all ` `// permutation greater than or equal to the number. ` `int` `countNumber(` `int` `n) ` `{ ` ` ` `int` `result = 0; ` ` ` ` ` `// Pushing 1 to 9 because all number from 1 ` ` ` `// to 9 have this property. ` ` ` `for` `(` `int` `i = 1; i <= 9; i++) ` ` ` `{ ` ` ` `stack<` `int` `> s; ` ` ` `if` `(i <= n) ` ` ` `{ ` ` ` `s.push(i); ` ` ` `result++; ` ` ` `} ` ` ` ` ` `// take a number from stack and add ` ` ` `// a digit smaller than last digit ` ` ` `// of it. ` ` ` `while` `(!s.empty()) ` ` ` `{ ` ` ` `int` `tp = s.top(); ` ` ` `s.pop(); ` ` ` `for` `(` `int` `j = tp%10; j <= 9; j++) ` ` ` `{ ` ` ` `int` `x = tp*10 + j; ` ` ` `if` `(x <= n) ` ` ` `{ ` ` ` `s.push(x); ` ` ` `result++; ` ` ` `} ` ` ` `} ` ` ` `} ` ` ` `} ` ` ` ` ` `return` `result; ` `} ` ` ` `// Driven Program ` `int` `main() ` `{ ` ` ` `int` `n = 15; ` ` ` `cout << countNumber(n) << endl; ` ` ` `return` `0; ` `} ` |

*chevron_right*

*filter_none*

## Java

`import` `java.util.Stack; ` ` ` `// Java program to count the number less than N, ` `// whose all permutation is greater than or equal to the number. ` `class` `GFG { ` `// Return the count of the number having all ` `// permutation greater than or equal to the number. ` ` ` ` ` `static` `int` `countNumber(` `int` `n) { ` ` ` `int` `result = ` `0` `; ` ` ` ` ` `// Pushing 1 to 9 because all number from 1 ` ` ` `// to 9 have this property. ` ` ` `for` `(` `int` `i = ` `1` `; i <= ` `9` `; i++) { ` ` ` `Stack<Integer> s = ` `new` `Stack<>(); ` ` ` `if` `(i <= n) { ` ` ` `s.push(i); ` ` ` `result++; ` ` ` `} ` ` ` ` ` `// take a number from stack and add ` ` ` `// a digit smaller than last digit ` ` ` `// of it. ` ` ` `while` `(!s.empty()) { ` ` ` `int` `tp = s.peek(); ` ` ` `s.pop(); ` ` ` `for` `(` `int` `j = tp % ` `10` `; j <= ` `9` `; j++) { ` ` ` `int` `x = tp * ` `10` `+ j; ` ` ` `if` `(x <= n) { ` ` ` `s.push(x); ` ` ` `result++; ` ` ` `} ` ` ` `} ` ` ` `} ` ` ` `} ` ` ` ` ` `return` `result; ` ` ` `} ` ` ` `// Driven Program ` ` ` `public` `static` `void` `main(String[] args) { ` ` ` `int` `n = ` `15` `; ` ` ` `System.out.println(countNumber(n)); ` ` ` ` ` `} ` `} ` ` ` `//this code contributed by Rajput-Ji ` |

*chevron_right*

*filter_none*

## Python3

`# Python3 program to count the number less ` `# than N, whose all permutation is greater ` `# than or equal to the number. ` ` ` `# Return the count of the number having ` `# all permutation greater than or equal ` `# to the number. ` `def` `countNumber(n): ` ` ` `result ` `=` `0` ` ` ` ` `# Pushing 1 to 9 because all number ` ` ` `# from 1 to 9 have this property. ` ` ` `for` `i ` `in` `range` `(` `1` `, ` `10` `): ` ` ` `s ` `=` `[] ` ` ` `if` `(i <` `=` `n): ` ` ` `s.append(i) ` ` ` `result ` `+` `=` `1` ` ` ` ` `# take a number from stack and add ` ` ` `# a digit smaller than last digit ` ` ` `# of it. ` ` ` `while` `len` `(s) !` `=` `0` `: ` ` ` `tp ` `=` `s[` `-` `1` `] ` ` ` `s.pop() ` ` ` `for` `j ` `in` `range` `(tp ` `%` `10` `, ` `10` `): ` ` ` `x ` `=` `tp ` `*` `10` `+` `j ` ` ` `if` `(x <` `=` `n): ` ` ` `s.append(x) ` ` ` `result ` `+` `=` `1` ` ` ` ` `return` `result ` ` ` `# Driver Code ` `if` `__name__ ` `=` `=` `'__main__'` `: ` ` ` ` ` `n ` `=` `15` ` ` `print` `(countNumber(n)) ` ` ` `# This code is contributed by PranchalK ` |

*chevron_right*

*filter_none*

## C#

`// C# program to count the number less than N, ` `// whose all permutation is greater than ` `// or equal to the number. ` `using` `System; ` `using` `System.Collections.Generic; ` ` ` `class` `GFG ` `{ ` ` ` `// Return the count of the number ` `// having all permutation greater than ` `// or equal to the number. ` `static` `int` `countNumber(` `int` `n) ` `{ ` ` ` `int` `result = 0; ` ` ` ` ` `// Pushing 1 to 9 because all number from 1 ` ` ` `// to 9 have this property. ` ` ` `for` `(` `int` `i = 1; i <= 9; i++) ` ` ` `{ ` ` ` `Stack<` `int` `> s = ` `new` `Stack<` `int` `>(); ` ` ` `if` `(i <= n) ` ` ` `{ ` ` ` `s.Push(i); ` ` ` `result++; ` ` ` `} ` ` ` ` ` `// take a number from stack and add ` ` ` `// a digit smaller than last digit ` ` ` `// of it. ` ` ` `while` `(s.Count != 0) ` ` ` `{ ` ` ` `int` `tp = s.Peek(); ` ` ` `s.Pop(); ` ` ` `for` `(` `int` `j = tp % 10; j <= 9; j++) ` ` ` `{ ` ` ` `int` `x = tp * 10 + j; ` ` ` `if` `(x <= n) ` ` ` `{ ` ` ` `s.Push(x); ` ` ` `result++; ` ` ` `} ` ` ` `} ` ` ` `} ` ` ` `} ` ` ` ` ` `return` `result; ` `} ` ` ` `// Driver Code ` `public` `static` `void` `Main(String[] args) ` `{ ` ` ` `int` `n = 15; ` ` ` `Console.WriteLine(countNumber(n)); ` `} ` `} ` ` ` `// This code is contributed by Rajput-Ji ` |

*chevron_right*

*filter_none*

**Output:**

14

**Time Complexity :** O(x) where x is number of elements printed in output.

This article is contributed by **Anuj Chauhan**. If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the **DSA Self Paced Course** at a student-friendly price and become industry ready.

## Recommended Posts:

- Count all Grandparent-Parent-Child Triplets in a binary tree whose sum is greater than X
- Smallest number greater than or equal to X whose sum of digits is divisible by Y
- Count numbers with difference between number and its digit sum greater than specific value
- Largest number less than N with digit sum greater than the digit sum of N
- Count of Array elements greater than all elements on its left and next K elements on its right
- Largest number not greater than N all the digits of which are odd
- Largest number less than N whose each digit is prime number
- Count all prime numbers in a given range whose sum of digits is also prime
- Count all N digit numbers whose digits are multiple of X
- Find the count of natural Hexadecimal numbers of size N
- Largest even digit number not greater than N
- Smallest number greater than Y with sum of digits equal to X
- Smallest number greater than or equal to N using only digits 1 to K
- Count of numbers whose sum of increasing powers of digits is equal to the number itself
- Count numbers in range [L, R] whose sum of digits is a Prime Number
- Smallest integer greater than n such that it consists of digit m exactly k times
- Print all n-digit numbers whose sum of digits equals to given sum
- Count of n digit numbers whose sum of digits equals to given sum
- Count of N digit Numbers whose sum of every K consecutive digits is equal
- Count of N digit Numbers whose sum of every K consecutive digits is equal | Set 2