N Digit Numbers with Increasing Digits

Last Updated : 24 Jun, 2026

Given an integer n, return all the n digit numbers in increasing order, such that their digits are in strictly increasing order(from left to right).

Examples: 

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.

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.

Try It Yourself
redirect icon

[Naive Approach] Checking Every n-Digit Number – O(10ⁿ × n) Time and O(1) Space

The idea is to traverse every n-digit number and check whether its digits are strictly increasing from left to right. A number is valid if every digit is greater than the previous digit. Each number is converted into a string so that adjacent digits can be compared easily. Since there are only 10 digits (0–9) and digits cannot repeat in a strictly increasing sequence, no valid number exists when n>9. All numbers satisfying the condition are stored in the result.

  • Base case is to return empty array for n > 9
  • Determine the range of n-digit numbers
  • Traverse every number in this range
  • Convert the number into a string
  • Compare adjacent digits: If every next digit is greater than the previous digit, the number is valid
  • Store valid numbers in the result array
C++
#include <iostream>
#include <vector>
#include <string>
#include <cmath>

using namespace std;

bool hasIncreasingDigits(int num, int n) {
    string s = to_string(num);
    
    // Number must have exactly n digits
    if (s.length() != n) return false;
    
    // Check each adjacent digit pair
    for (int i = 1; i < n; i++) {
        if (s[i] <= s[i-1]) return false;
    }
    return true;
}

vector<int> increasingNumbers(int n) {
    vector<int> result;
    
    // Base Case: Empty array in case of n > 9
    if(n > 9) return result;
    // For n=1: start from 0, else start from 10^(n-1)
    int start = (n == 1) ? 0 : pow(10, n - 1);
    int end = pow(10, n) - 1;
    
    // Check every number in range
    for (int num = start; num <= end; num++) {
        if (hasIncreasingDigits(num, n)) {
            result.push_back(num);
        }
    }
    
    return result;
}


int main()
{
    int n = 1;
    vector<int> ans = increasingNumbers(n);

    cout << "[";
    for (int i = 0; i < ans.size(); i++) {
        cout << ans[i];
        if (i != ans.size() - 1)
            cout << ", ";
    }
    cout << "]" << endl;

    return 0;
}
Java
import java.util.*;

class GFG {
    static boolean hasIncreasingDigits(int num, int n)
    {
        String s = Integer.toString(num);

        // Number must have exactly n digits
        if (s.length() != n)
            return false;

        // Check each adjacent digit pair
        for (int i = 1; i < n; i++) {
            if (s.charAt(i) <= s.charAt(i - 1))
                return false; 
        }
        return true;
    }

    static ArrayList<Integer> increasingNumbers(int n)
    {
        ArrayList<Integer> result = new ArrayList<>();

        // Base Case: Empty array in case of n > 9
        if (n > 9)
            return result;

        // For n=1: start from 0, else start from 10^(n-1)
        int start = (n == 1) ? 0 : (int)Math.pow(10, n - 1);
        int end = (int)Math.pow(10, n)
                  - 1;

        // Check every number in range
        for (int num = start; num <= end; num++) {
            if (hasIncreasingDigits(num, n)) {
                result.add(num);
            }
        }

        return result;
    }

    public static void main(String[] args)
    {
        int n = 1;
        ArrayList<Integer> ans = increasingNumbers(n);
        System.out.print("[");
        for (int i = 0; i < ans.size(); i++) {
            System.out.print(ans.get(i));
            if (i != ans.size() - 1) {
                System.out.print(", ");
            }
        }
        System.out.println("]");
    }
}
Python
def hasIncreasingDigits(num, n):
    s = str(num)

    # Number must have exactly n digits
    if len(s) != n:
        return False

    # Check each adjacent digit pair
    for i in range(1, n):
        if s[i] <= s[i - 1]:
            return False
    return True


def increasingNumbers(n):
    result = []

    # Base Case: Empty array in case of n > 9
    if n > 9:
        return result

    # For n=1: start from 0, else start from 10^(n-1)
    start = 0 if n == 1 else 10 ** (n - 1)
    end = (10 ** n) - 1

    # Check every number in range
    for num in range(start, end + 1):
        if hasIncreasingDigits(num, n):
            result.append(num)

    return result


if __name__ == "__main__":
    n = 1
    ans = increasingNumbers(n)

    print("[", end="")
    for i in range(len(ans)):
      print(ans[i], end="")
      if i != len(ans) - 1:
        print(", ", end="")
    print("]")
