Generate all palindromic subsequence of length N and K Even natural number
Here given code implementation process.
// C Program
// Generate all palindromic subsequence of
// length N and K Even natural number
#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 ");
}
void sequence(int result[], int index, int n, int k)
{
if (index == n / 2)
{
if ((n % 2) != 0)
{
// Handles the request of odd length palindrome sequence
for (int i = 2; i <= k *2; i += 2)
{
result[index] = i;
printResult(result, n);
}
}
else
{
printResult(result, n);
}
}
else if (index > n / 2)
{
return;
}
else
{
for (int i = 2; i <= k *2; i += 2)
{
// Set the value of boundary element is to zero
result[index] = i;
result[(n - 1) - index] = i;
sequence(result, index + 1, n, k);
}
}
}
void palindrome(int n, int k)
{
if (n <= 0 || k > n)
{
return;
}
printf("\n Given Length : %d", n);
printf("\n K : %d", k);
printf("\n Initial K Even Number [");
int i = 2;
while (i <= k *2)
{
printf(" %d ", i);
i += 2;
}
printf("]\n ");
// Auxiliary array which is collect result
int result[n];
sequence(result, 0, n, k);
}
int main()
{
int n = 6;
int k = 3;
/*
n = 6
k = 3
[2,4,6] 3 Even natural number
-------------------------------
2 2 2 2 2 2
2 2 4 4 2 2
2 2 6 6 2 2
2 4 2 2 4 2
2 4 4 4 4 2
2 4 6 6 4 2
2 6 2 2 6 2
2 6 4 4 6 2
2 6 6 6 6 2
4 2 2 2 2 4
4 2 4 4 2 4
4 2 6 6 2 4
4 4 2 2 4 4
4 4 4 4 4 4
4 4 6 6 4 4
4 6 2 2 6 4
4 6 4 4 6 4
4 6 6 6 6 4
6 2 2 2 2 6
6 2 4 4 2 6
6 2 6 6 2 6
6 4 2 2 4 6
6 4 4 4 4 6
6 4 6 6 4 6
6 6 2 2 6 6
6 6 4 4 6 6
6 6 6 6 6 6
---------------
*/
palindrome(n, k);
return 0;
}
Output
Given Length : 6
K : 3
Initial K Even Number [ 2 4 6 ]
2 2 2 2 2 2
2 2 4 4 2 2
2 2 6 6 2 2
2 4 2 2 4 2
2 4 4 4 4 2
2 4 6 6 4 2
2 6 2 2 6 2
2 6 4 4 6 2
2 6 6 6 6 2
4 2 2 2 2 4
4 2 4 4 2 4
4 2 6 6 2 4
4 4 2 2 4 4
4 4 4 4 4 4
4 4 6 6 4 4
4 6 2 2 6 4
4 6 4 4 6 4
4 6 6 6 6 4
6 2 2 2 2 6
6 2 4 4 2 6
6 2 6 6 2 6
6 4 2 2 4 6
6 4 4 4 4 6
6 4 6 6 4 6
6 6 2 2 6 6
6 6 4 4 6 6
6 6 6 6 6 6
// Java program for
// Generate all palindromic subsequence of
// length N and K Even natural number
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 void sequence(int[] result, int index, int n, int k)
{
if (index == n / 2)
{
if ((n % 2) != 0)
{
// Handles the request of odd length palindrome sequence
for (int i = 2; i <= k * 2; i += 2)
{
result[index] = i;
printResult(result, n);
}
}
else
{
printResult(result, n);
}
}
else if (index > n / 2)
{
return;
}
else
{
for (int i = 2; i <= k * 2; i += 2)
{
// Set the value of boundary element is to zero
result[index] = i;
result[(n - 1) - index] = i;
sequence(result, index + 1, n, k);
}
}
}
public void palindrome(int n, int k)
{
if (n <= 0 || k > n)
{
return;
}
System.out.print("\n Given Length : " + n);
System.out.print("\n K : " + k);
System.out.print("\n Initial K Even Number [");
int i = 2;
while (i <= k * 2)
{
System.out.print(" " + i + " ");
i += 2;
}
System.out.print("]\n ");
// Auxiliary array which is collect result
int[] result = new int[n];
sequence(result, 0, n, k);
}
public static void main(String[] args)
{
Combination task = new Combination();
int n = 6;
int k = 3;
/*
n = 6
k = 3
[2,4,6] 3 Even natural number
----------------------
2 2 2 2 2 2
2 2 4 4 2 2
2 2 6 6 2 2
2 4 2 2 4 2
2 4 4 4 4 2
2 4 6 6 4 2
2 6 2 2 6 2
2 6 4 4 6 2
2 6 6 6 6 2
4 2 2 2 2 4
4 2 4 4 2 4
4 2 6 6 2 4
4 4 2 2 4 4
4 4 4 4 4 4
4 4 6 6 4 4
4 6 2 2 6 4
4 6 4 4 6 4
4 6 6 6 6 4
6 2 2 2 2 6
6 2 4 4 2 6
6 2 6 6 2 6
6 4 2 2 4 6
6 4 4 4 4 6
6 4 6 6 4 6
6 6 2 2 6 6
6 6 4 4 6 6
6 6 6 6 6 6
---------------
*/
task.palindrome(n, k);
}
}
Output
Given Length : 6
K : 3
Initial K Even Number [ 2 4 6 ]
2 2 2 2 2 2
2 2 4 4 2 2
2 2 6 6 2 2
2 4 2 2 4 2
2 4 4 4 4 2
2 4 6 6 4 2
2 6 2 2 6 2
2 6 4 4 6 2
2 6 6 6 6 2
4 2 2 2 2 4
4 2 4 4 2 4
4 2 6 6 2 4
4 4 2 2 4 4
4 4 4 4 4 4
4 4 6 6 4 4
4 6 2 2 6 4
4 6 4 4 6 4
4 6 6 6 6 4
6 2 2 2 2 6
6 2 4 4 2 6
6 2 6 6 2 6
6 4 2 2 4 6
6 4 4 4 4 6
6 4 6 6 4 6
6 6 2 2 6 6
6 6 4 4 6 6
6 6 6 6 6 6
// Include header file
#include <iostream>
using namespace std;
// C++ program for
// Generate all palindromic subsequence of
// length N and K Even natural number
class Combination
{
public:
// Display calculated result
void printResult(int result[], int n)
{
for (int i = 0; i < n; i++)
{
cout << " " << result[i];
}
cout << "\n ";
}
void sequence(int result[], int index, int n, int k)
{
if (index == n / 2)
{
if ((n % 2) != 0)
{
// Handles the request of odd length palindrome sequence
for (int i = 2; i <= k *2; i += 2)
{
result[index] = i;
this->printResult(result, n);
}
}
else
{
this->printResult(result, n);
}
}
else if (index > n / 2)
{
return;
}
else
{
for (int i = 2; i <= k *2; i += 2)
{
// Set the value of boundary element is to zero
result[index] = i;
result[(n - 1) - index] = i;
this->sequence(result, index + 1, n, k);
}
}
}
void palindrome(int n, int k)
{
if (n <= 0 || k > n)
{
return;
}
cout << "\n Given Length : " << n;
cout << "\n K : " << k;
cout << "\n Initial K Even Number [";
int i = 2;
while (i <= k *2)
{
cout << " " << i << " ";
i += 2;
}
cout << "]\n ";
// Auxiliary array which is collect result
int result[n];
this->sequence(result, 0, n, k);
}
};
int main()
{
Combination *task = new Combination();
int n = 6;
int k = 3;
/*
n = 6
k = 3
[2,4,6] 3 Even natural number
----------------------
2 2 2 2 2 2
2 2 4 4 2 2
2 2 6 6 2 2
2 4 2 2 4 2
2 4 4 4 4 2
2 4 6 6 4 2
2 6 2 2 6 2
2 6 4 4 6 2
2 6 6 6 6 2
4 2 2 2 2 4
4 2 4 4 2 4
4 2 6 6 2 4
4 4 2 2 4 4
4 4 4 4 4 4
4 4 6 6 4 4
4 6 2 2 6 4
4 6 4 4 6 4
4 6 6 6 6 4
6 2 2 2 2 6
6 2 4 4 2 6
6 2 6 6 2 6
6 4 2 2 4 6
6 4 4 4 4 6
6 4 6 6 4 6
6 6 2 2 6 6
6 6 4 4 6 6
6 6 6 6 6 6
---------------
*/
task->palindrome(n, k);
return 0;
}
Output
Given Length : 6
K : 3
Initial K Even Number [ 2 4 6 ]
2 2 2 2 2 2
2 2 4 4 2 2
2 2 6 6 2 2
2 4 2 2 4 2
2 4 4 4 4 2
2 4 6 6 4 2
2 6 2 2 6 2
2 6 4 4 6 2
2 6 6 6 6 2
4 2 2 2 2 4
4 2 4 4 2 4
4 2 6 6 2 4
4 4 2 2 4 4
4 4 4 4 4 4
4 4 6 6 4 4
4 6 2 2 6 4
4 6 4 4 6 4
4 6 6 6 6 4
6 2 2 2 2 6
6 2 4 4 2 6
6 2 6 6 2 6
6 4 2 2 4 6
6 4 4 4 4 6
6 4 6 6 4 6
6 6 2 2 6 6
6 6 4 4 6 6
6 6 6 6 6 6
// Include namespace system
using System;
// Csharp program for
// Generate all palindromic subsequence of
// length N and K Even natural number
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 void sequence(int[] result, int index, int n, int k)
{
if (index == n / 2)
{
if ((n % 2) != 0)
{
// Handles the request of odd length palindrome sequence
for (int i = 2; i <= k * 2; i += 2)
{
result[index] = i;
this.printResult(result, n);
}
}
else
{
this.printResult(result, n);
}
}
else if (index > n / 2)
{
return;
}
else
{
for (int i = 2; i <= k * 2; i += 2)
{
// Set the value of boundary element is to zero
result[index] = i;
result[(n - 1) - index] = i;
this.sequence(result, index + 1, n, k);
}
}
}
public void palindrome(int n, int k)
{
if (n <= 0 || k > n)
{
return;
}
Console.Write("\n Given Length : " + n);
Console.Write("\n K : " + k);
Console.Write("\n Initial K Even Number [");
int i = 2;
while (i <= k * 2)
{
Console.Write(" " + i + " ");
i += 2;
}
Console.Write("]\n ");
// Auxiliary array which is collect result
int[] result = new int[n];
this.sequence(result, 0, n, k);
}
public static void Main(String[] args)
{
Combination task = new Combination();
int n = 6;
int k = 3;
/*
n = 6
k = 3
[2,4,6] 3 Even natural number
----------------------
2 2 2 2 2 2
2 2 4 4 2 2
2 2 6 6 2 2
2 4 2 2 4 2
2 4 4 4 4 2
2 4 6 6 4 2
2 6 2 2 6 2
2 6 4 4 6 2
2 6 6 6 6 2
4 2 2 2 2 4
4 2 4 4 2 4
4 2 6 6 2 4
4 4 2 2 4 4
4 4 4 4 4 4
4 4 6 6 4 4
4 6 2 2 6 4
4 6 4 4 6 4
4 6 6 6 6 4
6 2 2 2 2 6
6 2 4 4 2 6
6 2 6 6 2 6
6 4 2 2 4 6
6 4 4 4 4 6
6 4 6 6 4 6
6 6 2 2 6 6
6 6 4 4 6 6
6 6 6 6 6 6
---------------
*/
task.palindrome(n, k);
}
}
Output
Given Length : 6
K : 3
Initial K Even Number [ 2 4 6 ]
2 2 2 2 2 2
2 2 4 4 2 2
2 2 6 6 2 2
2 4 2 2 4 2
2 4 4 4 4 2
2 4 6 6 4 2
2 6 2 2 6 2
2 6 4 4 6 2
2 6 6 6 6 2
4 2 2 2 2 4
4 2 4 4 2 4
4 2 6 6 2 4
4 4 2 2 4 4
4 4 4 4 4 4
4 4 6 6 4 4
4 6 2 2 6 4
4 6 4 4 6 4
4 6 6 6 6 4
6 2 2 2 2 6
6 2 4 4 2 6
6 2 6 6 2 6
6 4 2 2 4 6
6 4 4 4 4 6
6 4 6 6 4 6
6 6 2 2 6 6
6 6 4 4 6 6
6 6 6 6 6 6
package main
import "fmt"
// Go program for
// Generate all palindromic subsequence of
// length N and K Even natural number
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) sequence(result[] int,
index int, n int, k int) {
if index == n / 2 {
if (n % 2) != 0 {
// Handles the request of odd length palindrome sequence
for i := 2 ; i <= k * 2 ; i += 2 {
result[index] = i
this.printResult(result, n)
}
} else {
this.printResult(result, n)
}
} else if index > n / 2 {
return
} else {
for i := 2 ; i <= k * 2 ; i += 2 {
// Set the value of boundary element is to zero
result[index] = i
result[(n - 1) - index] = i
this.sequence(result, index + 1, n, k)
}
}
}
func(this Combination) palindrome(n, k int) {
if n <= 0 || k > n {
return
}
fmt.Print("\n Given Length : ", n)
fmt.Print("\n K : ", k)
fmt.Print("\n Initial K Even Number [")
var i int = 2
for (i <= k * 2) {
fmt.Print(" ", i, " ")
i += 2
}
fmt.Print("]\n ")
// Auxiliary array which is collect result
var result = make([] int, n)
this.sequence(result, 0, n, k)
}
func main() {
var task * Combination = getCombination()
var n int = 6
var k int = 3
/*
n = 6
k = 3
[2,4,6] 3 Even natural number
----------------------
2 2 2 2 2 2
2 2 4 4 2 2
2 2 6 6 2 2
2 4 2 2 4 2
2 4 4 4 4 2
2 4 6 6 4 2
2 6 2 2 6 2
2 6 4 4 6 2
2 6 6 6 6 2
4 2 2 2 2 4
4 2 4 4 2 4
4 2 6 6 2 4
4 4 2 2 4 4
4 4 4 4 4 4
4 4 6 6 4 4
4 6 2 2 6 4
4 6 4 4 6 4
4 6 6 6 6 4
6 2 2 2 2 6
6 2 4 4 2 6
6 2 6 6 2 6
6 4 2 2 4 6
6 4 4 4 4 6
6 4 6 6 4 6
6 6 2 2 6 6
6 6 4 4 6 6
6 6 6 6 6 6
---------------
*/
task.palindrome(n, k)
}
Output
Given Length : 6
K : 3
Initial K Even Number [ 2 4 6 ]
2 2 2 2 2 2
2 2 4 4 2 2
2 2 6 6 2 2
2 4 2 2 4 2
2 4 4 4 4 2
2 4 6 6 4 2
2 6 2 2 6 2
2 6 4 4 6 2
2 6 6 6 6 2
4 2 2 2 2 4
4 2 4 4 2 4
4 2 6 6 2 4
4 4 2 2 4 4
4 4 4 4 4 4
4 4 6 6 4 4
4 6 2 2 6 4
4 6 4 4 6 4
4 6 6 6 6 4
6 2 2 2 2 6
6 2 4 4 2 6
6 2 6 6 2 6
6 4 2 2 4 6
6 4 4 4 4 6
6 4 6 6 4 6
6 6 2 2 6 6
6 6 4 4 6 6
6 6 6 6 6 6
<?php
// Php program for
// Generate all palindromic subsequence of
// length N and K Even natural number
class Combination
{
// Display calculated result
public function printResult($result, $n)
{
for ($i = 0; $i < $n; $i++)
{
echo(" ".$result[$i]);
}
echo("\n ");
}
public function sequence($result, $index, $n, $k)
{
if ($index == (int)($n / 2))
{
if (($n % 2) != 0)
{
// Handles the request of odd length palindrome sequence
for ($i = 2; $i <= $k * 2; $i += 2)
{
$result[$index] = $i;
$this->printResult($result, $n);
}
}
else
{
$this->printResult($result, $n);
}
}
else if ($index > (int)($n / 2))
{
return;
}
else
{
for ($i = 2; $i <= $k * 2; $i += 2)
{
// Set the value of boundary element is to zero
$result[$index] = $i;
$result[($n - 1) - $index] = $i;
$this->sequence($result, $index + 1, $n, $k);
}
}
}
public function palindrome($n, $k)
{
if ($n <= 0 || $k > $n)
{
return;
}
echo("\n Given Length : ".$n);
echo("\n K : ".$k);
echo("\n Initial K Even Number [");
$i = 2;
while ($i <= $k * 2)
{
echo(" ".$i.
" ");
$i += 2;
}
echo("]\n ");
// Auxiliary array which is collect result
$result = array_fill(0, $n, 0);
$this->sequence($result, 0, $n, $k);
}
}
function main()
{
$task = new Combination();
$n = 6;
$k = 3;
/*
n = 6
k = 3
[2,4,6] 3 Even natural number
----------------------
2 2 2 2 2 2
2 2 4 4 2 2
2 2 6 6 2 2
2 4 2 2 4 2
2 4 4 4 4 2
2 4 6 6 4 2
2 6 2 2 6 2
2 6 4 4 6 2
2 6 6 6 6 2
4 2 2 2 2 4
4 2 4 4 2 4
4 2 6 6 2 4
4 4 2 2 4 4
4 4 4 4 4 4
4 4 6 6 4 4
4 6 2 2 6 4
4 6 4 4 6 4
4 6 6 6 6 4
6 2 2 2 2 6
6 2 4 4 2 6
6 2 6 6 2 6
6 4 2 2 4 6
6 4 4 4 4 6
6 4 6 6 4 6
6 6 2 2 6 6
6 6 4 4 6 6
6 6 6 6 6 6
---------------
*/
$task->palindrome($n, $k);
}
main();
Output
Given Length : 6
K : 3
Initial K Even Number [ 2 4 6 ]
2 2 2 2 2 2
2 2 4 4 2 2
2 2 6 6 2 2
2 4 2 2 4 2
2 4 4 4 4 2
2 4 6 6 4 2
2 6 2 2 6 2
2 6 4 4 6 2
2 6 6 6 6 2
4 2 2 2 2 4
4 2 4 4 2 4
4 2 6 6 2 4
4 4 2 2 4 4
4 4 4 4 4 4
4 4 6 6 4 4
4 6 2 2 6 4
4 6 4 4 6 4
4 6 6 6 6 4
6 2 2 2 2 6
6 2 4 4 2 6
6 2 6 6 2 6
6 4 2 2 4 6
6 4 4 4 4 6
6 4 6 6 4 6
6 6 2 2 6 6
6 6 4 4 6 6
6 6 6 6 6 6
// Node JS program for
// Generate all palindromic subsequence of
// length N and K Even natural number
class Combination
{
// Display calculated result
printResult(result, n)
{
for (var i = 0; i < n; i++)
{
process.stdout.write(" " + result[i]);
}
process.stdout.write("\n ");
}
sequence(result, index, n, k)
{
if (index == parseInt(n / 2))
{
if ((n % 2) != 0)
{
// Handles the request of odd length palindrome sequence
for (var i = 2; i <= k * 2; i += 2)
{
result[index] = i;
this.printResult(result, n);
}
}
else
{
this.printResult(result, n);
}
}
else if (index > parseInt(n / 2))
{
return;
}
else
{
for (var i = 2; i <= k * 2; i += 2)
{
// Set the value of boundary element is to zero
result[index] = i;
result[(n - 1) - index] = i;
this.sequence(result, index + 1, n, k);
}
}
}
palindrome(n, k)
{
if (n <= 0 || k > n)
{
return;
}
process.stdout.write("\n Given Length : " + n);
process.stdout.write("\n K : " + k);
process.stdout.write("\n Initial K Even Number [");
var i = 2;
while (i <= k * 2)
{
process.stdout.write(" " + i + " ");
i += 2;
}
process.stdout.write("]\n ");
// Auxiliary array which is collect result
var result = Array(n).fill(0);
this.sequence(result, 0, n, k);
}
}
function main()
{
var task = new Combination();
var n = 6;
var k = 3;
/*
n = 6
k = 3
[2,4,6] 3 Even natural number
----------------------
2 2 2 2 2 2
2 2 4 4 2 2
2 2 6 6 2 2
2 4 2 2 4 2
2 4 4 4 4 2
2 4 6 6 4 2
2 6 2 2 6 2
2 6 4 4 6 2
2 6 6 6 6 2
4 2 2 2 2 4
4 2 4 4 2 4
4 2 6 6 2 4
4 4 2 2 4 4
4 4 4 4 4 4
4 4 6 6 4 4
4 6 2 2 6 4
4 6 4 4 6 4
4 6 6 6 6 4
6 2 2 2 2 6
6 2 4 4 2 6
6 2 6 6 2 6
6 4 2 2 4 6
6 4 4 4 4 6
6 4 6 6 4 6
6 6 2 2 6 6
6 6 4 4 6 6
6 6 6 6 6 6
---------------
*/
task.palindrome(n, k);
}
main();
Output
Given Length : 6
K : 3
Initial K Even Number [ 2 4 6 ]
2 2 2 2 2 2
2 2 4 4 2 2
2 2 6 6 2 2
2 4 2 2 4 2
2 4 4 4 4 2
2 4 6 6 4 2
2 6 2 2 6 2
2 6 4 4 6 2
2 6 6 6 6 2
4 2 2 2 2 4
4 2 4 4 2 4
4 2 6 6 2 4
4 4 2 2 4 4
4 4 4 4 4 4
4 4 6 6 4 4
4 6 2 2 6 4
4 6 4 4 6 4
4 6 6 6 6 4
6 2 2 2 2 6
6 2 4 4 2 6
6 2 6 6 2 6
6 4 2 2 4 6
6 4 4 4 4 6
6 4 6 6 4 6
6 6 2 2 6 6
6 6 4 4 6 6
6 6 6 6 6 6
# Python 3 program for
# Generate all palindromic subsequence of
# length N and K Even natural number
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 sequence(self, result, index, n, k) :
if (index == int(n / 2)) :
if ((n % 2) != 0) :
i = 2
# Handles the request of odd length palindrome sequence
while (i <= k * 2) :
result[index] = i
self.printResult(result, n)
i += 2
else :
self.printResult(result, n)
elif (index > int(n / 2)) :
return
else :
i = 2
while (i <= k * 2) :
# Set the value of boundary element is to zero
result[index] = i
result[(n - 1) - index] = i
self.sequence(result, index + 1, n, k)
i += 2
def palindrome(self, n, k) :
if (n <= 0 or k > n) :
return
print("\n Given Length : ", n, end = "")
print("\n K : ", k, end = "")
print("\n Initial K Even Number [", end = "")
i = 2
while (i <= k * 2) :
print(" ", i ," ", end = "")
i += 2
print("]\n ", end = "")
# Auxiliary list which is collect result
result = [0] * (n)
self.sequence(result, 0, n, k)
def main() :
task = Combination()
n = 6
k = 3
# n = 6
# k = 3
# [2,4,6] 3 Even natural number
# ----------------------
# 2 2 2 2 2 2
# 2 2 4 4 2 2
# 2 2 6 6 2 2
# 2 4 2 2 4 2
# 2 4 4 4 4 2
# 2 4 6 6 4 2
# 2 6 2 2 6 2
# 2 6 4 4 6 2
# 2 6 6 6 6 2
# 4 2 2 2 2 4
# 4 2 4 4 2 4
# 4 2 6 6 2 4
# 4 4 2 2 4 4
# 4 4 4 4 4 4
# 4 4 6 6 4 4
# 4 6 2 2 6 4
# 4 6 4 4 6 4
# 4 6 6 6 6 4
# 6 2 2 2 2 6
# 6 2 4 4 2 6
# 6 2 6 6 2 6
# 6 4 2 2 4 6
# 6 4 4 4 4 6
# 6 4 6 6 4 6
# 6 6 2 2 6 6
# 6 6 4 4 6 6
# 6 6 6 6 6 6
# ---------------
task.palindrome(n, k)
if __name__ == "__main__": main()
Output
Given Length : 6
K : 3
Initial K Even Number [ 2 4 6 ]
2 2 2 2 2 2
2 2 4 4 2 2
2 2 6 6 2 2
2 4 2 2 4 2
2 4 4 4 4 2
2 4 6 6 4 2
2 6 2 2 6 2
2 6 4 4 6 2
2 6 6 6 6 2
4 2 2 2 2 4
4 2 4 4 2 4
4 2 6 6 2 4
4 4 2 2 4 4
4 4 4 4 4 4
4 4 6 6 4 4
4 6 2 2 6 4
4 6 4 4 6 4
4 6 6 6 6 4
6 2 2 2 2 6
6 2 4 4 2 6
6 2 6 6 2 6
6 4 2 2 4 6
6 4 4 4 4 6
6 4 6 6 4 6
6 6 2 2 6 6
6 6 4 4 6 6
6 6 6 6 6 6
# Ruby program for
# Generate all palindromic subsequence of
# length N and K Even natural number
class Combination
# Display calculated result
def printResult(result, n)
i = 0
while (i < n)
print(" ", result[i])
i += 1
end
print("\n ")
end
def sequence(result, index, n, k)
if (index == n / 2)
if ((n % 2) != 0)
i = 2
# Handles the request of odd length palindrome sequence
while (i <= k * 2)
result[index] = i
self.printResult(result, n)
i += 2
end
else
self.printResult(result, n)
end
elsif (index > n / 2)
return
else
i = 2
while (i <= k * 2)
# Set the value of boundary element is to zero
result[index] = i
result[(n - 1) - index] = i
self.sequence(result, index + 1, n, k)
i += 2
end
end
end
def palindrome(n, k)
if (n <= 0 || k > n)
return
end
print("\n Given Length : ", n)
print("\n K : ", k)
print("\n Initial K Even Number [")
i = 2
while (i <= k * 2)
print(" ", i ," ")
i += 2
end
print("]\n ")
# Auxiliary array which is collect result
result = Array.new(n) {0}
self.sequence(result, 0, n, k)
end
end
def main()
task = Combination.new()
n = 6
k = 3
# n = 6
# k = 3
# [2,4,6] 3 Even natural number
# ----------------------
# 2 2 2 2 2 2
# 2 2 4 4 2 2
# 2 2 6 6 2 2
# 2 4 2 2 4 2
# 2 4 4 4 4 2
# 2 4 6 6 4 2
# 2 6 2 2 6 2
# 2 6 4 4 6 2
# 2 6 6 6 6 2
# 4 2 2 2 2 4
# 4 2 4 4 2 4
# 4 2 6 6 2 4
# 4 4 2 2 4 4
# 4 4 4 4 4 4
# 4 4 6 6 4 4
# 4 6 2 2 6 4
# 4 6 4 4 6 4
# 4 6 6 6 6 4
# 6 2 2 2 2 6
# 6 2 4 4 2 6
# 6 2 6 6 2 6
# 6 4 2 2 4 6
# 6 4 4 4 4 6
# 6 4 6 6 4 6
# 6 6 2 2 6 6
# 6 6 4 4 6 6
# 6 6 6 6 6 6
# ---------------
task.palindrome(n, k)
end
main()
Output
Given Length : 6
K : 3
Initial K Even Number [ 2 4 6 ]
2 2 2 2 2 2
2 2 4 4 2 2
2 2 6 6 2 2
2 4 2 2 4 2
2 4 4 4 4 2
2 4 6 6 4 2
2 6 2 2 6 2
2 6 4 4 6 2
2 6 6 6 6 2
4 2 2 2 2 4
4 2 4 4 2 4
4 2 6 6 2 4
4 4 2 2 4 4
4 4 4 4 4 4
4 4 6 6 4 4
4 6 2 2 6 4
4 6 4 4 6 4
4 6 6 6 6 4
6 2 2 2 2 6
6 2 4 4 2 6
6 2 6 6 2 6
6 4 2 2 4 6
6 4 4 4 4 6
6 4 6 6 4 6
6 6 2 2 6 6
6 6 4 4 6 6
6 6 6 6 6 6
// Scala program for
// Generate all palindromic subsequence of
// length N and K Even natural number
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 sequence(result: Array[Int],
index: Int,
n: Int,
k: Int): Unit = {
if (index == n / 2)
{
if ((n % 2) != 0)
{
var i: Int = 2;
// Handles the request of odd length palindrome sequence
while (i <= k * 2)
{
result(index) = i;
printResult(result, n);
i += 2;
}
}
else
{
printResult(result, n);
}
}
else if (index > n / 2)
{
return;
}
else
{
var i: Int = 2;
while (i <= k * 2)
{
// Set the value of boundary element is to zero
result(index) = i;
result((n - 1) - index) = i;
sequence(result, index + 1, n, k);
i += 2;
}
}
}
def palindrome(n: Int, k: Int): Unit = {
if (n <= 0 || k > n)
{
return;
}
print("\n Given Length : " + n);
print("\n K : " + k);
print("\n Initial K Even Number [");
var i: Int = 2;
while (i <= k * 2)
{
print(" " + i + " ");
i += 2;
}
print("]\n ");
// Auxiliary array which is collect result
var result: Array[Int] = Array.fill[Int](n)(0);
sequence(result, 0, n, k);
}
}
object Main
{
def main(args: Array[String]): Unit = {
var task: Combination = new Combination();
var n: Int = 6;
var k: Int = 3;
/*
n = 6
k = 3
[2,4,6] 3 Even natural number
----------------------
2 2 2 2 2 2
2 2 4 4 2 2
2 2 6 6 2 2
2 4 2 2 4 2
2 4 4 4 4 2
2 4 6 6 4 2
2 6 2 2 6 2
2 6 4 4 6 2
2 6 6 6 6 2
4 2 2 2 2 4
4 2 4 4 2 4
4 2 6 6 2 4
4 4 2 2 4 4
4 4 4 4 4 4
4 4 6 6 4 4
4 6 2 2 6 4
4 6 4 4 6 4
4 6 6 6 6 4
6 2 2 2 2 6
6 2 4 4 2 6
6 2 6 6 2 6
6 4 2 2 4 6
6 4 4 4 4 6
6 4 6 6 4 6
6 6 2 2 6 6
6 6 4 4 6 6
6 6 6 6 6 6
---------------
*/
task.palindrome(n, k);
}
}
Output
Given Length : 6
K : 3
Initial K Even Number [ 2 4 6 ]
2 2 2 2 2 2
2 2 4 4 2 2
2 2 6 6 2 2
2 4 2 2 4 2
2 4 4 4 4 2
2 4 6 6 4 2
2 6 2 2 6 2
2 6 4 4 6 2
2 6 6 6 6 2
4 2 2 2 2 4
4 2 4 4 2 4
4 2 6 6 2 4
4 4 2 2 4 4
4 4 4 4 4 4
4 4 6 6 4 4
4 6 2 2 6 4
4 6 4 4 6 4
4 6 6 6 6 4
6 2 2 2 2 6
6 2 4 4 2 6
6 2 6 6 2 6
6 4 2 2 4 6
6 4 4 4 4 6
6 4 6 6 4 6
6 6 2 2 6 6
6 6 4 4 6 6
6 6 6 6 6 6
// Swift 4 program for
// Generate all palindromic subsequence of
// length N and K Even natural number
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 sequence(_ result: inout[Int],
_ index: Int,
_ n: Int,
_ k: Int)
{
if (index == n / 2)
{
if ((n % 2) != 0)
{
var i: Int = 2;
// Handles the request of odd length palindrome sequence
while (i <= k * 2)
{
result[index] = i;
self.printResult(result, n);
i += 2;
}
}
else
{
self.printResult(result, n);
}
}
else if (index > n / 2)
{
return;
}
else
{
var i: Int = 2;
while (i <= k * 2)
{
// Set the value of boundary element is to zero
result[index] = i;
result[(n - 1) - index] = i;
self.sequence(&result, index + 1, n, k);
i += 2;
}
}
}
func palindrome(_ n: Int, _ k: Int)
{
if (n <= 0 || k > n)
{
return;
}
print("\n Given Length : ", n, terminator: "");
print("\n K : ", k, terminator: "");
print("\n Initial K Even Number [", terminator: "");
var i: Int = 2;
while (i <= k * 2)
{
print(" ", i ," ", terminator: "");
i += 2;
}
print("]\n ", terminator: "");
// Auxiliary array which is collect result
var result: [Int] = Array(repeating: 0, count: n);
self.sequence(&result, 0, n, k);
}
}
func main()
{
let task: Combination = Combination();
let n: Int = 6;
let k: Int = 3;
/*
n = 6
k = 3
[2,4,6] 3 Even natural number
----------------------
2 2 2 2 2 2
2 2 4 4 2 2
2 2 6 6 2 2
2 4 2 2 4 2
2 4 4 4 4 2
2 4 6 6 4 2
2 6 2 2 6 2
2 6 4 4 6 2
2 6 6 6 6 2
4 2 2 2 2 4
4 2 4 4 2 4
4 2 6 6 2 4
4 4 2 2 4 4
4 4 4 4 4 4
4 4 6 6 4 4
4 6 2 2 6 4
4 6 4 4 6 4
4 6 6 6 6 4
6 2 2 2 2 6
6 2 4 4 2 6
6 2 6 6 2 6
6 4 2 2 4 6
6 4 4 4 4 6
6 4 6 6 4 6
6 6 2 2 6 6
6 6 4 4 6 6
6 6 6 6 6 6
---------------
*/
task.palindrome(n, k);
}
main();
Output
Given Length : 6
K : 3
Initial K Even Number [ 2 4 6 ]
2 2 2 2 2 2
2 2 4 4 2 2
2 2 6 6 2 2
2 4 2 2 4 2
2 4 4 4 4 2
2 4 6 6 4 2
2 6 2 2 6 2
2 6 4 4 6 2
2 6 6 6 6 2
4 2 2 2 2 4
4 2 4 4 2 4
4 2 6 6 2 4
4 4 2 2 4 4
4 4 4 4 4 4
4 4 6 6 4 4
4 6 2 2 6 4
4 6 4 4 6 4
4 6 6 6 6 4
6 2 2 2 2 6
6 2 4 4 2 6
6 2 6 6 2 6
6 4 2 2 4 6
6 4 4 4 4 6
6 4 6 6 4 6
6 6 2 2 6 6
6 6 4 4 6 6
6 6 6 6 6 6
// Kotlin program for
// Generate all palindromic subsequence of
// length N and K Even natural number
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 sequence(result: Array < Int > ,
index: Int,
n: Int,
k: Int): Unit
{
if (index == n / 2)
{
if ((n % 2) != 0)
{
var i: Int = 2;
// Handles the request of odd length palindrome sequence
while (i <= k * 2)
{
result[index] = i;
this.printResult(result, n);
i += 2;
}
}
else
{
this.printResult(result, n);
}
}
else if (index > n / 2)
{
return;
}
else
{
var i: Int = 2;
while (i <= k * 2)
{
// Set the value of boundary element is to zero
result[index] = i;
result[(n - 1) - index] = i;
this.sequence(result, index + 1, n, k);
i += 2;
}
}
}
fun palindrome(n: Int, k: Int): Unit
{
if (n <= 0 || k > n)
{
return;
}
print("\n Given Length : " + n);
print("\n K : " + k);
print("\n Initial K Even Number [");
var i: Int = 2;
while (i <= k * 2)
{
print(" " + i + " ");
i += 2;
}
print("]\n ");
// Auxiliary array which is collect result
var result: Array < Int > = Array(n)
{
0
};
this.sequence(result, 0, n, k);
}
}
fun main(args: Array < String > ): Unit
{
val task: Combination = Combination();
val n: Int = 6;
val k: Int = 3;
/*
n = 6
k = 3
[2,4,6] 3 Even natural number
----------------------
2 2 2 2 2 2
2 2 4 4 2 2
2 2 6 6 2 2
2 4 2 2 4 2
2 4 4 4 4 2
2 4 6 6 4 2
2 6 2 2 6 2
2 6 4 4 6 2
2 6 6 6 6 2
4 2 2 2 2 4
4 2 4 4 2 4
4 2 6 6 2 4
4 4 2 2 4 4
4 4 4 4 4 4
4 4 6 6 4 4
4 6 2 2 6 4
4 6 4 4 6 4
4 6 6 6 6 4
6 2 2 2 2 6
6 2 4 4 2 6
6 2 6 6 2 6
6 4 2 2 4 6
6 4 4 4 4 6
6 4 6 6 4 6
6 6 2 2 6 6
6 6 4 4 6 6
6 6 6 6 6 6
---------------
*/
task.palindrome(n, k);
}
Output
Given Length : 6
K : 3
Initial K Even Number [ 2 4 6 ]
2 2 2 2 2 2
2 2 4 4 2 2
2 2 6 6 2 2
2 4 2 2 4 2
2 4 4 4 4 2
2 4 6 6 4 2
2 6 2 2 6 2
2 6 4 4 6 2
2 6 6 6 6 2
4 2 2 2 2 4
4 2 4 4 2 4
4 2 6 6 2 4
4 4 2 2 4 4
4 4 4 4 4 4
4 4 6 6 4 4
4 6 2 2 6 4
4 6 4 4 6 4
4 6 6 6 6 4
6 2 2 2 2 6
6 2 4 4 2 6
6 2 6 6 2 6
6 4 2 2 4 6
6 4 4 4 4 6
6 4 6 6 4 6
6 6 2 2 6 6
6 6 4 4 6 6
6 6 6 6 6 6
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