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++

filter_none

edit
close

play_arrow

link
brightness_4
code

// C++ program to find all combinations where every
// element appears twice and distance between
// appearances is equal to the value
#include <bits/stdc++.h>
using namespace std;
  
// Find all combinations that satisfies given constraints
void allCombinationsRec(vector<int> &arr, int elem, int n)
{
    // if all elements are filled, print the solution
    if (elem > n)
    {
        for (int i : arr)
            cout << i << " ";
        cout << endl;
  
        return;
    }
  
    // try all possible combinations for element elem
    for (int i = 0; i < 2*n; i++)
    {
        // if position i and (i+elem+1) are not occupied
        // in the vector
        if (arr[i] == -1 && (i + elem + 1) < 2*n &&
                arr[i + elem + 1] == -1)
        {
            // place elem at position i and (i+elem+1)
            arr[i] = elem;
            arr[i + elem + 1] = elem;
  
            // recurse for next element
            allCombinationsRec(arr, elem + 1, n);
  
            // backtrack (remove elem from position i and (i+elem+1) )
            arr[i] = -1;
            arr[i + elem + 1] = -1;
        }
    }
}
  
void allCombinations(int n)
{
    // create a vector of double the size of given number with
    vector<int> arr(2*n, -1);
  
    // all its elements initialized by 1
    int elem = 1;
  
    // start from element 1
    allCombinationsRec(arr, elem, n);
}
  
// Driver code
int main()
{
    // given number
    int n = 3;
    allCombinations(n);
    return 0;
}

chevron_right


Java

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java program to find all combinations where every
// element appears twice and distance between
// appearances is equal to the value
  
import java.util.Vector;
  
class Test
{
    // Find all combinations that satisfies given constraints
    static void allCombinationsRec(Vector<Integer> arr, int elem, int n)
    {
        // if all elements are filled, print the solution
        if (elem > n)
        {
            for (int i : arr)
                System.out.print(i + " ");
            System.out.println();
       
            return;
        }
       
        // try all possible combinations for element elem
        for (int i = 0; i < 2*n; i++)
        {
            // if position i and (i+elem+1) are not occupied
            // in the vector
            if (arr.get(i) == -1 && (i + elem + 1) < 2*n &&
                    arr.get(i + elem + 1) == -1)
            {
                // place elem at position i and (i+elem+1)
                arr.set(i, elem);
                arr.set(i + elem + 1, elem);
       
                // recurse for next element
                allCombinationsRec(arr, elem + 1, n);
       
                // backtrack (remove elem from position i and (i+elem+1) )
                arr.set(i, -1);
                arr.set(i + elem + 1, -1);
            }
        }
    }
       
    static void allCombinations(int n)
    {
          
        // create a vector of double the size of given number with
        Vector<Integer> arr = new Vector<>();
          
        for (int i = 0; i < 2*n; i++) {
            arr.add(-1);
        }
          
        // all its elements initialized by 1
        int elem = 1;
       
        // start from element 1
        allCombinationsRec(arr, elem, n);
    }
      
    // Driver method
    public static void main(String[] args) 
    {
        // given number
        int n = 3;
        allCombinations(n);
    }
}

chevron_right


Python3

filter_none

edit
close

play_arrow

link
brightness_4
code

# Python3 program to find all combinations 
# where every element appears twice and distance 
# between appearances is equal to the value
  
# Find all combinations that
# satisfies given constraints
def allCombinationsRec(arr, elem, n):
  
    # if all elements are filled,
    # print the solution
    if (elem > n):
      
        for i in (arr):
            print(i, end = " ")
              
        print("")
        return
  
    # Try all possible combinations
    # for element elem
    for i in range(0, 2 * n):
      
        # if position i and (i+elem+1) are  
        # not occupied in the vector
        if (arr[i] == -1 and
           (i + elem + 1) < 2*n and
           arr[i + elem + 1] == -1):
          
            # place elem at position
            # i and (i+elem+1)
            arr[i] = elem
            arr[i + elem + 1] = elem
  
            # recurse for next element
            allCombinationsRec(arr, elem + 1, n)
  
            # backtrack (remove elem from
            # position i and (i+elem+1) )
            arr[i] = -1
            arr[i + elem + 1] = -1
          
def allCombinations(n):
  
    # create a vector of double
    # the size of given number with
    arr = [-1] * (2 * n)
  
    # all its elements initialized by 1
    elem = 1
  
    # start from element 1
    allCombinationsRec(arr, elem, n)
  
# Driver code
n = 3
allCombinations(n)
  
# This code is contributed by Smitha Dinesh Semwal.

chevron_right



Output:

 3 1 2 1 3 2 
 2 3 1 2 1 3 

This article is contributed by Rakesh Kumar. 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 write comments if you find anything incorrect, or you want to share more information about the topic discussed above.



My Personal Notes arrow_drop_up


Article Tags :
Practice Tags :


1


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.