Generate k digit numbers with digits in strictly increasing order - GeeksforGeeks

26 Apr, 2021
Given a value k, generate all well-ordered numbers of length k. Well-ordered means that dig­its should be in increas­ing.Examples :  
Input : K = 7
Output :
1234567 1234568 1234569 1234578 1234579
1234589 1234678 1234679 1234689 1234789 
1235678 1235679 1235689 1235789 1236789 
1245678 1245679 1245689 1245789 1246789 
1256789 1345678 1345679 1345689 1345789 
1346789 1356789 1456789 2345678 2345679 
2345689 2345789 2346789 2356789 2456789 
3456789

Input  : K = 3
Output :
123 124 125 126 127 128 129 134 135 136 
137 138 139 145 146 147 148 149 156 157 
158 159 167 168 169 178 179 189 234 235 
236 237 238 239 245 246 247 248 249 256 
257 258 259 267 268 269 278 279 289 345 
346 347 348 349 356 357 358 359 367 368 
369 378 379 389 456 457 458 459 467 468 
469 478 479 489 567 568 569 578 579 589 
678 679 689 789 
 
This prob­lem is quite sim­i­lar to print all possible strings of length k that can be formed from a set of n characters1. Loop through i = x to 9. (all dig­its will be from 1 to 9). 2. x will start with 0 and will be incre­mented with every recur­sive call to make sure that the numbers are well-formed. 3. With every recur­sive call, mul­ti­ply the result (which will start with 0) by 10 and add i and make k = k-1.Below is the implementation of the above idea : 
C++
Java
Python 3
C#
PHP
Javascript
// C++ program to generate well// ordered numbers with k digits.#include<bits/stdc++.h>using namespace std;  // number --> Current value of number.// x --> Current digit to be considered// k --> Remaining number of digitsvoid printWellOrdered(int number,                       int x, int k){    if (k == 0)    {        cout << number << " ";        return;    }      // Try all possible greater digits    for (int i = (x + 1); i < 10; i++)        printWellOrdered(number * 10 +                          i, i, k - 1);}  // Generates all well ordered // numbers of length k.void generateWellOrdered(int k){    printWellOrdered(0, 0, k);}  // Driver codeint main(){    int k = 3;    generateWellOrdered(k);    return 0;}
// Java program to generate well // ordered numbers with k digits.  class Generate{    // number --> Current value of number.    // x --> Current digit to be considered    // k --> Remaining number of digits    static void printWellOrdered(int number,                                  int x, int k)    {        if (k == 0)        {            System.out.print(number+" ");            return;        }              // Try all possible greater digits        for (int i = (x + 1); i < 10; i++)            printWellOrdered(number * 10 +                              i, i, k - 1);    }          // Generates all well ordered     // numbers of length k.    static void generateWellOrdered(int k)    {        printWellOrdered(0, 0, k);    }          // Driver Code     public static void main (String[] args)     {        int k = 3;        generateWellOrdered(k);    }}
# Python 3 program to generate well# ordered numbers with k digits.  # number --> Current value of number.# x --> Current digit to be considered# k --> Remaining number of digitsdef printWellOrdered(number, x, k):      if (k == 0):        print(number, end = " ")        return      # Try all possible greater digits    for i in range( (x + 1), 10):        printWellOrdered(number * 10 + i,                                i, k - 1)  # Generates all well ordered # numbers of length k.def generateWellOrdered(k):    printWellOrdered(0, 0, k)  # Driver codeif __name__ == "__main__":          k = 3    generateWellOrdered(k)  # This code is contributed by Ita_c
// C# program to generate well// ordered numbers with k digits.using System;  class GFG {          // number --> Current value of number.    // x --> Current digit to be considered    // k --> Remaining number of digits    static void printWellOrdered(int number,                                  int x, int k)    {                  if (k == 0)        {            Console.Write(number + " ");            return;        }              // Try all possible greater digits        for (int i = (x + 1); i < 10; i++)            printWellOrdered(number * 10 +                              i, i, k - 1);    }          // Generates all well ordered     // numbers of length k.    static void generateWellOrdered(int k)    {        printWellOrdered(0, 0, k);    }          // Driver Code    public static void Main ()     {        int k = 3;        generateWellOrdered(k);    }}  // This code is contributed by nitin mittal
<?php// PHP program to generate well // ordered numbers with k digits.  // number --> Current value of number.// x --> Current digit to be considered// k --> Remaining number of digitsfunction printWellOrdered($number, $x, $k){    if ($k == 0)    {        echo $number, " ";        return;    }      // Try all possible greater digits    for ($i = ($x + 1); $i < 10; $i++)        printWellOrdered($number * 10 +                          $i, $i, $k - 1);}  // Generates all well ordered // numbers of length kfunction generateWellOrdered($k){    printWellOrdered(0, 0, $k);}  // Driver code$k = 3;generateWellOrdered($k);  // This code is contributed by ajit.?>
<script>  // Javascript program to generate well // ordered numbers with k digits.          // number --> Current value of number.    // x --> Current digit to be considered    // k --> Remaining number of digits    function printWellOrdered(number,x,k)    {        if (k == 0)        {            document.write(number+" ");            return;        }                // Try all possible greater digits        for (let i = (x + 1); i < 10; i++)            printWellOrdered(number * 10 +                              i, i, k - 1);    }          // Generates all well ordered     // numbers of length k.    function generateWellOrdered(k)    {        printWellOrdered(0, 0, k);    }          // Driver Code     let k = 3;    generateWellOrdered(k);                // This code is contributed by rag2127      </script>
Output : 
123 124 125 126 127 128 129 134 135 136 137 
138 139 145 146 147 148 149 156 157 158 159 
167 168 169 178 179 189 234 235 236 237 238 
239 245 246 247 248 249 256 257 258 259 267 
268 269 278 279 289 345 346 347 348 349 356 
357 358 359 367 368 369 378 379 389 456 457 
458 459 467 468 469 478 479 489 567 568 569 
578 579 589 678 679 689 789 
This article is contributed by Rakesh Kumar. 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. 
nitin mittal
jit_t
ukasp
rag2127
number-digits
Mathematical
Mathematical
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Comments
Old Comments
Find all factors of a natural number | Set 1
Check if a number is Palindrome
Program to print prime numbers from 1 to N.
Program to add two binary strings
Program to multiply two matrices
Fizz Buzz Implementation
Find pair with maximum GCD in an array
Find Union and Intersection of two unsorted arrays
Count all possible paths from top left to bottom right of a mXn matrix
Count ways to reach the n'th stair