N Digit numbers with digits in increasing order | Practice | GeeksforGeeks

Given an integer N, print all the N digit numbers in increasing order, such that their digits are in strictly increasing order(from left to right).
Example 1:
Input:
N = 1
Output:
0 1 2 3 4 5 6 7 8 9
Explanation:
Single digit numbers are considered to be 
strictly increasing order.

Example 2:
Input:
N = 2
Output:
12 13 14 15 16 17 18 19 23....79 89
Explanation:
For N = 2, the correct sequence is
12 13 14 15 16 17 18 19 23 and so on 
up to 89.
Your Task:  
You don't need to read input or print anything. Your task is to complete the function increasingNumbers() which takes an integer N as an input parameter and return the list of numbers such that their digits are in strictly increasing order.
Expected Time Complexity: O(9N)
Expected Auxiliary Space: O(N)
Constraints:
1 <= N <= 9
-1
blank551k2 months ago
class Solution
{
public:
   void answer(vector<int>& a,int n,int i,int fill,int num){
       if(n==1){
           for(int j=0;j<10;++j){
               a.push_back(j);
           }
           return;
       }
       
       if(i>n){
           a.push_back(num);
           return ;
       }
       
       while(i<=n){
           int t = (9-(n-i));
           int j = fill;
           for(j=fill;j<=t;++j){
               answer(a,n,i+1,j+1,num*10+j);
           }
           i++;
           if(num<pow(fill,j)){
               return;
           }
       }
       
       return;
   }
   vector<int> increasingNumbers(int n)
   {
       vector<int> a;
       answer(a,n,1,1,0);
       return a;
   }
};
-1
akamazing4122 months ago
//Simple Recursive solution.........................
class Solution
{
public:
   void printIncresingNumbers(vector<int> &ans,int start, string &out, int n) {
       if (out.length() == n){
        //cout<<out<<" ";
        int x;
        x = stoi(out);
        ans.push_back(x);
        return;
      }
      if(n==1)
         start=0;
      for (int i = start; i <= 9; i++){
        string str = out + to_string(i);
        printIncresingNumbers(ans,i + 1, str, n);
      }
   }
   vector<int> increasingNumbers(int N)
   {
       // Write Your Code here
       vector<int> ans;
       string s = "";
       printIncresingNumbers(ans,1,s,N);
       return ans;
   }
};
-1
tarun2002ts0213 months ago
incomplete iterative approch
 
vector<int> increasingNumbers(int N)   {       // Write Your Code here       vector<int>v;       vector<int>digit(N,0);       if(N==1)       {           for(int i=0;i<10;i++)           v.push_back(i);           return v;       }       int limit=1,start=1;       for(int i=0;i<N;i++)       limit=limit*10;       limit--;       vector<int>digit(N,0);              while(--N)       start=start*10;              int temp=start;       for(int i=digit.size()-1;i>=0;i++)       {           digit[i]=temp%10;           temp=temp/10;       }       for(int i=start;i<=limit;i++)       {             bool flag=true;           for(i=1;i<digit.size();i++)           {              if(digit[i-1]>digit[i])               {                  flag=false;                  break;              }           }           if(flag==true)           v.push_back(i);                      for(i=digit.size();i>=0;i++)           {               if(digit[i]+1<10)               digit[i]++;               else()           }       }       return v;   }
0
rogueninjaofkonoha3 months ago
class Solution{
    static ArrayList<Integer> increasingNumbers(int N){
        // code here
        
    return getnums(N,9,N-1);
    }
    
    static ArrayList<Integer> getnums(int N, int prev,int pos) {
        
        ArrayList<Integer> ret = new ArrayList<Integer>();
        if(N==1) {
            for(int j=0;j<=prev;j++) {
                ret.add(j);
            }
            return ret;
            
        }
        if(pos == 0) {
            for(int j=1;j<=prev;j++) {
                ret.add(j);
            }
            return ret;
        }
        
        for(int j=prev;j>=0;j--) {
                 
             ArrayList<Integer> temp = getnums(N,j-1,pos-1);
            if(temp != null)
            for(int k=0;k<temp.size();k++) {
                temp.set(k,( j + temp.get(k)*10));
            }
         if(temp!=null)
            ret.addAll(temp);
        }
        Collections.sort(ret);
        return ret;
    }
}
+1
manushresthofficial246693 months ago
what is this segmentation fault??? i am getting this , my code is giving output btw...
 
+1
sindhujasai789
This comment was deleted.
0
gs5921483 months ago
// Easy recursive solution 
class Solution{public:vector<int>ans;void find_ans(int num,int n){ if(n==0){     ans.push_back(num);     return ; } int m=num; for(int i=(num%10)+1; i<10;i++){      num=m*10+i;      find_ans(num,n-1); }}   vector<int> increasingNumbers(int n)   {     if(n==1)ans.push_back(0);   find_ans(0,n);   return ans;   }};
0
triple2double13 months ago
class Solution{public:   vector<int> ans;   void recursion(int curr, int n, int m)   {       if (n == m){           ans.push_back(curr);           return;       }       int i=0;       if (m == 1)           i=curr % 10;       else          i=curr % 10+1;       for (; i < 10; i++)           recursion(curr * 10 + i, n + 1,m);   }   vector<int> increasingNumbers(int n){       recursion(0,0,n);       return ans;   };}; 
+3
gkumars29563 months ago
 
//c++ solution
class Solution{public:
   vector<int> v ;       void rec(int last, int n, int num)       {           if (n == 0)           {               v.push_back(num) ;                return;           }           for (int i = last; i <= 9; i++)           {               int a = num * 10 + i;               rec(i + 1, n - 1, a);           }       }       vector<int> increasingNumbers(int N)       {           if (N == 1)           {               for (int i = 0; i <= 9; i++)               v.push_back(i);               return v;           }           else           {               rec(1, N, 0);               return v;           }       }}; 
0
mayank20453 months ago
class Solution {
    static ArrayList<Integer> list = new ArrayList<Integer>();

    static ArrayList<Integer> increasingNumbers(int N) {
        // code here

        if (N == 1) {

            list.add(0);

        }
        function(N, 0, 1);
        return list;
    }

    static void function(int N, int num, int i) {

        if (N == 0) {
            list.add(num);
            return;
        }

        int S = 0;
        while (i < 10) {

            S = num + i * (int) (Math.pow(10, (N - 1)));

            function(N - 1, S, i + 1);

            ++i;

        }
    }

   
}
We strongly recommend solving this problem on your own before viewing its editorial. Do you still
                    want to view the editorial?
 Login to access your submissions. 

Problem


Contest

Reset the IDE using the second button on the top right corner.
Avoid using static/global variables in your code as your code is tested against multiple test cases and these tend to retain their previous values.
Passing the Sample/Custom Test cases does not guarantee the correctness of code. On submission, your code is tested against multiple test cases consisting of all possible corner cases and stress constraints.
You can access the hints to get an idea about what is expected of you as well as the final solution code.
You can view the solutions submitted by other users from the submission tab.