Generate all palindromic subsequence of n natural number Whose adjacent elements are different type

Here given code implementation process.

// C Program
// Generate all palindromic subsequence of n natural number 
// Whose adjacent elements are different type
#include <stdio.h>

// Display calculated result
void printResult(int result[], int n)
{
	for (int i = 0; i < n; i++)
	{
		printf("%d ", result[i]);
	}
	printf("\n ");
}
int bothSimilar(int a, int b)
{
	if ((a % 2 == 0 && b % 2 == 0) || 
        (a % 2 == 1 && b % 2 == 1))
	{
		return 1;
	}
	return 0;
}
void sequence(int result[], int index, int n)
{
	if (index == n / 2)
	{
		if ((n % 2) != 0)
		{
			// Handles the request of odd length palindrome sequence
			for (int i = 1; i <= n; i++)
			{
				if (!bothSimilar(result[index - 1], i))
				{
					result[index] = i;
					printResult(result, n);
				}
			}
		}
		else
		{
			printResult(result, n);
		}
	}
	else if (index > n / 2)
	{
		return;
	}
	else
	{
		for (int i = 1; i <= n; i++)
		{
			if (index == 0 || (index > 0 && 
                               !bothSimilar(result[index - 1], i)))
			{
				result[index] = i;
				result[(n - 1) - index] = i;
				sequence(result, index + 1, n);
			}
		}
	}
}
void palindrome(int n)
{
	if (n <= 1 || n % 2 == 0)
	{
		return;
	}
	printf("\n Given Length : %d \n ", n);
	int result[n];
	sequence(result, 0, n);
}
int main()
{
	int n = 3;
	/*
	    n = 3
	    Given Length : 3 
	    ---------------
	
	    1 2 1 
	    2 1 2 
	    2 3 2 
	    3 2 3 
	    ---------------
	*/
	palindrome(n);
	n = 5;
	/*
	    n = 5
	    Given Length : 5 
	  ----------------
	  
	    1 2 1 2 1 
	    1 2 3 2 1 
	    1 2 5 2 1 
	    1 4 1 4 1 
	    1 4 3 4 1 
	    1 4 5 4 1 
	    2 1 2 1 2 
	    2 1 4 1 2 
	    2 3 2 3 2 
	    2 3 4 3 2 
	    2 5 2 5 2 
	    2 5 4 5 2 
	    3 2 1 2 3 
	    3 2 3 2 3 
	    3 2 5 2 3 
	    3 4 1 4 3 
	    3 4 3 4 3 
	    3 4 5 4 3 
	    4 1 2 1 4 
	    4 1 4 1 4 
	    4 3 2 3 4 
	    4 3 4 3 4 
	    4 5 2 5 4 
	    4 5 4 5 4 
	    5 2 1 2 5 
	    5 2 3 2 5 
	    5 2 5 2 5 
	    5 4 1 4 5 
	    5 4 3 4 5 
	    5 4 5 4 5
	  ---------------
	  Combination of Even Odd 
	              or Odd Even
	*/
	palindrome(n);
	return 0;
}

Output

 Given Length : 3
 1 2 1
 2 1 2
 2 3 2
 3 2 3

 Given Length : 5
 1 2 1 2 1
 1 2 3 2 1
 1 2 5 2 1
 1 4 1 4 1
 1 4 3 4 1
 1 4 5 4 1
 2 1 2 1 2
 2 1 4 1 2
 2 3 2 3 2
 2 3 4 3 2
 2 5 2 5 2
 2 5 4 5 2
 3 2 1 2 3
 3 2 3 2 3
 3 2 5 2 3
 3 4 1 4 3
 3 4 3 4 3
 3 4 5 4 3
 4 1 2 1 4
 4 1 4 1 4
 4 3 2 3 4
 4 3 4 3 4
 4 5 2 5 4
 4 5 4 5 4
 5 2 1 2 5
 5 2 3 2 5
 5 2 5 2 5
 5 4 1 4 5
 5 4 3 4 5
 5 4 5 4 5
// Java program for
// Generate all palindromic subsequence of n natural number 
// Whose adjacent elements are different type
public class Combination
{
	// Display calculated result
	public void printResult(int[] result, int n)
	{
		for (int i = 0; i < n; i++)
		{
			System.out.print("  " + result[i]);
		}
		System.out.print("\n ");
	}
	public boolean bothSimilar(int a, int b)
	{
		if ((a % 2 == 0 && b % 2 == 0) || 
            (a % 2 == 1 && b % 2 == 1))
		{
			return true;
		}
		return false;
	}
	public void sequence(int[] result, int index, int n)
	{
		if (index == n / 2)
		{
			if ((n % 2) != 0)
			{
				// Handles the request of odd length palindrome sequence
				for (int i = 1; i <= n; i++)
				{
					if (!bothSimilar(result[index - 1], i))
					{
						result[index] = i;
						printResult(result, n);
					}
				}
			}
			else
			{
				printResult(result, n);
			}
		}
		else if (index > n / 2)
		{
			return;
		}
		else
		{
			for (int i = 1; i <= n; i++)
			{
				if (index == 0 || (index > 0 && 
                                   !bothSimilar(result[index - 1], i)))
				{
					// Set palindrome elements
					result[index] = i;
					result[(n - 1) - index] = i;
					sequence(result, index + 1, n);
				}
			}
		}
	}
	public void palindrome(int n)
	{
		if (n <= 1 || n % 2 == 0)
		{
			return;
		}
		System.out.print("\n Given Length : " + n + " \n ");
		// Auxiliary array which is collect result
		int[] result = new int[n];
		sequence(result, 0, n);
	}
	public static void main(String[] args)
	{
		Combination task = new Combination();
		int n = 3;
		/*
		    n = 3
		    Given Length : 3 
		    ---------------
		
		    1 2 1 
		    2 1 2 
		    2 3 2 
		    3 2 3 
		    ---------------
		*/
		task.palindrome(n);
		n = 5;
		/*
		    n = 5
		    Given Length : 5 
		  ----------------
		  
		    1 2 1 2 1 
		    1 2 3 2 1 
		    1 2 5 2 1 
		    1 4 1 4 1 
		    1 4 3 4 1 
		    1 4 5 4 1 
		    2 1 2 1 2 
		    2 1 4 1 2 
		    2 3 2 3 2 
		    2 3 4 3 2 
		    2 5 2 5 2 
		    2 5 4 5 2 
		    3 2 1 2 3 
		    3 2 3 2 3 
		    3 2 5 2 3 
		    3 4 1 4 3 
		    3 4 3 4 3 
		    3 4 5 4 3 
		    4 1 2 1 4 
		    4 1 4 1 4 
		    4 3 2 3 4 
		    4 3 4 3 4 
		    4 5 2 5 4 
		    4 5 4 5 4 
		    5 2 1 2 5 
		    5 2 3 2 5 
		    5 2 5 2 5 
		    5 4 1 4 5 
		    5 4 3 4 5 
		    5 4 5 4 5
		  ---------------
		  Combination of Even Odd 
		              or Odd Even
		*/
		task.palindrome(n);
	}
}

