Given an array arr[] consisting of N characters, the task is to generate all possible combinations of at most X elements ( 1 ? X ? N).
Examples:
Input: N = 3, X = 2, arr[] = {‘a’, ‘b’, ‘a’}
Output: a b c bc ca ab cb ac ba
Explanation: All possible combinations using 1 character is 3 {‘a’, ‘b’, ‘c’}. All possible combinations using 2 characters are {“bc” “ca” “ab” “cb” “ac” “ba”}.
Input: N = 3, X = 3, arr[] = {‘d’, ‘a’, ‘b’}
Output: d a b da ab bd ad ba db dab dba abd adb bda bad
Approach: The given problem can be solved using the Dynamic Programming approach. Follow the below steps to solve the problem:
- Generate all possible permutations that can be created with 1 character, which is the given array arr[].
- Store all permutations.
- Once stored, generate all possible permutations of 2 characters and store them.
- Once the last step is completed, discard all permutations of a single character.
- Iteratively, in the same way, calculate the permutations until X is reached.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
void differentFlagPermutations( int X,
vector<string> arr)
{
vector<string> ml;
ml = arr;
for ( int i = 0; i < ml.size(); i++)
{
cout << ml[i] << " " ;
}
int count = ml.size();
for ( int z = 0; z < X - 1; z++)
{
vector<string> tmp;
for ( int i = 0; i < arr.size(); i++)
{
for ( int k = 0; k < ml.size(); k++)
{
if (arr[i] != ml[k])
{
tmp.push_back(ml[k] + arr[i]);
count += 1;
}
}
}
for ( int i = 0; i < tmp.size(); i++)
{
cout << tmp[i] << " " ;
}
ml = tmp;
}
}
int main()
{
vector<string> arr{ "c" , "a" , "b" };
int X = 2;
differentFlagPermutations(X, arr);
return 0;
}
|
Java
import java.util.*;
class GFG
{
static void differentFlagPermutations( int X,
String[] arr)
{
String[] ml = arr;
for ( int i = 0 ; i < ml.length; i++)
{
System.out.print(ml[i] + " " );
}
int count = ml.length;
for ( int z = 0 ; z < X - 1 ; z++)
{
Vector<String> tmp = new Vector<String>();
for ( int i = 0 ; i < arr.length; i++)
{
for ( int k = 0 ; k < ml.length; k++)
{
if (arr[i] != ml[k])
{
tmp.add(ml[k] + arr[i]);
count += 1 ;
}
}
}
for ( int i = 0 ; i < tmp.size(); i++)
{
System.out.print(tmp.get(i) + " " );
}
ml = tmp.toArray( new String[tmp.size()]);;
}
}
public static void main(String[] args)
{
String []arr = { "c" , "a" , "b" };
int X = 2 ;
differentFlagPermutations(X, arr);
}
}
|
Python3
def differentFlagPermutations(X, arr):
ml = arr.copy()
print ( " " .join(ml), end = " " )
count = len (ml)
for z in range (X - 1 ):
tmp = []
for i in arr:
for k in ml:
if i not in k:
tmp.append(k + i)
count + = 1
print ( " " .join(tmp), end = " " )
ml = tmp
arr = [ 'c' , 'a' , 'b' ]
X = 2
differentFlagPermutations(X, arr)
|
C#
using System;
using System.Collections.Generic;
class GFG
{
static void differentFlagPermutations( int X, List< string > arr)
{
List< string > ml = new List< string >();
ml = arr;
for ( int i = 0; i < ml.Count; i++)
{
Console.Write(ml[i] + " " );
}
int count = ml.Count;
for ( int z = 0; z < X - 1; z++)
{
List< string > tmp = new List< string >();
for ( int i = 0; i < arr.Count; i++)
{
for ( int k = 0; k < ml.Count; k++)
{
if (arr[i] != ml[k])
{
tmp.Add(ml[k] + arr[i]);
count += 1;
}
}
}
for ( int i = 0; i < tmp.Count; i++)
{
Console.Write(tmp[i] + " " );
}
ml = tmp;
}
}
static void Main()
{
List< string > arr = new List< string >( new string [] { "c" , "a" , "b" });
int X = 2;
differentFlagPermutations(X, arr);
}
}
|
Javascript
<script>
function differentFlagPermutations(X, arr)
{
let ml = [];
ml = arr;
for (let i = 0; i < ml.length; i++)
{
document.write(ml[i] + " " );
}
let count = ml.length;
for (let z = 0; z < X - 1; z++)
{
let tmp = [];
for (let i = 0; i < arr.length; i++)
{
for (let k = 0; k < ml.length; k++)
{
if (arr[i] != ml[k])
{
tmp.push(ml[k] + arr[i]);
count += 1;
}
}
}
for (let i = 0; i < tmp.length; i++)
{
document.write(tmp[i] + " " );
}
ml = tmp;
}
}
let arr = [ "c" , "a" , "b" ];
let X = 2;
differentFlagPermutations(X, arr);
</script>
|
Output: c a b ac bc ca ba cb ab
Time Complexity: O(X*N2)
Auxiliary Space: O(N2)