Given a left, right, and a spur track as shown in the below figure. There are N trucks from value 1 to N arranged in the left track. We can move directly N trucks to the right track but there can be more possibilities of moving the trucks to the right track using the spur track. We can move any truck to spur track and then move it to the right track. The task is to print all the possible permutation order in which all the N trucks can be moved from left track to right track.
Note: Once a truck is moved from left truck to right/spur track then it can’t be moved to left track again.

Examples:
Input: N = 2
Output:
1 2
2 1
Explanation:
For the first permutation:
left[] = {1, 2} right[] = {}, and spur[] = {}The truck with value 2 moved to the right track, then
left[] = {1} right[] = {2}, and spur[] = {}Now moving with value 1 to the right track, then
left[] = {} right[] = {1, 2}, and spur[] = {}
For the second permutation:
left[] = {1, 2} right[] = {}, and spur[] = {}The truck with value 2 move to the spur track, then
left[] = {1} right[] = {}, and spur[] = {2}The truck with value 1 move to the right track, then
left[] = {} right[] = {1}, and spur[] = {2}The truck with value 2 in the spur track move to the right track, then
left[] = {} right[] = {2, 1}, and spur[] = {}Input: N = 3
Output:
1 2 3
2 1 3
3 2 1
3 1 2
2 3 1
Approach: This problem is a variation of Tower Of Hanoi and can be solved using Recursion. Below are the following cases:
- Case 1: We can move the truck from left track to the spur track and recursively check for the remaining trucks on the left and spur tracks.
- Case 2: We can move the truck from the spur track to right track and check for the remaining trucks of the left and spur tracks.
Below are the steps:
- At each step we can move either truck from left track to spur track or from spur track to right track.
- Move one truck from left track to spur track and call recursively for remaining trucks on left and spur track.
- At any recursive call, if the input track is empty then, move every truck on the spur track to right track and print the current permutation on the right track
- Generate all binary permutations such that there are more or equal 1's than 0's before every point in all permutations
- Number of possible permutations when absolute difference between number of elements to the right and left are given
- Print the two possible permutations from a given sequence
- Count of possible permutations of a number represented as a sum of 2's, 4's and 6's only
- Smallest number containing all possible N length permutations using digits 0 to D
- Count of distinct permutations of every possible length of given string
- Count possible permutations of given array satisfying the given conditions
- Generate all possible permutations of a Number divisible by N
- Time taken by two persons to meet on a circular track
- Length of race track based on the final distance between participants
- Minimum LCM and GCD possible among all possible sub-arrays
- Minimum possible value T such that at most D Partitions of the Array having at most sum T is possible
- Write a program to print all permutations of a given string
- Print all permutations with repetition of characters
- Print all permutations in sorted (lexicographic) order
- Anagram Substring Search (Or Search for all permutations)
- Print all distinct permutations of a given string with duplicates
- Print all palindrome permutations of a string
- Permutations of a given string using STL
- All permutations of a string using iteration
Below is the implementation of the above approach:
// C++ program for the above approach #include "bits/stdc++.h" using namespace std; // Helper function to print all the // possible permutation void printPermute(vector<int>&input;, vector<int>&spur;, vector<int>&output;) { // If at any recursive call input // array is empty, then we got our // one of the permutation if(input.empty()) { // Print the right track trucks for(auto ⁢ : output) { cout << it << ' '; } // Print the spur track trucks for(auto ⁢ : spur) { cout << it << ' '; } cout << endl; } else { int temp; // Pop the element from input // track and move it to spur temp=input.back(); input.pop_back(); // Case 1 // Push the popped truck from // input to spur track spur.push_back(temp); // Recursive call for remaining // trucks on input, spur and // output track printPermute(input,spur,output); // remove the top truck from spur // track and push it in input for // Case 2 iteration spur.pop_back(); input.push_back(temp); // Case 2 if(!spur.empty()) { // Remove the truck from the spur // track and move it to the // output track temp=spur.back(); spur.pop_back(); output.push_back(temp); // Recursive call for remaining // truck on input, spur and // output track printPermute(input,spur,output); // Remove the top truck from the // output track and move it to // the spur track for the next // iteration output.pop_back(); spur.push_back(temp); } } } // Function to print all the possible // permutation of trucks void possiblePermute(int n) { // Array for left, spur and right track vector<int>spur; vector<int>output; vector<int>input; // Insert all truck value 1 to N for(int i = 1; i <= n; i++) { input.push_back(i); } // Helper function to find // possible arrangement printPermute(input, spur, output); } // Driver Code int main() { // Input number of truck int N = 4; // Function Call possiblePermute(N); } |
1 2 3 4 2 1 3 4 3 2 1 4 4 3 2 1 3 1 2 4 2 3 1 4 4 2 3 1 4 3 1 2 2 4 3 1 4 1 2 3 2 4 1 3 3 2 4 1 3 4 1 2 2 3 4 1
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.
Recommended Posts:
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.

