Skip to content
Related Articles
Print all sequences of given length
• Difficulty Level : Medium
• Last Updated : 11 May, 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++

 `// CPP program of above approach``#include` `/* 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

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

 `# 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, ..n``def` `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 code``n ``=` `3``k ``=` `2``printSequences(n, k)` `# This code is contributed by shubhamsingh10`

## C#

 `// C# program of above approach``using` `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`

## Javascript

 ``

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).
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++ program of above approach``#include` `/* 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

 `// 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 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() to prints 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 Program to test above functions */``public` `static` `void` `main(String[] args)``{``    ``int` `n = ``3``;``    ``int` `k = ``2``;``    ``printSequences(n, k);``}``}`

## Python3

 `# 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``;` `# The core function that recursively``# generates and prints all sequences``# of length k``def` `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, ..n``def` `printSequences(n, k):``    ``arr ``=` `[``0``] ``*` `n;``    ``printSequencesRecur(arr, n, k, ``0``);` `    ``return``;` `# Driver Code``n ``=` `3``;``k ``=` `2``;``printSequences(n, k);` `# This code is contributed mits`

## C#

 `// C# program of above approach``using` `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(arr[i] + ``" "``);``    ``Console.WriteLine();``    ``return``;``}` `/* The core function that recursively generates``and 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() to``prints 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 Code``public` `static` `void` `Main()``{``    ``int` `n = 3;``    ``int` `k = 2;``    ``printSequences(n, k);``}``}` `// This code is contributed``// by Akanksha Rai`

## PHP

 ` 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``)``{``    ``\$arr` `= ``array``();``    ``printSequencesRecur(``\$arr``, ``\$n``, ``\$k``, 0);` `    ``return``;``}` `// Driver Code``\$n` `= 3;``\$k` `= 2;``printSequences(``\$n``, ``\$k``);` `// This code is contributed``// by Akanksha Rai``?>`

## Javascript

 ``

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).
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++ 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)``    ``{``        ``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 sequences``from 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

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

 `// 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 sequences``from 1, 1, ..1 to n, n, ..n */``static` `void` `printSequences(``int` `k, ``int` `n)``{``    ``printSeqRecur(``0``, ``0``, k, n);``}`

## Python3

 `# Python program of above approach``# We have used number instead of``# arrays to prevent linear time``# required to print each string``def` `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 Code``if` `__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#

 `// C# 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)``    ``{``        ``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

 ``

## Javascript

 ``

References:
Algorithms and Programming: Problems and Solutions by Alexander Shen
Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.  To complete your preparation from learning a language to DS Algo and many more,  please refer Complete Interview Preparation Course.

In case you wish to attend live classes with industry experts, please refer Geeks Classes Live

My Personal Notes arrow_drop_up