 GeeksforGeeks App
Open App Browser
Continue

# Possible permutations at a railway track

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

## Recommended: Please try your approach on {IDE} first, before moving on to the solution.

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:

1. At each step we can move either truck from left track to spur track or from spur track to right track.
2. Move one truck from left track to spur track and call recursively for remaining trucks on left and spur track.
3. 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

## C++

 `// 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` `&it : output) {``            ``cout << it << ``' '``;``        ``}``        ` `        ``// Print the spur track trucks``        ``for``(``auto` `&it : 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);``}`

## Java

 `// Java program for the above approach``import` `java.util.*;``class` `GFG``{` `  ``// Helper function to print all the``  ``// possible permutation``  ``static` `void` `printPermute(ArrayList input,``                           ``ArrayList spur,``                           ``ArrayList output)``  ``{` `    ``// If at any recursive call input``    ``// array is empty, then we got our``    ``// one of the permutation``    ``if` `(input.size() == ``0``) {` `      ``// Print the right track trucks``      ``for` `(var it : output) {``        ``System.out.print(it + ``" "``);``      ``}` `      ``// Print the spur track trucks``      ``for` `(var it : spur) {``        ``System.out.print(it + ``" "``);``      ``}` `      ``System.out.print(``"\n"``);``    ``}``    ``else` `{``      ``int` `temp;` `      ``// Pop the element from input``      ``// track and move it to spur``      ``temp = input.get(input.size() - ``1``);``      ``input.remove(input.size() - ``1``);` `      ``// Case 1``      ``// Push the popped truck from``      ``// input to spur track``      ``spur.add(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.remove(spur.size() - ``1``);``      ``input.add(temp);` `      ``// Case 2``      ``if` `(spur.size() != ``0``) {` `        ``// Remove the truck from the spur``        ``// track and move it to the``        ``// output track``        ``temp = spur.get(spur.size() - ``1``);``        ``spur.remove(spur.size() - ``1``);``        ``output.add(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.remove(output.size() - ``1``);``        ``spur.add(temp);``      ``}``    ``}``  ``}` `  ``// Function to print all the possible``  ``// permutation of trucks``  ``static` `void` `possiblePermute(``int` `n)``  ``{``    ``// Array for left, spur and right track``    ``ArrayList spur = ``new` `ArrayList();``    ``ArrayList output``      ``= ``new` `ArrayList();``    ``ArrayList input = ``new` `ArrayList();` `    ``// Insert all truck value 1 to N``    ``for` `(``int` `i = ``1``; i <= n; i++) {``      ``input.add(i);``    ``}` `    ``// Helper function to find``    ``// possible arrangement``    ``printPermute(input, spur, output);``  ``}` `  ``// Driver Code``  ``public` `static` `void` `main(String[] args)``  ``{``    ``// Input number of truck``    ``int` `N = ``4``;` `    ``// Function Call``    ``possiblePermute(N);``  ``}``}` `// This code is contributed by phasing17.`

## Python3

 `# Python program for the above approach` `# Helper function to print all the``# possible permutation``def` `printPermute( input1, spur, output):` `    ``# If at any recursive call input1``    ``# array is empty, then we got our``    ``# one of the permutation``    ``if``(``len``(input1) ``=``=` `0``):``     ` `        ``# Print the right track trucks``        ``for` `it ``in` `output:``            ``print``(it, end ``=` `' '``);``        ` `        ``# Print the spur track trucks``        ``for` `it ``in` `spur:``            ``print``(it, end ``=` `' '``);``        ` `        ``print``()``    ` `    ``else``:``    ` `        ``# Pop the element from input1``        ``# track and move it to spur``        ``temp``=``input1.pop();` `        ``# Case 1``        ``# Push the popped truck from``        ``# input1 to spur track``        ``spur.append(temp);``        ` `        ``# Recursive call for remaining``        ``# trucks on input1, spur and``        ``# output track``        ``printPermute(input1,spur,output);``        ` `        ``# remove the top truck from spur``        ``# track and push it in input1 for``        ``# Case 2 iteration``        ``spur.pop();``        ``input1.append(temp);``        ` `        ``# Case 2``        ``if``(``len``(spur) > ``0``):``            ` `            ``# Remove the truck from the spur``            ``# track and move it to the``            ``# output track``            ``temp``=``spur.pop();``            ``output.append(temp);``            ` `            ``# Recursive call for remaining``            ``# truck on input1, spur and``            ``# output track``            ``printPermute(input1,spur,output);``            ` `            ``# Remove the top truck from the``            ``# output track and move it to``            ``# the spur track for the next``            ``# iteration``            ``output.pop();``            ``spur.append(temp);` `# Function to print all the possible``# permutation of trucks``def` `possiblePermute(n):` `    ``# Array for left, spur and right track``    ``spur ``=` `[];``    ``output ``=` `[];``    ``input1 ``=` `[];`` ` `    ``# Insert all truck value 1 to N``    ``for` `i ``in` `range``(``1``, n ``+` `1``):``        ``input1.append(i);``    ` `    ``# Helper function to find``    ``# possible arrangement``    ``printPermute(input1, spur, output);` `# Driver Code``N ``=` `4``;``    ` `# Function Call``possiblePermute(N);` `# This code is contributed by phasing17`