Output

 Given Length : 3
   1  2  1
   2  1  2
   2  3  2
   3  2  3

 Given Length : 5
   1  2  1  2  1
   1  2  3  2  1
   1  2  5  2  1
   1  4  1  4  1
   1  4  3  4  1
   1  4  5  4  1
   2  1  2  1  2
   2  1  4  1  2
   2  3  2  3  2
   2  3  4  3  2
   2  5  2  5  2
   2  5  4  5  2
   3  2  1  2  3
   3  2  3  2  3
   3  2  5  2  3
   3  4  1  4  3
   3  4  3  4  3
   3  4  5  4  3
   4  1  2  1  4
   4  1  4  1  4
   4  3  2  3  4
   4  3  4  3  4
   4  5  2  5  4
   4  5  4  5  4
   5  2  1  2  5
   5  2  3  2  5
   5  2  5  2  5
   5  4  1  4  5
   5  4  3  4  5
   5  4  5  4  5
// Include header file
#include <iostream>
using namespace std;
// C++ program for
// Generate all palindromic subsequence of n natural number
// Whose adjacent elements are different type
class Combination
{
	public:
		// Display calculated result
		void printResult(int result[], int n)
		{
			for (int i = 0; i < n; i++)
			{
				cout << "  " << result[i];
			}
			cout << "\n ";
		}
	bool bothSimilar(int a, int b)
	{
		if ((a % 2 == 0 && b % 2 == 0) || 
            (a % 2 == 1 && b % 2 == 1))
		{
			return true;
		}
		return false;
	}
	void sequence(int result[], int index, int n)
	{
		if (index == n / 2)
		{
			if ((n % 2) != 0)
			{
				// Handles the request of odd length palindrome sequence
				for (int i = 1; i <= n; i++)
				{
					if (!this->bothSimilar(result[index - 1], i))
					{
						result[index] = i;
						this->printResult(result, n);
					}
				}
			}
			else
			{
				this->printResult(result, n);
			}
		}
		else if (index > n / 2)
		{
			return;
		}
		else
		{
			for (int i = 1; i <= n; i++)
			{
				if (index == 0 || (index > 0 && 
                                   !this->bothSimilar(result[index - 1], i)))
				{
					// Set palindrome elements
					result[index] = i;
					result[(n - 1) - index] = i;
					this->sequence(result, index + 1, n);
				}
			}
		}
	}
	void palindrome(int n)
	{
		if (n <= 1 || n % 2 == 0)
		{
			return;
		}
		cout << "\n Given Length : " << n << " \n ";
		// Auxiliary array which is collect result
		int result[n];
		this->sequence(result, 0, n);
	}
};
int main()
{
	Combination *task = new Combination();
	int n = 3;
	/*
	    n = 3
	    Given Length : 3 
	    ---------------

	    1 2 1 
	    2 1 2 
	    2 3 2 
	    3 2 3 
	    ---------------
	*/
	task->palindrome(n);
	n = 5;
	/*
	    n = 5
	    Given Length : 5 
	  ----------------
	  
	    1 2 1 2 1 
	    1 2 3 2 1 
	    1 2 5 2 1 
	    1 4 1 4 1 
	    1 4 3 4 1 
	    1 4 5 4 1 
	    2 1 2 1 2 
	    2 1 4 1 2 
	    2 3 2 3 2 
	    2 3 4 3 2 
	    2 5 2 5 2 
	    2 5 4 5 2 
	    3 2 1 2 3 
	    3 2 3 2 3 
	    3 2 5 2 3 
	    3 4 1 4 3 
	    3 4 3 4 3 
	    3 4 5 4 3 
	    4 1 2 1 4 
	    4 1 4 1 4 
	    4 3 2 3 4 
	    4 3 4 3 4 
	    4 5 2 5 4 
	    4 5 4 5 4 
	    5 2 1 2 5 
	    5 2 3 2 5 
	    5 2 5 2 5 
	    5 4 1 4 5 
	    5 4 3 4 5 
	    5 4 5 4 5
	  ---------------
	  Combination of Even Odd 
	              or Odd Even
	*/
	task->palindrome(n);
	return 0;
}

Output

 Given Length : 3
   1  2  1
   2  1  2
   2  3  2
   3  2  3

 Given Length : 5
   1  2  1  2  1
   1  2  3  2  1
   1  2  5  2  1
   1  4  1  4  1
   1  4  3  4  1
   1  4  5  4  1
   2  1  2  1  2
   2  1  4  1  2
   2  3  2  3  2
   2  3  4  3  2
   2  5  2  5  2
   2  5  4  5  2
   3  2  1  2  3
   3  2  3  2  3
   3  2  5  2  3
   3  4  1  4  3
   3  4  3  4  3
   3  4  5  4  3
   4  1  2  1  4
   4  1  4  1  4
   4  3  2  3  4
   4  3  4  3  4
   4  5  2  5  4
   4  5  4  5  4
   5  2  1  2  5
   5  2  3  2  5
   5  2  5  2  5
   5  4  1  4  5
   5  4  3  4  5
   5  4  5  4  5
