Generate all palindromic subsequence of n natural number
Here given code implementation process.
// C Program
// Generate all palindromic subsequence of n 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)
{
if (index == n / 2)
{
if ((n % 2) != 0)
{
// Handles the request of odd length palindrome sequence
for (int i = 1; i <= n; ++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)
{
// Set the value of boundary element is to zero
result[index] = i;
result[(n - 1) - index] = i;
sequence(result, index + 1, n);
}
}
}
void palindrome(int n)
{
if (n <= 0)
{
return;
}
printf("\n Given Length : %d\n ", n);
// Auxiliary array which is collect result
int result[n];
sequence(result, 0, n);
}
int main()
{
int n = 4;
/*
n = 4
--------------------
1 1 1 1
1 2 2 1
1 3 3 1
1 4 4 1
2 1 1 2
2 2 2 2
2 3 3 2
2 4 4 2
3 1 1 3
3 2 2 3
3 3 3 3
3 4 4 3
4 1 1 4
4 2 2 4
4 3 3 4
4 4 4 4
---------------
*/
palindrome(n);
// Test B
n = 5;
// n is 5
palindrome(n);
return 0;
}
Output
Given Length : 4
1 1 1 1
1 2 2 1
1 3 3 1
1 4 4 1
2 1 1 2
2 2 2 2
2 3 3 2
2 4 4 2
3 1 1 3
3 2 2 3
3 3 3 3
3 4 4 3
4 1 1 4
4 2 2 4
4 3 3 4
4 4 4 4
Given Length : 5
1 1 1 1 1
1 1 2 1 1
1 1 3 1 1
1 1 4 1 1
1 1 5 1 1
1 2 1 2 1
1 2 2 2 1
1 2 3 2 1
1 2 4 2 1
1 2 5 2 1
1 3 1 3 1
1 3 2 3 1
1 3 3 3 1
1 3 4 3 1
1 3 5 3 1
1 4 1 4 1
1 4 2 4 1
1 4 3 4 1
1 4 4 4 1
1 4 5 4 1
1 5 1 5 1
1 5 2 5 1
1 5 3 5 1
1 5 4 5 1
1 5 5 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n 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)
{
if (index == n / 2)
{
if ((n % 2) != 0)
{
// Handles the request of
// odd length palindrome sequence
for (int i = 1; i <= n; ++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)
{
// Set the value of boundary element is to zero
result[index] = i;
result[(n - 1) - index] = i;
sequence(result, index + 1, n);
}
}
}
public void palindrome(int n)
{
if (n <= 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 = 4;
/*
n = 4
--------
1 1 1 1
1 2 2 1
1 3 3 1
1 4 4 1
2 1 1 2
2 2 2 2
2 3 3 2
2 4 4 2
3 1 1 3
3 2 2 3
3 3 3 3
3 4 4 3
4 1 1 4
4 2 2 4
4 3 3 4
4 4 4 4
--------
*/
task.palindrome(n);
// Test B
n = 5;
// n is 5
task.palindrome(n);
}
}
Output
Given Length : 4
1 1 1 1
1 2 2 1
1 3 3 1
1 4 4 1
2 1 1 2
2 2 2 2
2 3 3 2
2 4 4 2
3 1 1 3
3 2 2 3
3 3 3 3
3 4 4 3
4 1 1 4
4 2 2 4
4 3 3 4
4 4 4 4
Given Length : 5
1 1 1 1 1
1 1 2 1 1
1 1 3 1 1
1 1 4 1 1
1 1 5 1 1
1 2 1 2 1
1 2 2 2 1
1 2 3 2 1
1 2 4 2 1
1 2 5 2 1
1 3 1 3 1
1 3 2 3 1
1 3 3 3 1
1 3 4 3 1
1 3 5 3 1
1 4 1 4 1
1 4 2 4 1
1 4 3 4 1
1 4 4 4 1
1 4 5 4 1
1 5 1 5 1
1 5 2 5 1
1 5 3 5 1
1 5 4 5 1
1 5 5 5 1
2 1 1 1 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all palindromic subsequence of n 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)
{
if (index == n / 2)
{
if ((n % 2) != 0)
{
// Handles the request of
// odd length palindrome sequence
for (int i = 1; i <= n; ++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)
{
// Set the value of boundary element is to zero
result[index] = i;
result[(n - 1) - index] = i;
this->sequence(result, index + 1, n);
}
}
}
void palindrome(int n)
{
if (n <= 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 = 4;
/*
n = 4
--------
1 1 1 1
1 2 2 1
1 3 3 1
1 4 4 1
2 1 1 2
2 2 2 2
2 3 3 2
2 4 4 2
3 1 1 3
3 2 2 3
3 3 3 3
3 4 4 3
4 1 1 4
4 2 2 4
4 3 3 4
4 4 4 4
--------
*/
task->palindrome(n);
// Test B
n = 5;
// n is 5
task->palindrome(n);
return 0;
}
Output
Given Length : 4
1 1 1 1
1 2 2 1
1 3 3 1
1 4 4 1
2 1 1 2
2 2 2 2
2 3 3 2
2 4 4 2
3 1 1 3
3 2 2 3
3 3 3 3
3 4 4 3
4 1 1 4
4 2 2 4
4 3 3 4
4 4 4 4
Given Length : 5
1 1 1 1 1
1 1 2 1 1
1 1 3 1 1
1 1 4 1 1
1 1 5 1 1
1 2 1 2 1
1 2 2 2 1
1 2 3 2 1
1 2 4 2 1
1 2 5 2 1
1 3 1 3 1
1 3 2 3 1
1 3 3 3 1
1 3 4 3 1
1 3 5 3 1
1 4 1 4 1
1 4 2 4 1
1 4 3 4 1
1 4 4 4 1
1 4 5 4 1
1 5 1 5 1
1 5 2 5 1
1 5 3 5 1
1 5 4 5 1
1 5 5 5 1
2 1 1 1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equence of n 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)
{
if (index == n / 2)
{
if ((n % 2) != 0)
{
// Handles the request of
// odd length palindrome sequence
for (int i = 1; i <= n; ++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)
{
// Set the value of boundary element is to zero
result[index] = i;
result[(n - 1) - index] = i;
this.sequence(result, index + 1, n);
}
}
}
public void palindrome(int n)
{
if (n <= 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 = 4;
/*
n = 4
--------
1 1 1 1
1 2 2 1
1 3 3 1
1 4 4 1
2 1 1 2
2 2 2 2
2 3 3 2
2 4 4 2
3 1 1 3
3 2 2 3
3 3 3 3
3 4 4 3
4 1 1 4
4 2 2 4
4 3 3 4
4 4 4 4
--------
*/
task.palindrome(n);
// Test B
n = 5;
// n is 5
task.palindrome(n);
}
}
Output
Given Length : 4
1 1 1 1
1 2 2 1
1 3 3 1
1 4 4 1
2 1 1 2
2 2 2 2
2 3 3 2
2 4 4 2
3 1 1 3
3 2 2 3
3 3 3 3
3 4 4 3
4 1 1 4
4 2 2 4
4 3 3 4
4 4 4 4
Given Length : 5
1 1 1 1 1
1 1 2 1 1
1 1 3 1 1
1 1 4 1 1
1 1 5 1 1
1 2 1 2 1
1 2 2 2 1
1 2 3 2 1
1 2 4 2 1
1 2 5 2 1
1 3 1 3 1
1 3 2 3 1
1 3 3 3 1
1 3 4 3 1
1 3 5 3 1
1 4 1 4 1
1 4 2 4 1
1 4 3 4 1
1 4 4 4 1
1 4 5 4 1
1 5 1 5 1
1 5 2 5 1
1 5 3 5 1
1 5 4 5 1
1 5 5 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all palindromic subsequence of n 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) {
if index == n / 2 {
if (n % 2) != 0 {
// Handles the request of
// odd length palindrome sequence
for i := 1 ; i <= n ; 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++ {
// Set the value of boundary element is to zero
result[index] = i
result[(n - 1) - index] = i
this.sequence(result, index + 1, n)
}
}
}
func(this Combination) palindrome(n int) {
if n <= 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 = 4
/*
n = 4
--------
1 1 1 1
1 2 2 1
1 3 3 1
1 4 4 1
2 1 1 2
2 2 2 2
2 3 3 2
2 4 4 2
3 1 1 3
3 2 2 3
3 3 3 3
3 4 4 3
4 1 1 4
4 2 2 4
4 3 3 4
4 4 4 4
--------
*/
task.palindrome(n)
// Test B
n = 5
// n is 5
task.palindrome(n)
}
Output
Given Length : 4
1 1 1 1
1 2 2 1
1 3 3 1
1 4 4 1
2 1 1 2
2 2 2 2
2 3 3 2
2 4 4 2
3 1 1 3
3 2 2 3
3 3 3 3
3 4 4 3
4 1 1 4
4 2 2 4
4 3 3 4
4 4 4 4
Given Length : 5
1 1 1 1 1
1 1 2 1 1
1 1 3 1 1
1 1 4 1 1
1 1 5 1 1
1 2 1 2 1
1 2 2 2 1
1 2 3 2 1
1 2 4 2 1
1 2 5 2 1
1 3 1 3 1
1 3 2 3 1
1 3 3 3 1
1 3 4 3 1
1 3 5 3 1
1 4 1 4 1
1 4 2 4 1
1 4 3 4 1
1 4 4 4 1
1 4 5 4 1
1 5 1 5 1
1 5 2 5 1
1 5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 5
<?php
// Php program for
// Generate all palindromic subsequence of n 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)
{
if ($index == (int)($n / 2))
{
if (($n % 2) != 0)
{
// Handles the request of
// odd length palindrome sequence
for ($i = 1; $i <= $n; ++$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)
{
// Set the value of boundary element is to zero
$result[$index] = $i;
$result[($n - 1) - $index] = $i;
$this->sequence($result, $index + 1, $n);
}
}
}
public function palindrome($n)
{
if ($n <= 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 = 4;
/*
n = 4
--------
1 1 1 1
1 2 2 1
1 3 3 1
1 4 4 1
2 1 1 2
2 2 2 2
2 3 3 2
2 4 4 2
3 1 1 3
3 2 2 3
3 3 3 3
3 4 4 3
4 1 1 4
4 2 2 4
4 3 3 4
4 4 4 4
--------
*/
$task->palindrome($n);
// Test B
$n = 5;
// n is 5
$task->palindrome($n);
}
main();
Output
Given Length : 4
1 1 1 1
1 2 2 1
1 3 3 1
1 4 4 1
2 1 1 2
2 2 2 2
2 3 3 2
2 4 4 2
3 1 1 3
3 2 2 3
3 3 3 3
3 4 4 3
4 1 1 4
4 2 2 4
4 3 3 4
4 4 4 4
Given Length : 5
1 1 1 1 1
1 1 2 1 1
1 1 3 1 1
1 1 4 1 1
1 1 5 1 1
1 2 1 2 1
1 2 2 2 1
1 2 3 2 1
1 2 4 2 1
1 2 5 2 1
1 3 1 3 1
1 3 2 3 1
1 3 3 3 1
1 3 4 3 1
1 3 5 3 1
1 4 1 4 1
1 4 2 4 1
1 4 3 4 1
1 4 4 4 1
1 4 5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
5 5 3 5 5
5 5 4 5 5
5 5 5 5 5
// Node JS program for
// Generate all palindromic subsequence of n 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)
{
if (index == parseInt(n / 2))
{
if ((n % 2) != 0)
{
// Handles the request of
// odd length palindrome sequence
for (var i = 1; i <= n; ++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)
{
// Set the value of boundary element is to zero
result[index] = i;
result[(n - 1) - index] = i;
this.sequence(result, index + 1, n);
}
}
}
palindrome(n)
{
if (n <= 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 = 4;
/*
n = 4
--------
1 1 1 1
1 2 2 1
1 3 3 1
1 4 4 1
2 1 1 2
2 2 2 2
2 3 3 2
2 4 4 2
3 1 1 3
3 2 2 3
3 3 3 3
3 4 4 3
4 1 1 4
4 2 2 4
4 3 3 4
4 4 4 4
--------
*/
task.palindrome(n);
// Test B
n = 5;
// n is 5
task.palindrome(n);
}
main();
Output
Given Length : 4
1 1 1 1
1 2 2 1
1 3 3 1
1 4 4 1
2 1 1 2
2 2 2 2
2 3 3 2
2 4 4 2
3 1 1 3
3 2 2 3
3 3 3 3
3 4 4 3
4 1 1 4
4 2 2 4
4 3 3 4
4 4 4 4
Given Length : 5
1 1 1 1 1
1 1 2 1 1
1 1 3 1 1
1 1 4 1 1
1 1 5 1 1
1 2 1 2 1
1 2 2 2 1
1 2 3 2 1
1 2 4 2 1
1 2 5 2 1
1 3 1 3 1
1 3 2 3 1
1 3 3 3 1
1 3 4 3 1
1 3 5 3 1
1 4 1 4 1
1 4 2 4 1
1 4 3 4 1
1 4 4 4 1
1 4 5 4 1
1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 5 5
5 5 4 5 5
5 5 5 5 5
# Python 3 program for
# Generate all palindromic subsequence of n 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) :
if (index == int(n / 2)) :
if ((n % 2) != 0) :
i = 1
# Handles the request of
# odd length palindrome sequence
while (i <= n) :
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) :
# Set the value of boundary element is to zero
result[index] = i
result[(n - 1) - index] = i
self.sequence(result, index + 1, n)
i += 1
def palindrome(self, n) :
if (n <= 0) :
return
print("\n Given Length : ", n,end ="\n " )
# Auxiliary list which is collect result
result = [0] * (n)
self.sequence(result, 0, n)
def main() :
task = Combination()
n = 4
# n = 4
# --------
# 1 1 1 1
# 1 2 2 1
# 1 3 3 1
# 1 4 4 1
# 2 1 1 2
# 2 2 2 2
# 2 3 3 2
# 2 4 4 2
# 3 1 1 3
# 3 2 2 3
# 3 3 3 3
# 3 4 4 3
# 4 1 1 4
# 4 2 2 4
# 4 3 3 4
# 4 4 4 4
# --------
task.palindrome(n)
# Test B
n = 5
# n is 5
task.palindrome(n)
if __name__ == "__main__": main()
Output
Given Length : 4
1 1 1 1
1 2 2 1
1 3 3 1
1 4 4 1
2 1 1 2
2 2 2 2
2 3 3 2
2 4 4 2
3 1 1 3
3 2 2 3
3 3 3 3
3 4 4 3
4 1 1 4
4 2 2 4
4 3 3 4
4 4 4 4
Given Length : 5
1 1 1 1 1
1 1 2 1 1
1 1 3 1 1
1 1 4 1 1
1 1 5 1 1
1 2 1 2 1
1 2 2 2 1
1 2 3 2 1
1 2 4 2 1
1 2 5 2 1
1 3 1 3 1
1 3 2 3 1
1 3 3 3 1
1 3 4 3 1
1 3 5 3 1
1 4 1 4 1
1 4 2 4 1
1 4 3 4 1
1 4 4 4 1
1 4 5 4 1
1 5 1 5 1
1 5 2 5 1
1 5 3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
# Ruby program for
# Generate all palindromic subsequence of n 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)
if (index == n / 2)
if ((n % 2) != 0)
i = 1
# Handles the request of
# odd length palindrome sequence
while (i <= n)
result[index] = i
self.printResult(result, n)
i += 1
end
else
self.printResult(result, n)
end
elsif (index > n / 2)
return
else
i = 1
while (i <= n)
# Set the value of boundary element is to zero
result[index] = i
result[(n - 1) - index] = i
self.sequence(result, index + 1, n)
i += 1
end
end
end
def palindrome(n)
if (n <= 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 = 4
# n = 4
# --------
# 1 1 1 1
# 1 2 2 1
# 1 3 3 1
# 1 4 4 1
# 2 1 1 2
# 2 2 2 2
# 2 3 3 2
# 2 4 4 2
# 3 1 1 3
# 3 2 2 3
# 3 3 3 3
# 3 4 4 3
# 4 1 1 4
# 4 2 2 4
# 4 3 3 4
# 4 4 4 4
# --------
task.palindrome(n)
# Test B
n = 5
# n is 5
task.palindrome(n)
end
main()
Output
Given Length : 4
1 1 1 1
1 2 2 1
1 3 3 1
1 4 4 1
2 1 1 2
2 2 2 2
2 3 3 2
2 4 4 2
3 1 1 3
3 2 2 3
3 3 3 3
3 4 4 3
4 1 1 4
4 2 2 4
4 3 3 4
4 4 4 4
Given Length : 5
1 1 1 1 1
1 1 2 1 1
1 1 3 1 1
1 1 4 1 1
1 1 5 1 1
1 2 1 2 1
1 2 2 2 1
1 2 3 2 1
1 2 4 2 1
1 2 5 2 1
1 3 1 3 1
1 3 2 3 1
1 3 3 3 1
1 3 4 3 1
1 3 5 3 1
1 4 1 4 1
1 4 2 4 1
1 4 3 4 1
1 4 4 4 1
1 4 5 4 1
1 5 1 5 1
1 5 2 5 1
1 5 3 5 1
1 5 4 5 1
1 5 5 5 1
2 1 1 1 2
2 1 2 1 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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): Unit = {
if (index == n / 2)
{
if ((n % 2) != 0)
{
var i: Int = 1;
// Handles the request of
// odd length palindrome sequence
while (i <= n)
{
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)
{
// Set the value of boundary element is to zero
result(index) = i;
result((n - 1) - index) = i;
sequence(result, index + 1, n);
i += 1;
}
}
}
def palindrome(n: Int): Unit = {
if (n <= 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 = 4;
/*
n = 4
--------
1 1 1 1
1 2 2 1
1 3 3 1
1 4 4 1
2 1 1 2
2 2 2 2
2 3 3 2
2 4 4 2
3 1 1 3
3 2 2 3
3 3 3 3
3 4 4 3
4 1 1 4
4 2 2 4
4 3 3 4
4 4 4 4
--------
*/
task.palindrome(n);
// Test B
n = 5;
// n is 5
task.palindrome(n);
}
}
Output
Given Length : 4
1 1 1 1
1 2 2 1
1 3 3 1
1 4 4 1
2 1 1 2
2 2 2 2
2 3 3 2
2 4 4 2
3 1 1 3
3 2 2 3
3 3 3 3
3 4 4 3
4 1 1 4
4 2 2 4
4 3 3 4
4 4 4 4
Given Length : 5
1 1 1 1 1
1 1 2 1 1
1 1 3 1 1
1 1 4 1 1
1 1 5 1 1
1 2 1 2 1
1 2 2 2 1
1 2 3 2 1
1 2 4 2 1
1 2 5 2 1
1 3 1 3 1
1 3 2 3 1
1 3 3 3 1
1 3 4 3 1
1 3 5 3 1
1 4 1 4 1
1 4 2 4 1
1 4 3 4 1
1 4 4 4 1
1 4 5 4 1
1 5 1 5 1
1 5 2 5 1
1 5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 5
// Swift 4 program for
// Generate all palindromic subsequence of n 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)
{
if (index == n / 2)
{
if ((n % 2) != 0)
{
var i: Int = 1;
// Handles the request of
// odd length palindrome sequence
while (i <= n)
{
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)
{
// Set the value of boundary element is to zero
result[index] = i;
result[(n - 1) - index] = i;
self.sequence(&result, index + 1, n);
i += 1;
}
}
}
func palindrome(_ n: Int)
{
if (n <= 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 = 4;
/*
n = 4
--------
1 1 1 1
1 2 2 1
1 3 3 1
1 4 4 1
2 1 1 2
2 2 2 2
2 3 3 2
2 4 4 2
3 1 1 3
3 2 2 3
3 3 3 3
3 4 4 3
4 1 1 4
4 2 2 4
4 3 3 4
4 4 4 4
--------
*/
task.palindrome(n);
// Test B
n = 5;
// n is 5
task.palindrome(n);
}
main();
Output
Given Length : 4
1 1 1 1
1 2 2 1
1 3 3 1
1 4 4 1
2 1 1 2
2 2 2 2
2 3 3 2
2 4 4 2
3 1 1 3
3 2 2 3
3 3 3 3
3 4 4 3
4 1 1 4
4 2 2 4
4 3 3 4
4 4 4 4
Given Length : 5
1 1 1 1 1
1 1 2 1 1
1 1 3 1 1
1 1 4 1 1
1 1 5 1 1
1 2 1 2 1
1 2 2 2 1
1 2 3 2 1
1 2 4 2 1
1 2 5 2 1
1 3 1 3 1
1 3 2 3 1
1 3 3 3 1
1 3 4 3 1
1 3 5 3 1
1 4 1 4 1
1 4 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5
5 4 5 4 5
5 5 1 5 5
5 5 2 5 5
5 5 3 5 5
5 5 4 5 5
5 5 5 5 5
// Kotlin program for
// Generate all palindromic subsequence of n 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): Unit
{
if (index == n / 2)
{
if ((n % 2) != 0)
{
var i: Int = 1;
// Handles the request of
// odd length palindrome sequence
while (i <= n)
{
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)
{
// Set the value of boundary element is to zero
result[index] = i;
result[(n - 1) - index] = i;
this.sequence(result, index + 1, n);
i += 1;
}
}
}
fun palindrome(n: Int): Unit
{
if (n <= 0)
{
return;
}
print("\n Given Length : " + n + "\n ");
// Auxiliary array which is collect result
var result: Array < Int > = Array(n)
{
0
};
this.sequence(result, 0, n);
}
}
fun main(args: Array < String > ): Unit
{
val task: Combination = Combination();
var n: Int = 4;
/*
n = 4
--------
1 1 1 1
1 2 2 1
1 3 3 1
1 4 4 1
2 1 1 2
2 2 2 2
2 3 3 2
2 4 4 2
3 1 1 3
3 2 2 3
3 3 3 3
3 4 4 3
4 1 1 4
4 2 2 4
4 3 3 4
4 4 4 4
--------
*/
task.palindrome(n);
// Test B
n = 5;
// n is 5
task.palindrome(n);
}
Output
Given Length : 4
1 1 1 1
1 2 2 1
1 3 3 1
1 4 4 1
2 1 1 2
2 2 2 2
2 3 3 2
2 4 4 2
3 1 1 3
3 2 2 3
3 3 3 3
3 4 4 3
4 1 1 4
4 2 2 4
4 3 3 4
4 4 4 4
Given Length : 5
1 1 1 1 1
1 1 2 1 1
1 1 3 1 1
1 1 4 1 1
1 1 5 1 1
1 2 1 2 1
1 2 2 2 1
1 2 3 2 1
1 2 4 2 1
1 2 5 2 1
1 3 1 3 1
1 3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 5
5 3 5 3 5
5 4 1 4 5
5 4 2 4 5
5 4 3 4 5
5 4 4 4 5
5 4 5 4 5
5 5 1 5 5
5 5 2 5 5
5 5 3 5 5
5 5 4 5 5
5 5 5 5 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