## C#

 `// C# program for the above approach``using` `System;``using` `System.Collections.Generic;` `class` `GFG``{``  ``// Helper function to print all the``  ``// possible permutation``  ``static` `void` `printPermute(List<``int``> input,``                           ``List<``int``>spur,``                           ``List<``int``>output)``  ``{` `    ``// If at any recursive call input``    ``// array is empty, then we got our``    ``// one of the permutation``    ``if``(input.Count == 0)``    ``{` `      ``// Print the right track trucks``      ``foreach``(``var` `it ``in` `output) {``        ``Console.Write(it + ``" "``);``      ``}` `      ``// Print the spur track trucks``      ``foreach``(``var` `it ``in` `spur) {``        ``Console.Write(it + ``" "``);``      ``}`  `      ``Console.Write(``"\n"``);``    ``}``    ``else``    ``{``      ``int` `temp;` `      ``// Pop the element from input``      ``// track and move it to spur``      ``temp=input[input.Count - 1];``      ``input.RemoveAt(input.Count - 1);` `      ``// Case 1``      ``// Push the popped truck from``      ``// input to spur track``      ``spur.Add(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.RemoveAt(spur.Count - 1);``      ``input.Add(temp);` `      ``// Case 2``      ``if``(spur.Count != 0) {` `        ``// Remove the truck from the spur``        ``// track and move it to the``        ``// output track``        ``temp=spur[spur.Count - 1];``        ``spur.RemoveAt(spur.Count - 1);``        ``output.Add(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.RemoveAt(output.Count - 1);``        ``spur.Add(temp);``      ``}``    ``}``  ``}` `  ``// Function to print all the possible``  ``// permutation of trucks``  ``static` `void` `possiblePermute(``int` `n)``  ``{``    ``// Array for left, spur and right track``    ``List<``int``>spur = ``new` `List<``int``>();``    ``List<``int``>output = ``new` `List<``int``>();``    ``List<``int``>input =  ``new` `List<``int``>();` `    ``// Insert all truck value 1 to N``    ``for``(``int` `i = 1; i <= n; i++) {``      ``input.Add(i);``    ``}` `    ``// Helper function to find``    ``// possible arrangement``    ``printPermute(input, spur, output);``  ``}` `  ``// Driver Code``  ``public` `static` `void` `Main(``string``[] args)``  ``{``    ``// Input number of truck``    ``int` `N = 4;` `    ``// Function Call``    ``possiblePermute(N);``  ``}``}` `// This code is contributed by phasing17.`

## Javascript

 `// JS program for the above approach` `// Helper function to print all the``// possible permutation``function` `printPermute( input,``                 ``spur, output)``{` `    ``// If at any recursive call input``    ``// array is empty, then we got our``    ``// one of the permutation``    ``if``(input.length == 0)``    ``{``        ` `        ``// Print the right track trucks``        ``for``(let it of output) {``            ``process.stdout.write(it + ``' '``);``        ``}``        ` `        ``// Print the spur track trucks``        ``for``(let it of spur) {``            ``process.stdout.write(it + ``' '``);``        ``}``        ` `        ` `        ``console.log(``' '``);``    ``}``    ``else``    ``{``        ``let temp;``        ` `        ``// Pop the element from input``        ``// track and move it to spur``        ``temp=input.pop();` `        ``// Case 1``        ``// Push the popped truck from``        ``// input to spur track``        ``spur.push(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();``        ``input.push(temp);``        ` `        ``// Case 2``        ``if``(spur.length > 0) {``            ` `            ``// Remove the truck from the spur``            ``// track and move it to the``            ``// output track``            ``temp=spur.pop();``            ``output.push(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();``            ``spur.push(temp);``        ``}``    ``}``}` `// Function to print all the possible``// permutation of trucks``function` `possiblePermute(n)``{``    ``// Array for left, spur and right track``    ``let spur = [];``    ``let output = [];``    ``let input = [];``    ` `    ` `    ``// Insert all truck value 1 to N``    ``for``(``var` `i = 1; i <= n; i++) {``        ``input.push(i);``    ``}``    ` `    ``// Helper function to find``    ``// possible arrangement``    ``printPermute(input, spur, output);``}` `// Driver Code``let N = 4;``    ` `// Function Call``possiblePermute(N);` `// This code is contributed by phasing17`

Output

```4 3 2 1
2 4 3 1
2 3 4 1
2 3 4 1
3 4 2 1
3 2 4 1
3 2 4 1
3 4 2 1
3 4 2 1
4 3 2 1
4 2 3 1
4 2 3 1
4 3 2 1
4 3 2 1 ```

My Personal Notes arrow_drop_up