// Include namespace system
using System;
// Csharp program for
// Generate all palindromic subsequence of n natural number
// Whose adjacent elements are different type
public class Combination
{
	// Display calculated result
	public void printResult(int[] result, int n)
	{
		for (int i = 0; i < n; i++)
		{
			Console.Write("  " + result[i]);
		}
		Console.Write("\n ");
	}
	public Boolean bothSimilar(int a, int b)
	{
		if ((a % 2 == 0 && b % 2 == 0) || 
            (a % 2 == 1 && b % 2 == 1))
		{
			return true;
		}
		return false;
	}
	public void sequence(int[] result, int index, int n)
	{
		if (index == n / 2)
		{
			if ((n % 2) != 0)
			{
				// Handles the request of odd length palindrome sequence
				for (int i = 1; i <= n; i++)
				{
					if (!this.bothSimilar(result[index - 1], i))
					{
						result[index] = i;
						this.printResult(result, n);
					}
				}
			}
			else
			{
				this.printResult(result, n);
			}
		}
		else if (index > n / 2)
		{
			return;
		}
		else
		{
			for (int i = 1; i <= n; i++)
			{
				if (index == 0 || (index > 0 && 
                                   !this.bothSimilar(result[index - 1], i)))
				{
					// Set palindrome elements
					result[index] = i;
					result[(n - 1) - index] = i;
					this.sequence(result, index + 1, n);
				}
			}
		}
	}
	public void palindrome(int n)
	{
		if (n <= 1 || n % 2 == 0)
		{
			return;
		}
		Console.Write("\n Given Length : " + n + " \n ");
		// Auxiliary array which is collect result
		int[] result = new int[n];
		this.sequence(result, 0, n);
	}
	public static void Main(String[] args)
	{
		Combination task = new Combination();
		int n = 3;
		/*
		    n = 3
		    Given Length : 3 
		    ---------------

		    1 2 1 
		    2 1 2 
		    2 3 2 
		    3 2 3 
		    ---------------
		*/
		task.palindrome(n);
		n = 5;
		/*
		    n = 5
		    Given Length : 5 
		  ----------------
		  
		    1 2 1 2 1 
		    1 2 3 2 1 
		    1 2 5 2 1 
		    1 4 1 4 1 
		    1 4 3 4 1 
		    1 4 5 4 1 
		    2 1 2 1 2 
		    2 1 4 1 2 
		    2 3 2 3 2 
		    2 3 4 3 2 
		    2 5 2 5 2 
		    2 5 4 5 2 
		    3 2 1 2 3 
		    3 2 3 2 3 
		    3 2 5 2 3 
		    3 4 1 4 3 
		    3 4 3 4 3 
		    3 4 5 4 3 
		    4 1 2 1 4 
		    4 1 4 1 4 
		    4 3 2 3 4 
		    4 3 4 3 4 
		    4 5 2 5 4 
		    4 5 4 5 4 
		    5 2 1 2 5 
		    5 2 3 2 5 
		    5 2 5 2 5 
		    5 4 1 4 5 
		    5 4 3 4 5 
		    5 4 5 4 5
		  ---------------
		  Combination of Even Odd 
		              or Odd Even
		*/
		task.palindrome(n);
	}
}

Output

 Given Length : 3
   1  2  1
   2  1  2
   2  3  2
   3  2  3

 Given Length : 5
   1  2  1  2  1
   1  2  3  2  1
   1  2  5  2  1
   1  4  1  4  1
   1  4  3  4  1
   1  4  5  4  1
   2  1  2  1  2
   2  1  4  1  2
   2  3  2  3  2
   2  3  4  3  2
   2  5  2  5  2
   2  5  4  5  2
   3  2  1  2  3
   3  2  3  2  3
   3  2  5  2  3
   3  4  1  4  3
   3  4  3  4  3
   3  4  5  4  3
   4  1  2  1  4
   4  1  4  1  4
   4  3  2  3  4
   4  3  4  3  4
   4  5  2  5  4
   4  5  4  5  4
   5  2  1  2  5
   5  2  3  2  5
   5  2  5  2  5
   5  4  1  4  5
   5  4  3  4  5
   5  4  5  4  5
package main
import "fmt"
// Go program for
// Generate all palindromic subsequence of n natural number
// Whose adjacent elements are different type
type Combination struct {}
func getCombination() * Combination {
	var me *Combination = &Combination {}
	return me
}
// Display calculated result
func(this Combination) printResult(result[] int, n int) {
	for i := 0 ; i < n ; i++ {
		fmt.Print("  ", result[i])
	}
	fmt.Print("\n ")
}
func(this Combination) bothSimilar(a, b int) bool {
	if (a % 2 == 0 && b % 2 == 0) || 
	   (a % 2 == 1 && b % 2 == 1) {
		return true
	}
	return false
}
func(this Combination) sequence(result[] int, index int, n int) {
	if index == n / 2 {
		if (n % 2) != 0 {
			// Handles the request of odd length palindrome sequence
			for i := 1 ; i <= n ; i++ {
				if !this.bothSimilar(result[index - 1], i) {
					result[index] = i
					this.printResult(result, n)
				}
			}
		} else {
			this.printResult(result, n)
		}
	} else if index > n / 2 {
		return
	} else {
		for i := 1 ; i <= n ; i++ {
			if index == 0 || 
			(index > 0 && !this.bothSimilar(result[index - 1], i)) {
				// Set palindrome elements
				result[index] = i
				result[(n - 1) - index] = i
				this.sequence(result, index + 1, n)
			}
		}
	}
}
func(this Combination) palindrome(n int) {
	if n <= 1 || n % 2 == 0 {
		return
	}
	fmt.Print("\n Given Length : ", n, " \n ")
	// Auxiliary array which is collect result
	var result = make([] int, n)
	this.sequence(result, 0, n)
}
func main() {
	var task * Combination = getCombination()
	var n int = 3
	/*
	    n = 3
	    Given Length : 3 
	    ---------------

	    1 2 1 
	    2 1 2 
	    2 3 2 
	    3 2 3 
	    ---------------
	*/
	task.palindrome(n)
	n = 5
	/*
	    n = 5
	    Given Length : 5 
	  ----------------
	  
	    1 2 1 2 1 
	    1 2 3 2 1 
	    1 2 5 2 1 
	    1 4 1 4 1 
	    1 4 3 4 1 
	    1 4 5 4 1 
	    2 1 2 1 2 
	    2 1 4 1 2 
	    2 3 2 3 2 
	    2 3 4 3 2 
	    2 5 2 5 2 
	    2 5 4 5 2 
	    3 2 1 2 3 
	    3 2 3 2 3 
	    3 2 5 2 3 
	    3 4 1 4 3 
	    3 4 3 4 3 
	    3 4 5 4 3 
	    4 1 2 1 4 
	    4 1 4 1 4 
	    4 3 2 3 4 
	    4 3 4 3 4 
	    4 5 2 5 4 
	    4 5 4 5 4 
	    5 2 1 2 5 
	    5 2 3 2 5 
	    5 2 5 2 5 
	    5 4 1 4 5 
	    5 4 3 4 5 
	    5 4 5 4 5
	  ---------------
	  Combination of Even Odd 
	              or Odd Even
	*/
	task.palindrome(n)
}