C#
using System;
using System.Collections.Generic;

class GFG {

    static bool hasIncreasingDigits(int num, int n)
    {
        string s = num.ToString();

        // Number must have exactly n digits
        if (s.Length != n)
            return false;

        // Check each adjacent digit pair
        for (int i = 1; i < n; i++) {
            if (s[i] <= s[i - 1])
                return false;
        }
        return true;
    }

    static List<int> increasingNumbers(int n)
    {
        List<int> result = new List<int>();

        // Base Case: Empty array in case of n > 9
        if (n > 9)
            return result;

        // For n=1: start from 0, else start from 10^(n-1)
        int start = (n == 1) ? 0 : (int)Math.Pow(10, n - 1);
        int end = (int)Math.Pow(10, n)
                  - 1;

        // Check every number in range
        for (int num = start; num <= end; num++) {
            if (hasIncreasingDigits(num, n)) {
                result.Add(num);
            }
        }

        return result;
    }

    static void Main(string[] args)
    {
        int n = 1;
        List<int> ans = increasingNumbers(n);

        Console.Write("[");
        for (int i = 0; i < ans.Count; i++) {
            Console.Write(ans[i]);
            if (i != ans.Count - 1) {
                Console.Write(", ");
            }
        }
        Console.WriteLine("]");
    }
}
JavaScript
function hasIncreasingDigits(num, n) {
    let s = num.toString();

    // Number must have exactly n digits
    if (s.length !== n) return false;

    // Check each adjacent digit pair
    for (let i = 1; i < n; i++) {
        if (s[i] <= s[i - 1]) return false;
    }
    return true;
}

function increasingNumbers(n) {
    let result = [];

    // Base Case: Empty array in case of n > 9
    if (n > 9) return result;

    // For n=1: start from 0, else start from 10^(n-1)
    let start = (n === 1) ? 0 : Math.pow(10, n - 1);
    let end = Math.pow(10, n) - 1;

    // Check every number in range
    for (let num = start; num <= end; num++) {
        if (hasIncreasingDigits(num, n)) {
            result.push(num);
        }
    }

    return result;
}

// Driver code
let n = 1;
let ans = increasingNumbers(n);

console.log("[" + ans.join(", ") + "]");

Output
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]

[Expected Approach] Generating Valid Numbers Directly – O(C(9, n)) Time and O(n) Space

The idea is to generate only those numbers whose digits are strictly increasing instead of checking every possible number. We build numbers digit by digit. At every step, only digits greater than the previously chosen digit are allowed, ensuring the increasing property automatically. Once the constructed number reaches length n, it is added to the result.

  • Start recursion using digits 1 to 9 as the first digit
  • Ensure that every newly chosen digit is greater than the previous digit so that the number remains strictly increasing.
  • Build numbers mathematically instead of using strings.
  • If the length becomes n, store the number
C++
#include <iostream>
#include <vector>
using namespace std;

void generateNumbers(int remainingDigits, int currentDigit, 
                        int currentNumber, vector<int> &result)
{

    // Required number of digits formed
    if (remainingDigits == 0)
    {
        result.push_back(currentNumber);
        return;
    }

    // Choose next digit greater than current digit
    for (int nextDigit = currentDigit + 1; nextDigit <= 9; nextDigit++)
    {
        generateNumbers(remainingDigits - 1, nextDigit, 
                            currentNumber * 10 + nextDigit, result);
    }
}

vector<int> increasingNumbers(int n)
{
    vector<int> result;

    if (n == 1)
    {
        for (int digit = 0; digit <= 9; digit++)
            result.push_back(digit);

        return result;
    }

    // No valid number exists for n > 9
    if (n > 9)
        return result;

    // First digit must start from 1
    for (int firstDigit = 1; firstDigit <= 9; firstDigit++)
    {
        generateNumbers(n - 1, firstDigit, firstDigit, result);
    }

    return result;
}

int main()
{
    int n = 1;

    vector<int> result = increasingNumbers(n);

    cout << "[";
    for (int i = 0; i < result.size(); i++)
    {
        cout << result[i];
        if (i != result.size() - 1)
            cout << ", ";
    }
    cout << "]";

    return 0;
}
Java
import java.util.ArrayList;

public class GFG {

