Print all the permutation of length L using the elements of an array | Iterative - GeeksforGeeks

29 Apr, 2021
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 222Input: 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++
Java
Python3
C#
Javascript
// C++ implementation#include <bits/stdc++.h>using namespace std; // Convert the number to Lth// base and print the sequencevoid 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 permuataionsvoid 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 codeint main(){    int arr[] = { 1, 2, 3 };    int len = sizeof(arr) / sizeof(arr[0]);    int L = 2;     // function call    print(arr, len, L);     return 0;}
// Java implementation for above approachimport java.io.*; class GFG{     // Convert the number to Lth// base and print the sequencestatic 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 permuataionsstatic 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 codepublic 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.
# Python3 implementation for the above approach # Convert the number to Lth# base and print the sequencedef 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 permuataionsdef 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 codearr = [1, 2, 3]Len = len(arr)L = 2 # function callprintf(arr, Len, L) # This code is contributed by Mohit Kumar
// C# implementation for above approachusing System; class GFG{     // Convert the number to Lth// base and print the sequencestatic 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 permuataionsstatic 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 codepublic 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
<script>// Javascript implementation // Convert the number to Lth// base and print the sequencefunction convert_To_Len_th_base( n, arr, len, L){    // Sequence is of length L    for ( i = 0; i < L; i++) {        // Print the ith element        // of sequence        document.write(parseInt(arr[n % len]));        n = parseInt(n / len);    }   document.write("<br>");} // Print all the permuataionsfunction print( arr, len, L){    // There can be (len)^l    // permutations    for (var i = 0; i < parseInt(Math.pow(len, L)); i++) {        // Convert i to len th base        convert_To_Len_th_base(i, arr, len, L);    }} var arr = [ 1, 2, 3 ];var len = arr.length;var L = 2; // function callprint(arr, len, L); //This code is contributed by SoumikModnal</script>
11
21
31
12
22
32
13
23
33
 
mohit kumar 29
jit_t
Rajput-Ji
nidhi_biet
SoumikMondal
permutation
Combinatorial
Competitive Programming
Mathematical
Mathematical
permutation
Combinatorial
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Ways to sum to N using Natural Numbers up to K with repetitions allowed
Generate all possible combinations of K numbers that sums to N
Combinations with repetitions
Largest substring with same Characters
Generate all possible combinations of at most X characters from a given array
Competitive Programming - A Complete Guide
Practice for cracking any coding interview
Arrow operator -> in C/C++ with Examples
Prefix Sum Array - Implementation and Applications in Competitive Programming
Fast I/O for Competitive Programming