Output

 Given Length : 3
   1  2  1
   2  1  2
   2  3  2
   3  2  3

 Given Length : 5
   1  2  1  2  1
   1  2  3  2  1
   1  2  5  2  1
   1  4  1  4  1
   1  4  3  4  1
   1  4  5  4  1
   2  1  2  1  2
   2  1  4  1  2
   2  3  2  3  2
   2  3  4  3  2
   2  5  2  5  2
   2  5  4  5  2
   3  2  1  2  3
   3  2  3  2  3
   3  2  5  2  3
   3  4  1  4  3
   3  4  3  4  3
   3  4  5  4  3
   4  1  2  1  4
   4  1  4  1  4
   4  3  2  3  4
   4  3  4  3  4
   4  5  2  5  4
   4  5  4  5  4
   5  2  1  2  5
   5  2  3  2  5
   5  2  5  2  5
   5  4  1  4  5
   5  4  3  4  5
   5  4  5  4  5
<?php
// Php program for
// Generate all palindromic subsequence of n natural number
// Whose adjacent elements are different type
class Combination
{
	// Display calculated result
	public	function printResult($result, $n)
	{
		for ($i = 0; $i < $n; $i++)
		{
			echo("  ".$result[$i]);
		}
		echo("\n ");
	}
	public	function bothSimilar($a, $b)
	{
		if (($a % 2 == 0 && $b % 2 == 0) || 
            ($a % 2 == 1 && $b % 2 == 1))
		{
			return true;
		}
		return false;
	}
	public	function sequence($result, $index, $n)
	{
		if ($index == (int)($n / 2))
		{
			if (($n % 2) != 0)
			{
				// Handles the request of odd length palindrome sequence
				for ($i = 1; $i <= $n; $i++)
				{
					if (!$this->bothSimilar($result[$index - 1], $i))
					{
						$result[$index] = $i;
						$this->printResult($result, $n);
					}
				}
			}
			else
			{
				$this->printResult($result, $n);
			}
		}
		else if ($index > (int)($n / 2))
		{
			return;
		}
		else
		{
			for ($i = 1; $i <= $n; $i++)
			{
				if ($index == 0 || ($index > 0 && 
                                    !$this->bothSimilar($result[$index - 1],
                                                        $i)))
				{
					// Set palindrome elements
					$result[$index] = $i;
					$result[($n - 1) - $index] = $i;
					$this->sequence($result, $index + 1, $n);
				}
			}
		}
	}
	public	function palindrome($n)
	{
		if ($n <= 1 || $n % 2 == 0)
		{
			return;
		}
		echo("\n Given Length : ".$n.
			" \n ");
		// Auxiliary array which is collect result
		$result = array_fill(0, $n, 0);
		$this->sequence($result, 0, $n);
	}
}

function main()
{
	$task = new Combination();
	$n = 3;
	/*
	    n = 3
	    Given Length : 3 
	    ---------------

	    1 2 1 
	    2 1 2 
	    2 3 2 
	    3 2 3 
	    ---------------
	*/
	$task->palindrome($n);
	$n = 5;
	/*
	    n = 5
	    Given Length : 5 
	  ----------------
	  
	    1 2 1 2 1 
	    1 2 3 2 1 
	    1 2 5 2 1 
	    1 4 1 4 1 
	    1 4 3 4 1 
	    1 4 5 4 1 
	    2 1 2 1 2 
	    2 1 4 1 2 
	    2 3 2 3 2 
	    2 3 4 3 2 
	    2 5 2 5 2 
	    2 5 4 5 2 
	    3 2 1 2 3 
	    3 2 3 2 3 
	    3 2 5 2 3 
	    3 4 1 4 3 
	    3 4 3 4 3 
	    3 4 5 4 3 
	    4 1 2 1 4 
	    4 1 4 1 4 
	    4 3 2 3 4 
	    4 3 4 3 4 
	    4 5 2 5 4 
	    4 5 4 5 4 
	    5 2 1 2 5 
	    5 2 3 2 5 
	    5 2 5 2 5 
	    5 4 1 4 5 
	    5 4 3 4 5 
	    5 4 5 4 5
	  ---------------
	  Combination of Even Odd 
	              or Odd Even
	*/
	$task->palindrome($n);
}
main();

Output

 Given Length : 3
   1  2  1
   2  1  2
   2  3  2
   3  2  3

 Given Length : 5
   1  2  1  2  1
   1  2  3  2  1
   1  2  5  2  1
   1  4  1  4  1
   1  4  3  4  1
   1  4  5  4  1
   2  1  2  1  2
   2  1  4  1  2
   2  3  2  3  2
   2  3  4  3  2
   2  5  2  5  2
   2  5  4  5  2
   3  2  1  2  3
   3  2  3  2  3
   3  2  5  2  3
   3  4  1  4  3
   3  4  3  4  3
   3  4  5  4  3
   4  1  2  1  4
   4  1  4  1  4
   4  3  2  3  4
   4  3  4  3  4
   4  5  2  5  4
   4  5  4  5  4
   5  2  1  2  5
   5  2  3  2  5
   5  2  5  2  5
   5  4  1  4  5
   5  4  3  4  5
   5  4  5  4  5
