Combinations where every element appears twice and distance between appearances is equal to the value
Given a positive number n, we need to find all the combinations of 2*n elements such that every element from 1 to n appears exactly twice and distance between its appearances is exactly equal to value of the element.
Examples:
Input : n = 3
Output : 3 1 2 1 3 2
2 3 1 2 1 3
All elements from 1 to 3 appear
twice and distance between two
appearances is equal to value
of the element.
Input : n = 4
Output : 4 1 3 1 2 4 3 2
2 3 4 2 1 3 1 4
Explanation
We can use backtracking to solve this problem. The idea is to all possible combinations for the first element and recursively explore remaining element to check if they will lead to the solution or not. If current configuration doesn’t result in solution, we backtrack. Note that an element k can be placed at position i and (i+k+1) in the output array i >= 0 and (i+k+1) < 2*n.
Note that no combination of element is possible for some value of n like 2, 5, 6 etc.
C++
#include <bits/stdc++.h>
using namespace std;
void allCombinationsRec(vector< int > &arr, int elem, int n)
{
if (elem > n)
{
for ( int i : arr)
cout << i << " " ;
cout << endl;
return ;
}
for ( int i = 0; i < 2*n; i++)
{
if (arr[i] == -1 && (i + elem + 1) < 2*n &&
arr[i + elem + 1] == -1)
{
arr[i] = elem;
arr[i + elem + 1] = elem;
allCombinationsRec(arr, elem + 1, n);
arr[i] = -1;
arr[i + elem + 1] = -1;
}
}
}
void allCombinations( int n)
{
vector< int > arr(2*n, -1);
int elem = 1;
allCombinationsRec(arr, elem, n);
}
int main()
{
int n = 3;
allCombinations(n);
return 0;
}
|
Java
import java.util.Vector;
class Test
{
static void allCombinationsRec(Vector<Integer> arr, int elem, int n)
{
if (elem > n)
{
for ( int i : arr)
System.out.print(i + " " );
System.out.println();
return ;
}
for ( int i = 0 ; i < 2 *n; i++)
{
if (arr.get(i) == - 1 && (i + elem + 1 ) < 2 *n &&
arr.get(i + elem + 1 ) == - 1 )
{
arr.set(i, elem);
arr.set(i + elem + 1 , elem);
allCombinationsRec(arr, elem + 1 , n);
arr.set(i, - 1 );
arr.set(i + elem + 1 , - 1 );
}
}
}
static void allCombinations( int n)
{
Vector<Integer> arr = new Vector<>();
for ( int i = 0 ; i < 2 *n; i++) {
arr.add(- 1 );
}
int elem = 1 ;
allCombinationsRec(arr, elem, n);
}
public static void main(String[] args)
{
int n = 3 ;
allCombinations(n);
}
}
|
Python3
def allCombinationsRec(arr, elem, n):
if (elem > n):
for i in (arr):
print (i, end = " " )
print ("")
return
for i in range ( 0 , 2 * n):
if (arr[i] = = - 1 and
(i + elem + 1 ) < 2 * n and
arr[i + elem + 1 ] = = - 1 ):
arr[i] = elem
arr[i + elem + 1 ] = elem
allCombinationsRec(arr, elem + 1 , n)
arr[i] = - 1
arr[i + elem + 1 ] = - 1
def allCombinations(n):
arr = [ - 1 ] * ( 2 * n)
elem = 1
allCombinationsRec(arr, elem, n)
n = 3
allCombinations(n)
|
C#
using System;
using System.Collections.Generic;
class Test{
static void allCombinationsRec(List< int > arr, int elem,
int n)
{
if (elem > n)
{
foreach ( int i in arr)
Console.Write(i + " " );
Console.WriteLine();
return ;
}
for ( int i = 0; i < 2 * n; i++)
{
if (arr[i] == -1 && (i + elem + 1) < 2 * n &&
arr[i + elem + 1] == -1)
{
arr[i] = elem;
arr[i + elem + 1] = elem;
allCombinationsRec(arr, elem + 1, n);
arr[i] = -1;
arr[i + elem + 1] = -1;
}
}
}
static void allCombinations( int n)
{
List< int > arr = new List< int >();
for ( int i = 0; i < 2 * n; i++)
{
arr.Add(-1);
}
int elem = 1;
allCombinationsRec(arr, elem, n);
}
public static void Main( string [] args)
{
int n = 3;
allCombinations(n);
}
}
|
Javascript
<script>
function allCombinationsRec(arr, elem, n)
{
if (elem > n) {
for (i of arr)
document.write(i + " " );
document.write( "<br>" );
return ;
}
for (let i = 0; i < 2 * n; i++)
{
if (arr[i] == -1 && (i + elem + 1) < 2 * n &&
arr[i + elem + 1] == -1)
{
arr[i] = elem;
arr[i + elem + 1] = elem;
allCombinationsRec(arr, elem + 1, n);
arr[i] = -1;
arr[i + elem + 1] = -1;
}
}
}
function allCombinations(n)
{
let arr = new Array(2 * n).fill(-1);
let elem = 1;
allCombinationsRec(arr, elem, n);
}
let n = 3;
allCombinations(n);
</script>
|
Output:
3 1 2 1 3 2
2 3 1 2 1 3
Last Updated :
27 Jan, 2022
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...