Print all the permutation of length L using the elements of an array | Iterative
Given an array of unique elements, we have to find all the permutation of length L using the elements of the array. Repetition of elements is allowed.
Examples:
Input: arr = { 1, 2 }, L=3
Output:
111
211
121
221
112
212
122
222
Input: arr = { 1, 2, 3 }, L=2
Output:
11
21
31
12
22
32
13
23
33
Approach:
- To form a sequence of length L with N number of elements, it is known that the i-th element of the sequence can be filled in N ways. So there will be
sequences - We will run a loop from 0 to
, for every i we will convert i from base 10 to base N. The digits of the converted number will represent the indices of the array - We can print all the
sequences by this way.
Below is the implementation of the approach:
C++
// C++ implementation #include <bits/stdc++.h> using namespace std; // Convert the number to Lth // base and print the sequence void convert_To_Len_th_base(int n, int arr[], int len, int L) { // Sequence is of length L for (int i = 0; i < L; i++) { // Print the ith element // of sequence cout << arr[n % len]; n /= len; } cout << endl; } // Print all the permuataions void print(int arr[], int len, int L) { // There can be (len)^l // permutations for (int i = 0; i < (int)pow(len, L); i++) { // Convert i to len th base convert_To_Len_th_base(i, arr, len, L); } } // Driver code int main() { int arr[] = { 1, 2, 3 }; int len = sizeof(arr) / sizeof(arr[0]); int L = 2; // function call print(arr, len, L); return 0; } |
chevron_right
filter_none
Java
// Java implementation for above approach import java.io.*; class GFG { // Convert the number to Lth // base and print the sequence static void convert_To_Len_th_base(int n, int arr[], int len, int L) { // Sequence is of length L for (int i = 0; i < L; i++) { // Print the ith element // of sequence System.out.print(arr[n % len]); n /= len; } System.out.println(); } // Print all the permuataions static void print(int arr[], int len, int L) { // There can be (len)^l // permutations for (int i = 0; i < (int)Math.pow(len, L); i++) { // Convert i to len th base convert_To_Len_th_base(i, arr, len, L); } } // Driver code public static void main (String[] args) { int arr[] = { 1, 2, 3 }; int len = arr.length; int L = 2; // function call print(arr, len, L); } } // This code is contributed by ajit. |
chevron_right
filter_none
Python3
# Python3 implementation for the above approach # Convert the number to Lth # base and print the sequence def convert_To_Len_th_base(n, arr, Len, L): # Sequence is of Length L for i in range(L): # Print the ith element # of sequence print(arr[n % Len], end = "") n //= Len print() # Print all the permuataions def printf(arr, Len, L): # There can be (Len)^l permutations for i in range(pow(Len, L)): # Convert i to Len th base convert_To_Len_th_base(i, arr, Len, L) # Driver code arr = [1, 2, 3] Len = len(arr) L = 2 # function call printf(arr, Len, L) # This code is contributed by Mohit Kumar |
chevron_right
filter_none
C#
// C# implementation for above approach using System; class GFG { // Convert the number to Lth // base and print the sequence static void convert_To_Len_th_base(int n, int []arr, int len, int L) { // Sequence is of length L for (int i = 0; i < L; i++) { // Print the ith element // of sequence Console.Write(arr[n % len]); n /= len; } Console.WriteLine(); } // Print all the permuataions static void print(int []arr, int len, int L) { // There can be (len)^l // permutations for (int i = 0; i < (int)Math.Pow(len, L); i++) { // Convert i to len th base convert_To_Len_th_base(i, arr, len, L); } } // Driver code public static void Main (String[] args) { int []arr = { 1, 2, 3 }; int len = arr.Length; int L = 2; // function call print(arr, len, L); } } // This code is contributed by Rajput-Ji |
chevron_right
filter_none
Output:
11 21 31 12 22 32 13 23 33
Recommended Posts:
- Iterative approach to print all combinations of an Array
- Iterative approach to print all permutations of an Array
- Count arrays of length K whose product of elements is same as that of given array
- Print all possible combinations of r elements in a given array of size n
- Lexicographically Smallest Permutation of length N such that for exactly K indices, a[i] > a[i] + 1
- Find all even length binary sequences with same sum of first and second half bits | Iterative
- Print all subsequences of a string | Iterative Method
- Count number of permutation of an Array having no SubArray of size two or more from original Array
- Lexicographically smallest permutation with distinct elements using minimum replacements
- Generate a random permutation of elements from range [L, R] (Divide and Conquer)
- Permutation of an array that has smaller values from another array
- Find permutation array from the cumulative sum array
- Minimum length of string having all permutation of given string.
- Find the permutation p from the array q such that q[i] = p[i+1] - p[i]
- Print all sequences of given length
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 Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.