// Node JS program for
// Generate all palindromic subsequence of n natural number
// Whose adjacent elements are different type
class Combination
{
	// Display calculated result
	printResult(result, n)
	{
		for (var i = 0; i < n; i++)
		{
			process.stdout.write("  " + result[i]);
		}
		process.stdout.write("\n ");
	}
	bothSimilar(a, b)
	{
		if ((a % 2 == 0 && b % 2 == 0) || 
            (a % 2 == 1 && b % 2 == 1))
		{
			return true;
		}
		return false;
	}
	sequence(result, index, n)
	{
		if (index == parseInt(n / 2))
		{
			if ((n % 2) != 0)
			{
				// Handles the request of odd length palindrome sequence
				for (var i = 1; i <= n; i++)
				{
					if (!this.bothSimilar(result[index - 1], i))
					{
						result[index] = i;
						this.printResult(result, n);
					}
				}
			}
			else
			{
				this.printResult(result, n);
			}
		}
		else if (index > parseInt(n / 2))
		{
			return;
		}
		else
		{
			for (var i = 1; i <= n; i++)
			{
				if (index == 0 || 
                    (index > 0 && 
                     !this.bothSimilar(result[index - 1], i)))
				{
					// Set palindrome elements
					result[index] = i;
					result[(n - 1) - index] = i;
					this.sequence(result, index + 1, n);
				}
			}
		}
	}
	palindrome(n)
	{
		if (n <= 1 || n % 2 == 0)
		{
			return;
		}
		process.stdout.write("\n Given Length : " + n + " \n ");
		// Auxiliary array which is collect result
		var result = Array(n).fill(0);
		this.sequence(result, 0, n);
	}
}

function main()
{
	var task = new Combination();
	var n = 3;
	/*
	    n = 3
	    Given Length : 3 
	    ---------------

	    1 2 1 
	    2 1 2 
	    2 3 2 
	    3 2 3 
	    ---------------
	*/
	task.palindrome(n);
	n = 5;
	/*
	    n = 5
	    Given Length : 5 
	  ----------------
	  
	    1 2 1 2 1 
	    1 2 3 2 1 
	    1 2 5 2 1 
	    1 4 1 4 1 
	    1 4 3 4 1 
	    1 4 5 4 1 
	    2 1 2 1 2 
	    2 1 4 1 2 
	    2 3 2 3 2 
	    2 3 4 3 2 
	    2 5 2 5 2 
	    2 5 4 5 2 
	    3 2 1 2 3 
	    3 2 3 2 3 
	    3 2 5 2 3 
	    3 4 1 4 3 
	    3 4 3 4 3 
	    3 4 5 4 3 
	    4 1 2 1 4 
	    4 1 4 1 4 
	    4 3 2 3 4 
	    4 3 4 3 4 
	    4 5 2 5 4 
	    4 5 4 5 4 
	    5 2 1 2 5 
	    5 2 3 2 5 
	    5 2 5 2 5 
	    5 4 1 4 5 
	    5 4 3 4 5 
	    5 4 5 4 5
	  ---------------
	  Combination of Even Odd 
	              or Odd Even
	*/
	task.palindrome(n);
}
main();

Output

 Given Length : 3
   1  2  1
   2  1  2
   2  3  2
   3  2  3

 Given Length : 5
   1  2  1  2  1
   1  2  3  2  1
   1  2  5  2  1
   1  4  1  4  1
   1  4  3  4  1
   1  4  5  4  1
   2  1  2  1  2
   2  1  4  1  2
   2  3  2  3  2
   2  3  4  3  2
   2  5  2  5  2
   2  5  4  5  2
   3  2  1  2  3
   3  2  3  2  3
   3  2  5  2  3
   3  4  1  4  3
   3  4  3  4  3
   3  4  5  4  3
   4  1  2  1  4
   4  1  4  1  4
   4  3  2  3  4
   4  3  4  3  4
   4  5  2  5  4
   4  5  4  5  4
   5  2  1  2  5
   5  2  3  2  5
   5  2  5  2  5
   5  4  1  4  5
   5  4  3  4  5
   5  4  5  4  5
#  Python 3 program for
#  Generate all palindromic subsequence of n natural number
#  Whose adjacent elements are different type
class Combination :
	#  Display calculated result
	def printResult(self, result, n) :
		i = 0
		while (i < n) :
			print("  ", result[i], end = "")
			i += 1
		
		print("\n ", end = "")
	
	def bothSimilar(self, a, b) :
		if ((a % 2 == 0 and b % 2 == 0) or
            (a % 2 == 1 and b % 2 == 1)) :
			return True
		
		return False
	
	def sequence(self, result, index, n) :
		if (index == int(n / 2)) :
			if ((n % 2) != 0) :
				i = 1
				#  Handles the request of odd length palindrome sequence
				while (i <= n) :
					if (not self.bothSimilar(result[index - 1], i)) :
						result[index] = i
						self.printResult(result, n)
					
					i += 1
				
			else :
				self.printResult(result, n)
			
		elif (index > int(n / 2)) :
			return
		else :
			i = 1
			while (i <= n) :
				if (index == 0 or(index > 0 and not 
                    self.bothSimilar(result[index - 1], i))) :
					#  Set palindrome elements
					result[index] = i
					result[(n - 1) - index] = i
					self.sequence(result, index + 1, n)
				
				i += 1
			
		
	
	def palindrome(self, n) :
		if (n <= 1 or n % 2 == 0) :
			return
		
		print("\n Given Length : ", n ," \n ", end = "")
		#  Auxiliary list which is collect result
		result = [0] * (n)
		self.sequence(result, 0, n)
	

def main() :
	task = Combination()
	n = 3
	#    n = 3
	#    Given Length : 3 
	#    ---------------
	#    1 2 1 
	#    2 1 2 
	#    2 3 2 
	#    3 2 3 
	#    ---------------
	task.palindrome(n)
	n = 5
	#    n = 5
	#    Given Length : 5 
	#  ----------------
	#    1 2 1 2 1 
	#    1 2 3 2 1 
	#    1 2 5 2 1 
	#    1 4 1 4 1 
	#    1 4 3 4 1 
	#    1 4 5 4 1 
	#    2 1 2 1 2 
	#    2 1 4 1 2 
	#    2 3 2 3 2 
	#    2 3 4 3 2 
	#    2 5 2 5 2 
	#    2 5 4 5 2 
	#    3 2 1 2 3 
	#    3 2 3 2 3 
	#    3 2 5 2 3 
	#    3 4 1 4 3 
	#    3 4 3 4 3 
	#    3 4 5 4 3 
	#    4 1 2 1 4 
	#    4 1 4 1 4 
	#    4 3 2 3 4 
	#    4 3 4 3 4 
	#    4 5 2 5 4 
	#    4 5 4 5 4 
	#    5 2 1 2 5 
	#    5 2 3 2 5 
	#    5 2 5 2 5 
	#    5 4 1 4 5 
	#    5 4 3 4 5 
	#    5 4 5 4 5
	#  ---------------
	#  Combination of Even Odd 
	#              or Odd Even
	task.palindrome(n)

