Print all sequences of given length

11 Oct, 2021
Given two integers k and n, write a function that prints all the sequences of length k composed of numbers 1,2..n. You need to print these sequences in sorted order.Examples:  
Input: k = 2, n = 3

Output: 
1 1
1 2
1 3
2 1
2 2
2 3
3 1
3 2
3 3
 
Input:  k = 3, n = 4

Output: 
1 1 1
1 1 2
1 1 3
1 1 4
1 2 1
.....
.....
4 3 4
4 4 1
4 4 2
4 4 3
4 4 4
Method 1 (Simple and Iterative): The simple idea to print all sequences in sorted order is to start from {1 1 ... 1} and keep incrementing the sequence while the sequence doesn’t become {n n ... n}. Following is the detailed process.1) Create an output array arr[] of size k. Initialize the array as {1, 1...1}. 2) Print the array arr[]. 3) Update the array arr[] so that it becomes immediate successor (to be printed) of itself. For example, immediate successor of {1, 1, 1} is {1, 1, 2}, immediate successor of {1, 4, 4} is {2, 1, 1} and immediate successor of {4, 4, 3} is {4 4 4}. 4) Repeat steps 2 and 3 while there is a successor array. In other words, while the output array arr[] doesn’t become {n, n .. n}Now let us talk about how to modify the array so that it contains immediate successor. By definition, the immediate successor should have the same first p terms and larger (p+l)th term. In the original array, (p+1)th term (or arr[p]) is smaller than n and all terms after arr[p] i.e., arr[p+1], arr[p+2], ... arr[k-1] are n. To find the immediate successor, we find the point p in the previously printed array. To find point p, we start from rightmost side and keep moving till we find a number arr[p] such that arr[p] is smaller than n (or not n). Once we find such a point, we increment it arr[p] by 1 and make all the elements after arr[p] as 1 i.e., we do arr[p+1] = 1, arr[p+2] = 1 .. arr[k-1] = 1. Following are the detailed steps to get immediate successor of arr[]1) Start from the rightmost term arr[k-1] and move toward left. Find the first element arr[p] that is not same as n. 2) Increment arr[p] by 1 3) Starting from arr[p+1] to arr[k-1], set the value of all terms as 1. 
C++
C
Java
Python3
C#
Javascript
// CPP program of above approach#include<iostream>using namespace std;/* A utility function that prints a given arr[] of length size*/void printArray(int arr[], int size){    for(int i = 0; i < size; i++)        cout <<" "<< arr[i];    cout <<"\n";    return;} /* This function returns 0 if there are no more sequences to be printed, otherwise   modifies arr[] so that arr[] contains next sequence to be printed */int getSuccessor(int arr[], int k, int n){    /* start from the rightmost side and find the first number less than n */    int p = k - 1;    while (arr[p] == n)        p--;     /* If all numbers are n in the array then there is no successor, return 0 */    if (p < 0)        return 0;     /* Update arr[] so that it contains successor */    arr[p] = arr[p] + 1;    for(int i = p + 1; i < k; i++)        arr[i] = 1;     return 1;} /* The main function that prints all sequences from 1, 1, ..1 to n, n, ..n */void printSequences(int n, int k){    int *arr = new int[k];     /* Initialize the current sequence as the first sequence to be printed */    for(int i = 0; i < k; i++)        arr[i] = 1;     /* The loop breaks when there are no more successors to be printed */    while(1)    {        /* Print the current sequence */        printArray(arr, k);         /* Update arr[] so that it contains next sequence to be printed. And if           there are no more sequences then break the loop */        if(getSuccessor(arr, k, n) == 0)          break;    }     delete(arr); // free dynamically allocated array    return;} /* Driver Program to test above functions */int main(){    int n = 3;    int k = 2;    printSequences(n, k);    return 0;} // this code is contributed by shivanisinghss2110
// CPP program of above approach#include<stdio.h> /* A utility function that prints a given arr[] of length size*/void printArray(int arr[], int size){    for(int i = 0; i < size; i++)        printf("%d ", arr[i]);    printf("\n");    return;} /* This function returns 0 if there are no more sequences to be printed, otherwise   modifies arr[] so that arr[] contains next sequence to be printed */int getSuccessor(int arr[], int k, int n){    /* start from the rightmost side and find the first number less than n */    int p = k - 1;    while (arr[p] == n)        p--;     /* If all numbers are n in the array then there is no successor, return 0 */    if (p < 0)        return 0;     /* Update arr[] so that it contains successor */    arr[p] = arr[p] + 1;    for(int i = p + 1; i < k; i++)        arr[i] = 1;     return 1;} /* The main function that prints all sequences from 1, 1, ..1 to n, n, ..n */void printSequences(int n, int k){    int *arr = new int[k];     /* Initialize the current sequence as the first sequence to be printed */    for(int i = 0; i < k; i++)        arr[i] = 1;     /* The loop breaks when there are no more successors to be printed */    while(1)    {        /* Print the current sequence */        printArray(arr, k);         /* Update arr[] so that it contains next sequence to be printed. And if           there are no more sequences then break the loop */        if(getSuccessor(arr, k, n) == 0)          break;    }     delete(arr); // free dynamically allocated array    return;} /* Driver Program to test above functions */int main(){    int n = 3;    int k = 2;    printSequences(n, k);    return 0;}
// JAVA program of above approach class GFG{     /* A utility function that prints a given arr[] of length size*/    static void printArray(int arr[], int size)    {        for (int i = 0; i < size; i++)        {            System.out.printf("%d ", arr[i]);        }        System.out.printf("\n");        return;    }     /* This function returns 0 if there are    no more sequences to be printed, otherwise    modifies arr[] so that arr[] contains    next sequence to be printed */    static int getSuccessor(int arr[], int k, int n)    {        /* start from the rightmost side and        find the first number less than n */        int p = k - 1;        while (arr[p] == n)        {            p--;            if (p < 0)            {                break;            }        }         /* If all numbers are n in the array        then there is no successor, return 0 */        if (p < 0)        {            return 0;        }         /* Update arr[] so that it contains successor */        arr[p] = arr[p] + 1;        for (int i = p + 1; i < k; i++)        {            arr[i] = 1;        }        return 1;    }     /* The main function that prints all    sequences from 1, 1, ..1 to n, n, ..n */    static void printSequences(int n, int k)    {        int[] arr = new int[k];         /* Initialize the current sequence as        the first sequence to be printed */        for (int i = 0; i < k; i++)        {            arr[i] = 1;        }         /* The loop breaks when there are        no more successors to be printed */        while (true)        {            /* Print the current sequence */            printArray(arr, k);             /* Update arr[] so that it contains            next sequence to be printed. And if            there are no more sequences then            break the loop */            if (getSuccessor(arr, k, n) == 0)            {                break;            }        }    }     /* Driver code */    public static void main(String[] args)    {        int n = 3;        int k = 2;        printSequences(n, k);    }} // This code is contributed by PrinciRaj1992
# Python3 program of above approach # A utility function that prints# a given arr[] of length size#def printArray(arr, size):    for i in range(size):        print(arr[i], end = " ")    print()    return # This function returns 0 if there are# no more sequences to be printed, otherwise# modifies arr[] so that arr[] contains# next sequence to be printed #def getSuccessor(arr, k, n):         # start from the rightmost side and    # find the first number less than n    p = k - 1    while (arr[p] == n and 0 <= p < k):        p -= 1             # If all numbers are n in the array    # then there is no successor, return 0    if (p < 0):        return 0             # Update arr[] so that it contains successor    arr[p] = arr[p] + 1    i = p + 1    while(i < k):        arr[i] = 1        i += 1    return 1     # The main function that prints all sequences# from 1, 1, ..1 to n, n, ..ndef printSequences(n, k):    arr = [0] * k         # Initialize the current sequence as    # the first sequence to be printed #    for i in range(k):        arr[i] = 1             # The loop breaks when there are    # no more successors to be printed    while(1):                 # Print the current sequence        printArray(arr, k)                 # Update arr[] so that it contains        # next sequence to be printed. And if        # there are no more sequences then        # break the loop        if(getSuccessor(arr, k, n) == 0):            break    return # Driver coden = 3k = 2printSequences(n, k) # This code is contributed by shubhamsingh10
// C# program of above approachusing System; class GFG{     /* A utility function that prints    a given []arr of length size*/    static void printArray(int []arr, int size)    {        for (int i = 0; i < size; i++)        {            Console.Write("{0} ", arr[i]);        }        Console.Write("\n");        return;    }     /* This function returns 0 if there are    no more sequences to be printed, otherwise    modifies []arr so that []arr contains    next sequence to be printed */    static int getSuccessor(int []arr, int k, int n)    {        /* start from the rightmost side and        find the first number less than n */        int p = k - 1;        while (arr[p] == n)        {            p--;            if (p < 0)            {                break;            }        }         /* If all numbers are n in the array        then there is no successor, return 0 */        if (p < 0)        {            return 0;        }         /* Update []arr so that it contains successor */        arr[p] = arr[p] + 1;        for (int i = p + 1; i < k; i++)        {            arr[i] = 1;        }        return 1;    }     /* The main function that prints all    sequences from 1, 1, ..1 to n, n, ..n */    static void printSequences(int n, int k)    {        int[] arr = new int[k];         /* Initialize the current sequence as        the first sequence to be printed */        for (int i = 0; i < k; i++)        {            arr[i] = 1;        }         /* The loop breaks when there are        no more successors to be printed */        while (true)        {            /* Print the current sequence */            printArray(arr, k);             /* Update []arr so that it contains            next sequence to be printed. And if            there are no more sequences then            break the loop */            if (getSuccessor(arr, k, n) == 0)            {                break;            }        }    }     /* Driver code */    public static void Main(String[] args)    {        int n = 3;        int k = 2;        printSequences(n, k);    }} // This code is contributed by Rajput-Ji
<script> // javascript program of above approach /* A utility function that prints agiven arr of length size*/function printArray(arr , size){    for (i = 0; i < size; i++)    {        document.write(arr[i]+" ");    }    document.write("<br>");    return;} /* This function returns 0 if there areno more sequences to be printed, otherwisemodifies arr so that arr containsnext sequence to be printed */function getSuccessor(arr , k , n){    /* start from the rightmost side and    find the first number less than n */    var p = k - 1;    while (arr[p] == n)    {        p--;        if (p < 0)        {            break;        }    }     /* If all numbers are n in the array    then there is no successor, return 0 */    if (p < 0)    {        return 0;    }     /* Update arr so that it contains successor */    arr[p] = arr[p] + 1;    for (i = p + 1; i < k; i++)    {        arr[i] = 1;    }    return 1;} /* The main function that prints allsequences from 1, 1, ..1 to n, n, ..n */function printSequences(n , k){    var arr = Array.from({length: k}, (_, i) => 0);     /* Initialize the current sequence as    the first sequence to be printed */    for (i = 0; i < k; i++)    {        arr[i] = 1;    }     /* The loop breaks when there are    no more successors to be printed */    while (true)    {        /* Print the current sequence */        printArray(arr, k);         /* Update arr so that it contains        next sequence to be printed. And if        there are no more sequences then        break the loop */        if (getSuccessor(arr, k, n) == 0)        {            break;        }    }} /* Driver code */var n = 3;var k = 2;printSequences(n, k); // This code is contributed by 29AjayKumar </script>
Output: 
1 1
1 2
1 3
2 1
2 2
2 3
3 1
3 2
3 3
Time Complexity: There are total n^k sequences. Printing a sequence and finding its successor take O(k) time. So time complexity of above implementation is O(k*n^k).
Auxiliary Space: O(k)Method 2 (Tricky and Recursive) The recursive function printSequencesRecur generates and prints all sequences of length k. The idea is to use one more parameter index. The function printSequencesRecur keeps all the terms in arr[] sane till index, update the value at index and recursively calls itself for more terms after index.  
C++
C
Java
Python3
C#
PHP
Javascript
// C++ program of above approach#include<iostream>using namespace std; /* A utility function that prints a given arr[] of length size*/void printArray(int arr[], int size){    for(int i = 0; i < size; i++)        cout<< "  "<< arr[i];    cout<<"\n";    return;} /* The core function that recursively generates and prints all sequences of  length k */void printSequencesRecur(int arr[], int n, int k, int index){   int i;   if (k == 0)   {     printArray(arr, index);   }   if (k > 0)   {      for(i = 1; i<=n; ++i)      {        arr[index] = i;        printSequencesRecur(arr, n, k-1, index+1);      }   }} /* A function that uses printSequencesRecur() to prints all sequences   from 1, 1, ..1 to n, n, ..n */void printSequences(int n, int k){    int *arr = new int[k];    printSequencesRecur(arr, n, k, 0);     delete(arr); // free dynamically allocated array    return;} /* Driver Program to test above functions */int main(){    int n = 3;    int k = 2;    printSequences(n, k);    return 0;} // This code is contributed by shivanisinghss2110
// C++ program of above approach#include<stdio.h> /* A utility function that prints a given arr[] of length size*/void printArray(int arr[], int size){    for(int i = 0; i < size; i++)        printf("%d ", arr[i]);    printf("\n");    return;} /* The core function that recursively generates and prints all sequences of  length k */void printSequencesRecur(int arr[], int n, int k, int index){   int i;   if (k == 0)   {     printArray(arr, index);   }   if (k > 0)   {      for(i = 1; i<=n; ++i)      {        arr[index] = i;        printSequencesRecur(arr, n, k-1, index+1);      }   }}  /* A function that uses printSequencesRecur() to prints all sequences   from 1, 1, ..1 to n, n, ..n */void printSequences(int n, int k){    int *arr = new int[k];    printSequencesRecur(arr, n, k, 0);     delete(arr); // free dynamically allocated array    return;} /* Driver Program to test above functions */int main(){    int n = 3;    int k = 2;    printSequences(n, k);    return 0;}
// Java program of above approach class GfG { /* A utility function that prints a given arr[] of length size*/static void printArray(int arr[], int size){    for(int i = 0; i < size; i++)        System.out.print(arr[i] + " ");System.out.println();    return;} /* The core function that recursively generates and prints all sequences oflength k */static void printSequencesRecur(int arr[], int n, int k, int index){int i;if (k == 0){    printArray(arr, index);}if (k > 0){    for(i = 1; i<=n; ++i)    {        arr[index] = i;        printSequencesRecur(arr, n, k-1, index+1);    }}}  /* A function that uses printSequencesRecur() to prints all sequencesfrom 1, 1, ..1 to n, n, ..n */static void printSequences(int n, int k){    int arr[] = new int[k];    printSequencesRecur(arr, n, k, 0); return ;} /* Driver Program to test above functions */public static void main(String[] args){    int n = 3;    int k = 2;    printSequences(n, k);}}
# Python3 program of above approach # A utility function that prints a# given arr[] of length sizedef printArray(arr, size):     for i in range(size):        print(arr[i], end = " ");    print("");    return; # The core function that recursively# generates and prints all sequences# of length kdef printSequencesRecur(arr, n, k, index):    if (k == 0):        printArray(arr, index);     if (k > 0):        for i in range(1, n + 1):            arr[index] = i;            printSequencesRecur(arr, n, k - 1,                                    index + 1); # A function that uses printSequencesRecur() to# prints all sequences from 1, 1, ..1 to n, n, ..ndef printSequences(n, k):    arr = [0] * n;    printSequencesRecur(arr, n, k, 0);     return; # Driver Coden = 3;k = 2;printSequences(n, k); # This code is contributed mits
// C# program of above approachusing System; class GFG{ /* A utility function that prints a givenarr[] of length size*/static void printArray(int []arr, int size){    for(int i = 0; i < size; i++)        Console.Write(arr[i] + " ");    Console.WriteLine();    return;} /* The core function that recursively generatesand prints all sequences of length k */static void printSequencesRecur(int []arr, int n,                                int k, int index){    int i;    if (k == 0)    {        printArray(arr, index);    }    if (k > 0)    {        for(i = 1; i<=n; ++i)        {            arr[index] = i;            printSequencesRecur(arr, n, k - 1, index + 1);        }    }}  /* A function that uses printSequencesRecur() toprints all sequences from 1, 1, ..1 to n, n, ..n */static void printSequences(int n, int k){    int[] arr = new int[k];    printSequencesRecur(arr, n, k, 0);     return;} // Driver Codepublic static void Main(){    int n = 3;    int k = 2;    printSequences(n, k);}} // This code is contributed// by Akanksha Rai
<?php// PHP program of above approach /* A utility function that prints agiven arr[] of length size*/function printArray($arr, $size){    for($i = 0; $i < $size; $i++)        echo $arr[$i] . " ";    echo "\n";    return;} /* The core function that recursively generatesand prints all sequences of length k */function printSequencesRecur($arr, $n, $k, $index){    if ($k == 0)    {        printArray($arr, $index);    }    if ($k > 0)    {        for($i = 1; $i <= $n; ++$i)        {            $arr[$index] = $i;            printSequencesRecur($arr, $n,                                $k - 1, $index + 1);        }    }}  /* A function that uses printSequencesRecur() toprints all sequences from 1, 1, ..1 to n, n, ..n */function printSequences($n, $k){    $arr = array();    printSequencesRecur($arr, $n, $k, 0);     return;} // Driver Code$n = 3;$k = 2;printSequences($n, $k); // This code is contributed// by Akanksha Rai?>
<script>// javascript program of above approach /* A utility function that prints a given arr of length size*/function printArray(arr, size){    for(i = 0; i < size; i++)        document.write(arr[i] + " ");    document.write("<br>");    return;} /* The core function that recursivelygenerates and prints all sequences oflength k */function printSequencesRecur(arr , n , k , index){    var i;    if (k == 0)    {        printArray(arr, index);    }    if (k > 0)    {        for(i = 1; i <= n; ++i)        {            arr[index] = i;            printSequencesRecur(arr, n, k - 1, index+1);        }    }} /* A function that uses printSequencesRecur()to prints all sequences from 1, 1, ..1 to n, n, ..n */function printSequences(n, k){    var arr = Array.from({length: k}, (_, i) => 0);    printSequencesRecur(arr, n, k, 0); return ;} /* Driver Program to test above functions */var n = 3;var k = 2;printSequences(n, k); // This code is contributed by Amit Katiyar.</script>
Output:
1 1
1 2
1 3
2 1
2 2
2 3
3 1
3 2
3 3
Time Complexity: There are n^k sequences and printing a sequence takes O(k) time. So time complexity is O(k*n^k).
Auxiliary Space: O(k)Thanks to mopurizwarriors for suggesting above method. As suggested by alphayoung, we can avoid use of arrays for small sequences that can fit in an integer. Following is the implementation for same.  
C++
C
Java
Python3
C#
PHP
Javascript
// C++ program of above approach /* The core function that generates andprints all sequences of length k */void printSeqRecur(int num, int pos, int k, int n){    if (pos == k)    {        cout << num << endl;        return;    }    for (int i = 1; i <= n; i++)    {        printSeqRecur(num * 10 + i, pos + 1, k, n);    }} /* A function that uses printSequencesRecur()to prints all sequencesfrom 1, 1, ..1 to n, n, ..n */void printSequences(int k, int n){    printSeqRecur(0, 0, k, n);} // This code is contributed by SHUBHAMSINGH10
// C program of above approach/* The core function that generates and prints all sequences of length k */void printSeqRecur(int num, int pos, int k, int n){    if (pos == k) {        printf("%d \n", num);        return;    }    for (int i = 1; i <= n; i++) {        printSeqRecur(num * 10 + i, pos + 1, k, n);    }} /* A function that uses printSequencesRecur() to prints all sequences   from 1, 1, ..1 to n, n, ..n */void printSequences(int k, int n){    printSeqRecur(0, 0, k, n);}
// Java program of above approach /* The core function that generates and prints all sequences of length k */static void printSeqRecur(int num, int pos, int k, int n){    if (pos == k) {        System.out.print(num + " ");        return;    }    for (int i = 1; i <= n; i++) {        printSeqRecur(num * 10 + i, pos + 1, k, n);    }} /* A function that uses printSequencesRecur() to prints all sequencesfrom 1, 1, ..1 to n, n, ..n */static void printSequences(int k, int n){    printSeqRecur(0, 0, k, n);}
# Python program of above approach# We have used number instead of# arrays to prevent linear time# required to print each stringdef printSeqRecur ( num , n, k ):    if n == 0: # if total digits become equal                # to n, print the number and return        print(num )        return             for _ in range(1, k + 1):        printSeqRecur (num * 10 + _, n - 1, k) # Driver Codeif __name__ == "__main__":    k = 3 # length of k-ary string    n = 2 # string can take values          # from 1,2,3...n    printSeqRecur(0, n, k) # This code is contributed# by shivam purohit
// C# program of above approach /* The core function that generatesand prints all sequences of length k */static void printSeqRecur(int num, int pos,                            int k, int n){    if (pos == k)    {        Console.Write(num + " ");        return;    }    for (int i = 1; i <= n; i++)    {        printSeqRecur(num * 10 + i, pos + 1, k, n);    }} /* A function that uses printSequencesRecur()to prints all sequences from 1, 1, ..1 to n, n, ..n */static void printSequences(int k, int n){    printSeqRecur(0, 0, k, n);} // This code is contributed by Code_Mech
<?php// PHP program of above approach// We have used number instead of// arrays to prevent linear time// required to print each stringfunction printSeqRecur ($num ,$n, $k){    if ($n == 0)    {        # if total digits become equal        # to n, print the number and return        print($num . "\n");        return;    }             for ($i = 1; $i < $k + 1; $i++)        printSeqRecur($num * 10 +                      $i, $n - 1, $k);} // Driver Code$k = 3; // length of k-ary string$n = 2; // string can take values        // from 1,2,3...nprintSeqRecur(0, $n, $k); // This code is contributed mits?>
<script> // Javascript program of above approach /* The core function that generates andprints all sequences of length k */function printSeqRecur( num,  pos,  k,  n){    if (pos == k)    {       document.write( num + "<br>" );        return;    }    for (var i = 1; i <= n; i++)    {        printSeqRecur(num * 10 + i, pos + 1, k, n);    }} /* A function that uses printSequencesRecur()to prints all sequencesfrom 1, 1, ..1 to n, n, ..n */function printSequences( k,  n){    printSeqRecur(0, 0, k, n);} // This code is contributed by SoumikMondal</script>
Time Complexity: O(k*n^k)
Auxiliary Space: O(n * k)
References: Algorithms and Programming: Problems and Solutions by Alexander ShenPlease write comments if you find anything incorrect, or you want to share more information about the topic discussed above. 
Shivam Purohit 2
prerna saini
Mithun Kumar
Akanksha_Rai
Code_Mech
princiraj1992
Rajput-Ji
SHUBHAMSINGH10
amit143katiyar
29AjayKumar
SoumikMondal
subham348
subhammahato348
shivanisinghss2110
subsequence
Mathematical
Mathematical
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Merge two sorted arrays
Operators in C / C++
Sieve of Eratosthenes
Prime Numbers
Program to find GCD or HCF of two numbers
Find minimum number of coins that make a given value
Minimum number of jumps to reach end
Algorithm to solve Rubik's Cube
The Knight's tour problem | Backtracking-1
Program for Decimal to Binary Conversion