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
Explanation:
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++
#include <bits/stdc++.h>
using namespace std;
int countNumber( int n)
{
int result = 0;
stack< int > s;
for ( int i = 1; i <= 9; i++)
{
if (i <= n)
{
s.push(i);
result++;
}
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;
}
int main()
{
int n = 15;
cout << countNumber(n) << endl;
return 0;
}
|
Java
import java.util.Stack;
class GFG
{
static int countNumber( int n)
{
int result = 0 ;
Stack<Integer> s = new Stack<>();
for ( int i = 1 ; i <= 9 ; i++)
{
if (i <= n)
{
s.push(i);
result++;
}
while (!s.empty())
{
int tp = s.pop();
for ( int j = tp % 10 ; j <= 9 ; j++)
{
int x = tp * 10 + j;
if (x <= n) {
s.push(x);
result++;
}
}
}
}
return result;
}
public static void main(String[] args)
{
int n = 15 ;
System.out.println(countNumber(n));
}
}
|
Python3
def countNumber(n):
result = 0
s = []
for i in range ( 1 , 10 ):
if (i < = n):
s.append(i)
result + = 1
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
if __name__ = = '__main__' :
n = 15
print (countNumber(n))
|
C#
using System;
using System.Collections.Generic;
class GFG {
static int countNumber( int n)
{
int result = 0;
Stack< int > s = new Stack< int >();
for ( int i = 1; i <= 9; i++)
{
if (i <= n)
{
s.Push(i);
result++;
}
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;
}
public static void Main(String[] args)
{
int n = 15;
Console.WriteLine(countNumber(n));
}
}
|
Javascript
<script>
function countNumber(n)
{
let result = 0;
let s = [];
for (let i = 1; i <= 9; i++)
{
if (i <= n)
{
s.push(i);
result++;
}
while (s.length != 0)
{
let tp = s[s.length - 1];
s.pop();
for (let j = tp % 10; j <= 9; j++)
{
let x = tp * 10 + j;
if (x <= n)
{
s.push(x);
result++;
}
}
}
}
return result;
}
let n = 15;
document.write(countNumber(n));
</script>
|
Time Complexity : O(x) where x is number of elements printed in output.
Auxiliary Space: O(x) as it is using stack
If you like GeeksforGeeks and would like to contribute, you can also write an article using write.geeksforgeeks.org or mail your article to review-team@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Feeling lost in the world of random DSA topics, wasting time without progress? It's time for a change! Join our DSA course, where we'll guide you on an exciting journey to master DSA efficiently and on schedule.
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 geeks!
Last Updated :
11 Sep, 2023
Like Article
Save Article