if __name__ == "__main__": main()

Output

 Given Length :  3
    1   2   1
    2   1   2
    2   3   2
    3   2   3

 Given Length :  5
    1   2   1   2   1
    1   2   3   2   1
    1   2   5   2   1
    1   4   1   4   1
    1   4   3   4   1
    1   4   5   4   1
    2   1   2   1   2
    2   1   4   1   2
    2   3   2   3   2
    2   3   4   3   2
    2   5   2   5   2
    2   5   4   5   2
    3   2   1   2   3
    3   2   3   2   3
    3   2   5   2   3
    3   4   1   4   3
    3   4   3   4   3
    3   4   5   4   3
    4   1   2   1   4
    4   1   4   1   4
    4   3   2   3   4
    4   3   4   3   4
    4   5   2   5   4
    4   5   4   5   4
    5   2   1   2   5
    5   2   3   2   5
    5   2   5   2   5
    5   4   1   4   5
    5   4   3   4   5
    5   4   5   4   5
#  Ruby program for
#  Generate all palindromic subsequence of n natural number
#  Whose adjacent elements are different type
class Combination 
	#  Display calculated result
	def printResult(result, n) 
		i = 0
		while (i < n) 
			print("  ", result[i])
			i += 1
		end

		print("\n ")
	end

	def bothSimilar(a, b) 
		if ((a % 2 == 0 && b % 2 == 0) || 
            (a % 2 == 1 && b % 2 == 1)) 
			return true
		end

		return false
	end

	def sequence(result, index, n) 
		if (index == n / 2) 
			if ((n % 2) != 0) 
				i = 1
				#  Handles the request of odd length palindrome sequence
				while (i <= n) 
					if (!self.bothSimilar(result[index - 1], i)) 
						result[index] = i
						self.printResult(result, n)
					end

					i += 1
				end

			else
 
				self.printResult(result, n)
			end

		elsif (index > n / 2) 
			return
		else
 
			i = 1
			while (i <= n) 
				if (index == 0 || 
                    (index > 0 && !self.bothSimilar(result[index - 1], i))) 
					#  Set palindrome elements
					result[index] = i
					result[(n - 1) - index] = i
					self.sequence(result, index + 1, n)
				end

				i += 1
			end

		end

	end

	def palindrome(n) 
		if (n <= 1 || n % 2 == 0) 
			return
		end

		print("\n Given Length : ", n ," \n ")
		#  Auxiliary array which is collect result
		result = Array.new(n) {0}
		self.sequence(result, 0, n)
	end

end

def main() 
	task = Combination.new()
	n = 3
	#    n = 3
	#    Given Length : 3 
	#    ---------------
	#    1 2 1 
	#    2 1 2 
	#    2 3 2 
	#    3 2 3 
	#    ---------------
	task.palindrome(n)
	n = 5
	#    n = 5
	#    Given Length : 5 
	#  ----------------
	#    1 2 1 2 1 
	#    1 2 3 2 1 
	#    1 2 5 2 1 
	#    1 4 1 4 1 
	#    1 4 3 4 1 
	#    1 4 5 4 1 
	#    2 1 2 1 2 
	#    2 1 4 1 2 
	#    2 3 2 3 2 
	#    2 3 4 3 2 
	#    2 5 2 5 2 
	#    2 5 4 5 2 
	#    3 2 1 2 3 
	#    3 2 3 2 3 
	#    3 2 5 2 3 
	#    3 4 1 4 3 
	#    3 4 3 4 3 
	#    3 4 5 4 3 
	#    4 1 2 1 4 
	#    4 1 4 1 4 
	#    4 3 2 3 4 
	#    4 3 4 3 4 
	#    4 5 2 5 4 
	#    4 5 4 5 4 
	#    5 2 1 2 5 
	#    5 2 3 2 5 
	#    5 2 5 2 5 
	#    5 4 1 4 5 
	#    5 4 3 4 5 
	#    5 4 5 4 5
	#  ---------------
	#  Combination of Even Odd 
	#              or Odd Even
	task.palindrome(n)
end

main()

Output

 Given Length : 3 
   1  2  1
   2  1  2
   2  3  2
   3  2  3
 
 Given Length : 5 
   1  2  1  2  1
   1  2  3  2  1
   1  2  5  2  1
   1  4  1  4  1
   1  4  3  4  1
   1  4  5  4  1
   2  1  2  1  2
   2  1  4  1  2
   2  3  2  3  2
   2  3  4  3  2
   2  5  2  5  2
   2  5  4  5  2
   3  2  1  2  3
   3  2  3  2  3
   3  2  5  2  3
   3  4  1  4  3
   3  4  3  4  3
   3  4  5  4  3
   4  1  2  1  4
   4  1  4  1  4
   4  3  2  3  4
   4  3  4  3  4
   4  5  2  5  4
   4  5  4  5  4
   5  2  1  2  5
   5  2  3  2  5
   5  2  5  2  5
   5  4  1  4  5
   5  4  3  4  5
   5  4  5  4  5
 