    public void generateNumbers(int remainingDigits,int currentDigit,
                                int currentNumber, ArrayList<Integer> result)
    {

        // Required number of digits formed
        if (remainingDigits == 0) {
            result.add(currentNumber);
            return;
        }

        // Choose next digit greater than current digit
        for (int nextDigit = currentDigit + 1; nextDigit <= 9; nextDigit++) {
            generateNumbers(remainingDigits - 1, nextDigit, 
            currentNumber * 10 + nextDigit, result);
        }
    }

    public ArrayList<Integer> increasingNumbers(int n)
    {
        ArrayList<Integer> result = new ArrayList<>();

        // Special case for n = 1
        if (n == 1) {
            for (int digit = 0; digit <= 9; digit++)
                result.add(digit);

            return result;
        }

        // No valid number exists for n > 9
        if (n > 9)
            return result;

        // First digit starts from 1
        for (int firstDigit = 1; firstDigit <= 9;
             firstDigit++) {
            generateNumbers(n - 1, firstDigit, firstDigit,
                            result);
        }

        return result;
    }

    public void main(String[] args)
    {
        int n = 1;

        ArrayList<Integer> result = increasingNumbers(n);

        System.out.print("[");
        for (int i = 0; i < result.size(); i++) {
            System.out.print(result.get(i));
            if (i != result.size() - 1)
                System.out.print(", ");
        }
        System.out.println("]");
    }
}
Python
def generateNumbers(remainingDigits, currentDigit, currentNumber, result):
    # Required number of digits formed
    if remainingDigits == 0:
        result.append(currentNumber)
        return

    # Choose next digit greater than current digit
    for nextDigit in range(currentDigit + 1, 10):
        generateNumbers( remainingDigits - 1, nextDigit,
            currentNumber * 10 + nextDigit,result )


def increasingNumbers(n):
    result = []

    # Special case for n = 1
    if n == 1:
        for digit in range(10):
            result.append(digit)
        return result

    # No valid number exists for n > 9
    if n > 9:
        return result

    # First digit starts from 1
    for firstDigit in range(1, 10):
        generateNumbers(n - 1, firstDigit, firstDigit, result)

    return result


if __name__ == '__main__':
    n = 1
    result = increasingNumbers(n)

    print("[", end="")
    for i in range(len(result)):
        print(result[i], end="")
        if i != len(result) - 1:
            print(", ", end="")
    print("]")
C#
using System;
using System.Collections.Generic;

public class GFG {

    public static void generateNumbers(int remainingDigits,int currentDigit,
                                       int currentNumber,List<int> result) {

        if (remainingDigits == 0) {
            result.Add(currentNumber);
            return;
        }

        for (int nextDigit = currentDigit + 1; nextDigit <= 9; nextDigit++) {
            generateNumbers(
                remainingDigits - 1, nextDigit,
                currentNumber * 10 + nextDigit,result
            );
        }
    }

    public static List<int> increasingNumbers(int n) {
        List<int> result = new List<int>();

        if (n == 1) {
            for (int digit = 0; digit <= 9; digit++)
                result.Add(digit);

            return result;
        }

        if (n > 9)
            return result;

        for (int firstDigit = 1; firstDigit <= 9; firstDigit++) {
            generateNumbers(n - 1, firstDigit, firstDigit, result);
        }

        return result;
    }

    public static void Main() {
        int n = 1;
        List<int> result = increasingNumbers(n);

        Console.Write("[");
        for (int i = 0; i < result.Count; i++) {
            Console.Write(result[i]);
            if (i != result.Count - 1)
                Console.Write(", ");
        }
        Console.WriteLine("]");
    }
}
JavaScript
function generateNumbers(remainingDigits, currentDigit, currentNumber, result) {
    
    // Required number of digits formed
    if (remainingDigits === 0) {
        result.push(currentNumber);
        return;
    }

    // Choose next digit greater than current digit
    for (let nextDigit = currentDigit + 1; nextDigit <= 9; nextDigit++) {
        generateNumbers(
            remainingDigits - 1, nextDigit,
            currentNumber * 10 + nextDigit,result
        );
    }
}

function increasingNumbers(n) {
    let result = [];

    // Special case for n = 1
    if (n === 1) {
        for (let digit = 0; digit <= 9; digit++)
            result.push(digit);

        return result;
    }

    // No valid number exists for n > 9
    if (n > 9)
        return result;

    // First digit starts from 1
    for (let firstDigit = 1; firstDigit <= 9; firstDigit++) {
        generateNumbers(n - 1, firstDigit, firstDigit, result);
    }

    return result;
}

// Driver code
let n = 1;
let result = increasingNumbers(n);

console.log("[" + result.join(", ") + "]");

Output
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
Comment