Generate k digit numbers with digits in strictly increasing order

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 characters

1. 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 above idea :

C++

filter_none

edit
close

play_arrow

link
brightness_4
code

// 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 digits
void 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 code
int main()
{
    int k = 3;
    generateWellOrdered(k);
    return 0;
}

chevron_right


Java

filter_none

edit
close

play_arrow

link
brightness_4
code

// 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);
    }
}

chevron_right


Python 3

filter_none

edit
close

play_arrow

link
brightness_4
code

# 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 digits
def 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 code
if __name__ == "__main__":
      
    k = 3
    generateWellOrdered(k)
  
# This code is contributed by Ita_c

chevron_right


C#

filter_none

edit
close

play_arrow

link
brightness_4
code

// 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

chevron_right


PHP

filter_none

edit
close

play_arrow

link
brightness_4
code

<?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 digits
function 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 k
function generateWellOrdered($k)
{
    printWellOrdered(0, 0, $k);
}
  
// Driver code
$k = 3;
generateWellOrdered($k);
  
// This code is contributed by ajit.
?>

chevron_right



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.



My Personal Notes arrow_drop_up

Improved By : nitin mittal, jit_t, Ita_c



Article Tags :
Practice Tags :


Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.