// Scala program for
// Generate all palindromic subsequence of n natural number
// Whose adjacent elements are different type
class Combination()
{
	// Display calculated result
	def printResult(result: Array[Int], n: Int): Unit = {
		var i: Int = 0;
		while (i < n)
		{
			print("  " + result(i));
			i += 1;
		}
		print("\n ");
	}
	def bothSimilar(a: Int, b: Int): Boolean = {
		if ((a % 2 == 0 && b % 2 == 0) || 
            (a % 2 == 1 && b % 2 == 1))
		{
			return true;
		}
		return false;
	}
	def sequence(result: Array[Int], 
      	index: Int,
        n: Int): Unit = {
		if (index == n / 2)
		{
			if ((n % 2) != 0)
			{
				var i: Int = 1;
				// Handles the request of odd length palindrome sequence
				while (i <= n)
				{
					if (!bothSimilar(result(index - 1), i))
					{
						result(index) = i;
						printResult(result, n);
					}
					i += 1;
				}
			}
			else
			{
				printResult(result, n);
			}
		}
		else if (index > n / 2)
		{
			return;
		}
		else
		{
			var i: Int = 1;
			while (i <= n)
			{
				if (index == 0 || (index > 0 && 
                                   !bothSimilar(result(index - 1), i)))
				{
					// Set palindrome elements
					result(index) = i;
					result((n - 1) - index) = i;
					sequence(result, index + 1, n);
				}
				i += 1;
			}
		}
	}
	def palindrome(n: Int): Unit = {
		if (n <= 1 || n % 2 == 0)
		{
			return;
		}
		print("\n Given Length : " + n + " \n ");
		// Auxiliary array which is collect result
		var result: Array[Int] = Array.fill[Int](n)(0);
		sequence(result, 0, n);
	}
}
object Main
{
	def main(args: Array[String]): Unit = {
		var task: Combination = new Combination();
		var n: Int = 3;
		/*
		    n = 3
		    Given Length : 3 
		    ---------------

		    1 2 1 
		    2 1 2 
		    2 3 2 
		    3 2 3 
		    ---------------
		*/
		task.palindrome(n);
		n = 5;
		/*
		    n = 5
		    Given Length : 5 
		  ----------------
		  
		    1 2 1 2 1 
		    1 2 3 2 1 
		    1 2 5 2 1 
		    1 4 1 4 1 
		    1 4 3 4 1 
		    1 4 5 4 1 
		    2 1 2 1 2 
		    2 1 4 1 2 
		    2 3 2 3 2 
		    2 3 4 3 2 
		    2 5 2 5 2 
		    2 5 4 5 2 
		    3 2 1 2 3 
		    3 2 3 2 3 
		    3 2 5 2 3 
		    3 4 1 4 3 
		    3 4 3 4 3 
		    3 4 5 4 3 
		    4 1 2 1 4 
		    4 1 4 1 4 
		    4 3 2 3 4 
		    4 3 4 3 4 
		    4 5 2 5 4 
		    4 5 4 5 4 
		    5 2 1 2 5 
		    5 2 3 2 5 
		    5 2 5 2 5 
		    5 4 1 4 5 
		    5 4 3 4 5 
		    5 4 5 4 5
		  ---------------
		  Combination of Even Odd 
		              or Odd Even
		*/
		task.palindrome(n);
	}
}

Output

 Given Length : 3
   1  2  1
   2  1  2
   2  3  2
   3  2  3

 Given Length : 5
   1  2  1  2  1
   1  2  3  2  1
   1  2  5  2  1
   1  4  1  4  1
   1  4  3  4  1
   1  4  5  4  1
   2  1  2  1  2
   2  1  4  1  2
   2  3  2  3  2
   2  3  4  3  2
   2  5  2  5  2
   2  5  4  5  2
   3  2  1  2  3
   3  2  3  2  3
   3  2  5  2  3
   3  4  1  4  3
   3  4  3  4  3
   3  4  5  4  3
   4  1  2  1  4
   4  1  4  1  4
   4  3  2  3  4
   4  3  4  3  4
   4  5  2  5  4
   4  5  4  5  4
   5  2  1  2  5
   5  2  3  2  5
   5  2  5  2  5
   5  4  1  4  5
   5  4  3  4  5
   5  4  5  4  5
// Swift 4 program for
// Generate all palindromic subsequence of n natural number
// Whose adjacent elements are different type
class Combination
{
	// Display calculated result
	func printResult(_ result: [Int], _ n: Int)
	{
		var i: Int = 0;
		while (i < n)
		{
			print("  ", result[i], terminator: "");
			i += 1;
		}
		print("\n ", terminator: "");
	}
	func bothSimilar(_ a: Int, _ b: Int) -> Bool
	{
		if ((a % 2 == 0 && b % 2 == 0) || 
            (a % 2 == 1 && b % 2 == 1))
		{
			return true;
		}
		return false;
	}
	func sequence(
      _ result: inout[Int], 
      _ index: Int, 
      _ n: Int)
	{
		if (index == n / 2)
		{
			if ((n % 2)  != 0)
			{
				var i: Int = 1;
				// Handles the request of odd length palindrome sequence
				while (i <= n)
				{
					if (!self.bothSimilar(result[index - 1], i))
					{
						result[index] = i;
						self.printResult(result, n);
					}
					i += 1;
				}
			}
			else
			{
				self.printResult(result, n);
			}
		}
		else if (index > n / 2)
		{
			return;
		}
		else
		{
			var i: Int = 1;
			while (i <= n)
			{
				if (index == 0 || 
                    (index > 0 && 
                     !self.bothSimilar(result[index - 1], i)))
				{
					// Set palindrome elements
					result[index] = i;
					result[(n - 1) - index] = i;
					self.sequence(&result, index + 1, n);
				}
				i += 1;
			}
		}
	}
	func palindrome(_ n: Int)
	{
		if (n <= 1 || n % 2 == 0)
		{
			return;
		}
		print("\n Given Length : ", n ," \n ", terminator: "");
		// Auxiliary array which is collect result
		var result: [Int] = Array(repeating: 0, count: n);
		self.sequence(&result, 0, n);
	}
}
func main()
{
	let task: Combination = Combination();
	var n: Int = 3;
	/*
	    n = 3
	    Given Length : 3 
	    ---------------

	    1 2 1 
	    2 1 2 
	    2 3 2 
	    3 2 3 
	    ---------------
	*/
	task.palindrome(n);
	n = 5;
	/*
	    n = 5
	    Given Length : 5 
	  ----------------
	  
	    1 2 1 2 1 
	    1 2 3 2 1 
	    1 2 5 2 1 
	    1 4 1 4 1 
	    1 4 3 4 1 
	    1 4 5 4 1 
	    2 1 2 1 2 
	    2 1 4 1 2 
	    2 3 2 3 2 
	    2 3 4 3 2 
	    2 5 2 5 2 
	    2 5 4 5 2 
	    3 2 1 2 3 
	    3 2 3 2 3 
	    3 2 5 2 3 
	    3 4 1 4 3 
	    3 4 3 4 3 
	    3 4 5 4 3 
	    4 1 2 1 4 
	    4 1 4 1 4 
	    4 3 2 3 4 
	    4 3 4 3 4 
	    4 5 2 5 4 
	    4 5 4 5 4 
	    5 2 1 2 5 
	    5 2 3 2 5 
	    5 2 5 2 5 
	    5 4 1 4 5 
	    5 4 3 4 5 
	    5 4 5 4 5
	  ---------------
	  Combination of Even Odd 
	              or Odd Even
	*/
	task.palindrome(n);
}
main();

Output

 Given Length :  3
    1   2   1
    2   1   2
    2   3   2
    3   2   3

 Given Length :  5
    1   2   1   2   1
    1   2   3   2   1
    1   2   5   2   1
    1   4   1   4   1
    1   4   3   4   1
    1   4   5   4   1
    2   1   2   1   2
    2   1   4   1   2
    2   3   2   3   2
    2   3   4   3   2
    2   5   2   5   2
    2   5   4   5   2
    3   2   1   2   3
    3   2   3   2   3
    3   2   5   2   3
    3   4   1   4   3
    3   4   3   4   3
    3   4   5   4   3
    4   1   2   1   4
    4   1   4   1   4
    4   3   2   3   4
    4   3   4   3   4
    4   5   2   5   4
    4   5   4   5   4
    5   2   1   2   5
    5   2   3   2   5
    5   2   5   2   5
    5   4   1   4   5
    5   4   3   4   5
    5   4   5   4   5
// Kotlin program for
// Generate all palindromic subsequence of n natural number
// Whose adjacent elements are different type
class Combination
{
	// Display calculated result
	fun printResult(result: Array < Int > , n: Int): Unit
	{
		var i: Int = 0;
		while (i < n)
		{
			print("  " + result[i]);
			i += 1;
		}
		print("\n ");
	}
	fun bothSimilar(a: Int, b: Int): Boolean
	{
		if ((a % 2 == 0 && b % 2 == 0) || 
            (a % 2 == 1 && b % 2 == 1))
		{
			return true;
		}
		return false;
	}
	fun sequence(result: Array < Int > , 
                 index: Int, n: Int): Unit
	{
		if (index == n / 2)
		{
			if ((n % 2) != 0)
			{
				var i: Int = 1;
				// Handles the request of odd length palindrome sequence
				while (i <= n)
				{
					if (!this.bothSimilar(result[index - 1], i))
					{
						result[index] = i;
						this.printResult(result, n);
					}
					i += 1;
				}
			}
			else
			{
				this.printResult(result, n);
			}
		}
		else if (index > n / 2)
		{
			return;
		}
		else
		{
			var i: Int = 1;
			while (i <= n)
			{
				if (index == 0 || 
                    (index > 0 && 
                     !this.bothSimilar(result[index - 1], i)))
				{
					// Set palindrome elements
					result[index] = i;
					result[(n - 1) - index] = i;
					this.sequence(result, index + 1, n);
				}
				i += 1;
			}
		}
	}
	fun palindrome(n: Int): Unit
	{
		if (n <= 1 || n % 2 == 0)
		{
			return;
		}
		print("\n Given Length : " + n + " \n ");
		// Auxiliary array which is collect result
		val result: Array < Int > = Array(n)
		{
			0
		};
		this.sequence(result, 0, n);
	}
}
fun main(args: Array < String > ): Unit
{
	val task: Combination = Combination();
	var n: Int = 3;
	/*
	    n = 3
	    Given Length : 3 
	    ---------------

	    1 2 1 
	    2 1 2 
	    2 3 2 
	    3 2 3 
	    ---------------
	*/
	task.palindrome(n);
	n = 5;
	/*
	    n = 5
	    Given Length : 5 
	  ----------------
	  
	    1 2 1 2 1 
	    1 2 3 2 1 
	    1 2 5 2 1 
	    1 4 1 4 1 
	    1 4 3 4 1 
	    1 4 5 4 1 
	    2 1 2 1 2 
	    2 1 4 1 2 
	    2 3 2 3 2 
	    2 3 4 3 2 
	    2 5 2 5 2 
	    2 5 4 5 2 
	    3 2 1 2 3 
	    3 2 3 2 3 
	    3 2 5 2 3 
	    3 4 1 4 3 
	    3 4 3 4 3 
	    3 4 5 4 3 
	    4 1 2 1 4 
	    4 1 4 1 4 
	    4 3 2 3 4 
	    4 3 4 3 4 
	    4 5 2 5 4 
	    4 5 4 5 4 
	    5 2 1 2 5 
	    5 2 3 2 5 
	    5 2 5 2 5 
	    5 4 1 4 5 
	    5 4 3 4 5 
	    5 4 5 4 5
	  ---------------
	  Combination of Even Odd 
	              or Odd Even
	*/
	task.palindrome(n);
}

Output

 Given Length : 3
   1  2  1
   2  1  2
   2  3  2
   3  2  3

 Given Length : 5
   1  2  1  2  1
   1  2  3  2  1
   1  2  5  2  1
   1  4  1  4  1
   1  4  3  4  1
   1  4  5  4  1
   2  1  2  1  2
   2  1  4  1  2
   2  3  2  3  2
   2  3  4  3  2
   2  5  2  5  2
   2  5  4  5  2
   3  2  1  2  3
   3  2  3  2  3
   3  2  5  2  3
   3  4  1  4  3
   3  4  3  4  3
   3  4  5  4  3
   4  1  2  1  4
   4  1  4  1  4
   4  3  2  3  4
   4  3  4  3  4
   4  5  2  5  4
   4  5  4  5  4
   5  2  1  2  5
   5  2  3  2  5
   5  2  5  2  5
   5  4  1  4  5
   5  4  3  4  5
   5  4  5  4  5


Please share your knowledge to improve code and content standard. Also submit your doubts, and test case. We improve by your feedback. We will try to resolve your query as soon as possible.

New Comment







© 2021, kalkicode.com, All rights reserved