Generate fixed bitonic sequence of n natural number
Here given code implementation process.
// C Program
// Generate fixed bitonic sequence of n natural number
#include <stdio.h>
void printResult(int result[], int n)
{
for (int i = 0; i < n; ++i)
{
printf(" %d", result[i]);
}
printf("\n");
}
void sequence(int result[], int value,
int index, int flag, int n)
{
if (index == n)
{
// Display result
printResult(result, n);
}
else if (value > n || value < 1 ||
(index > 0 && result[index - 1] == value) ||
(index == n - 1 && value == n))
{
// When following conditions are occur
// ➀ When value is greater than n or
// ➁ When value is less than 1 or
// ➂ When consecutive elements are same or
// ➃ Last element is a nth natural number
return;
}
else
{
result[index] = value;
if (flag == 1)
{
sequence(result, n, index + 1, 0, n);
sequence(result, value + 1, index + 1, 1, n);
sequence(result, value + 1, index, 1, n);
}
else
{
sequence(result, value - 1, index + 1, 0, n);
sequence(result, value - 1, index, 0, n);
}
}
}
void findSequence(int n)
{
if (n < 2)
{
return;
}
// Auxiliary array which is collect result
int result[n];
printf("\n Given N : %d\n", n);
sequence(result, 1, 0, 1, n);
}
int main(int argc, char
const *argv[])
{
int n = 3;
/*
1 3 2
1 3 1
1 2 1
2 3 2
2 3 1
--------
Possible bitonic sequence
*/
findSequence(n);
n = 5;
findSequence(n);
return 0;
}
Output
Given N : 3
1 3 2
1 3 1
1 2 1
2 3 2
2 3 1
Given N : 5
1 5 4 3 2
1 5 4 3 1
1 5 4 2 1
1 5 3 2 1
1 4 3 2 1
1 2 5 4 3
1 2 5 4 2
1 2 5 4 1
1 2 5 3 2
1 2 5 3 1
1 2 5 2 1
1 2 4 3 2
1 2 4 3 1
1 2 4 2 1
1 2 3 2 1
1 2 3 5 4
1 2 3 5 3
1 2 3 5 2
1 2 3 5 1
1 2 3 4 3
1 2 3 4 2
1 2 3 4 1
1 2 4 5 4
1 2 4 5 3
1 2 4 5 2
1 2 4 5 1
1 3 5 4 3
1 3 5 4 2
1 3 5 4 1
1 3 5 3 2
1 3 5 3 1
1 3 5 2 1
1 3 4 3 2
1 3 4 3 1
1 3 4 2 1
1 3 4 5 4
1 3 4 5 3
1 3 4 5 2
1 3 4 5 1
1 4 5 4 3
1 4 5 4 2
1 4 5 4 1
1 4 5 3 2
1 4 5 3 1
1 4 5 2 1
2 5 4 3 2
2 5 4 3 1
2 5 4 2 1
2 5 3 2 1
2 4 3 2 1
2 3 5 4 3
2 3 5 4 2
2 3 5 4 1
2 3 5 3 2
2 3 5 3 1
2 3 5 2 1
2 3 4 3 2
2 3 4 3 1
2 3 4 2 1
2 3 4 5 4
2 3 4 5 3
2 3 4 5 2
2 3 4 5 1
2 4 5 4 3
2 4 5 4 2
2 4 5 4 1
2 4 5 3 2
2 4 5 3 1
2 4 5 2 1
3 5 4 3 2
3 5 4 3 1
3 5 4 2 1
3 5 3 2 1
3 4 3 2 1
3 4 5 4 3
3 4 5 4 2
3 4 5 4 1
3 4 5 3 2
3 4 5 3 1
3 4 5 2 1
4 5 4 3 2
4 5 4 3 1
4 5 4 2 1
4 5 3 2 1
// Java program for
// Generate fixed bitonic sequence of n natural number
public class Combination
{
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 value,
int index,
int flag,
int n)
{
if (index == n)
{
// Display result
printResult(result, n);
}
else if (value > n || value < 1 ||
(index > 0 && result[index - 1] == value) ||
(index == n - 1 && value == n))
{
// When following conditions are occur
// ➀ When value is greater than n or
// ➁ When value is less than 1 or
// ➂ When consecutive elements are same or
// ➃ Last element is a nth natural number
return;
}
else
{
result[index] = value;
if (flag == 1)
{
sequence(result, n, index + 1, 0, n);
sequence(result, value + 1, index + 1, 1, n);
sequence(result, value + 1, index, 1, n);
}
else
{
sequence(result, value - 1, index + 1, 0, n);
sequence(result, value - 1, index, 0, n);
}
}
}
public void findSequence(int n)
{
if (n < 2)
{
return;
}
// Auxiliary array which is collect result
int[] result = new int[n];
System.out.print("\n Given N : " + n + "\n");
sequence(result, 1, 0, 1, n);
}
public static void main(String[] args)
{
Combination task = new Combination();
int n = 3;
/*
1 3 2
1 3 1
1 2 1
2 3 2
2 3 1
--------
Possible bitonic sequence
*/
task.findSequence(n);
n = 5;
task.findSequence(n);
}
}
Output
Given N : 3
1 3 2
1 3 1
1 2 1
2 3 2
2 3 1
Given N : 5
1 5 4 3 2
1 5 4 3 1
1 5 4 2 1
1 5 3 2 1
1 4 3 2 1
1 2 5 4 3
1 2 5 4 2
1 2 5 4 1
1 2 5 3 2
1 2 5 3 1
1 2 5 2 1
1 2 4 3 2
1 2 4 3 1
1 2 4 2 1
1 2 3 2 1
1 2 3 5 4
1 2 3 5 3
1 2 3 5 2
1 2 3 5 1
1 2 3 4 3
1 2 3 4 2
1 2 3 4 1
1 2 4 5 4
1 2 4 5 3
1 2 4 5 2
1 2 4 5 1
1 3 5 4 3
1 3 5 4 2
1 3 5 4 1
1 3 5 3 2
1 3 5 3 1
1 3 5 2 1
1 3 4 3 2
1 3 4 3 1
1 3 4 2 1
1 3 4 5 4
1 3 4 5 3
1 3 4 5 2
1 3 4 5 1
1 4 5 4 3
1 4 5 4 2
1 4 5 4 1
1 4 5 3 2
1 4 5 3 1
1 4 5 2 1
2 5 4 3 2
2 5 4 3 1
2 5 4 2 1
2 5 3 2 1
2 4 3 2 1
2 3 5 4 3
2 3 5 4 2
2 3 5 4 1
2 3 5 3 2
2 3 5 3 1
2 3 5 2 1
2 3 4 3 2
2 3 4 3 1
2 3 4 2 1
2 3 4 5 4
2 3 4 5 3
2 3 4 5 2
2 3 4 5 1
2 4 5 4 3
2 4 5 4 2
2 4 5 4 1
2 4 5 3 2
2 4 5 3 1
2 4 5 2 1
3 5 4 3 2
3 5 4 3 1
3 5 4 2 1
3 5 3 2 1
3 4 3 2 1
3 4 5 4 3
3 4 5 4 2
3 4 5 4 1
3 4 5 3 2
3 4 5 3 1
3 4 5 2 1
4 5 4 3 2
4 5 4 3 1
4 5 4 2 1
4 5 3 2 1
// Include header file
#include <iostream>
using namespace std;
// C++ program for
// Generate fixed bitonic sequence of n natural number
class Combination
{
public: void printResult(int result[], int n)
{
for (int i = 0; i < n; ++i)
{
cout << " " << result[i];
}
cout << "\n";
}
void sequence(int result[],
int value, int index,
int flag, int n)
{
if (index == n)
{
// Display result
this->printResult(result, n);
}
else if (value > n || value < 1 ||
(index > 0 && result[index - 1] == value) ||
(index == n - 1 && value == n))
{
// When following conditions are occur
// ➀ When value is greater than n or
// ➁ When value is less than 1 or
// ➂ When consecutive elements are same or
// ➃ Last element is a nth natural number
return;
}
else
{
result[index] = value;
if (flag == 1)
{
this->sequence(result, n, index + 1, 0, n);
this->sequence(result, value + 1, index + 1, 1, n);
this->sequence(result, value + 1, index, 1, n);
}
else
{
this->sequence(result, value - 1, index + 1, 0, n);
this->sequence(result, value - 1, index, 0, n);
}
}
}
void findSequence(int n)
{
if (n < 2)
{
return;
}
// Auxiliary array which is collect result
int result[n];
cout << "\n Given N : "
<< n << "\n";
this->sequence(result, 1, 0, 1, n);
}
};
int main()
{
Combination *task = new Combination();
int n = 3;
/*
1 3 2
1 3 1
1 2 1
2 3 2
2 3 1
--------
Possible bitonic sequence
*/
task->findSequence(n);
n = 5;
task->findSequence(n);
return 0;
}
Output
Given N : 3
1 3 2
1 3 1
1 2 1
2 3 2
2 3 1
Given N : 5
1 5 4 3 2
1 5 4 3 1
1 5 4 2 1
1 5 3 2 1
1 4 3 2 1
1 2 5 4 3
1 2 5 4 2
1 2 5 4 1
1 2 5 3 2
1 2 5 3 1
1 2 5 2 1
1 2 4 3 2
1 2 4 3 1
1 2 4 2 1
1 2 3 2 1
1 2 3 5 4
1 2 3 5 3
1 2 3 5 2
1 2 3 5 1
1 2 3 4 3
1 2 3 4 2
1 2 3 4 1
1 2 4 5 4
1 2 4 5 3
1 2 4 5 2
1 2 4 5 1
1 3 5 4 3
1 3 5 4 2
1 3 5 4 1
1 3 5 3 2
1 3 5 3 1
1 3 5 2 1
1 3 4 3 2
1 3 4 3 1
1 3 4 2 1
1 3 4 5 4
1 3 4 5 3
1 3 4 5 2
1 3 4 5 1
1 4 5 4 3
1 4 5 4 2
1 4 5 4 1
1 4 5 3 2
1 4 5 3 1
1 4 5 2 1
2 5 4 3 2
2 5 4 3 1
2 5 4 2 1
2 5 3 2 1
2 4 3 2 1
2 3 5 4 3
2 3 5 4 2
2 3 5 4 1
2 3 5 3 2
2 3 5 3 1
2 3 5 2 1
2 3 4 3 2
2 3 4 3 1
2 3 4 2 1
2 3 4 5 4
2 3 4 5 3
2 3 4 5 2
2 3 4 5 1
2 4 5 4 3
2 4 5 4 2
2 4 5 4 1
2 4 5 3 2
2 4 5 3 1
2 4 5 2 1
3 5 4 3 2
3 5 4 3 1
3 5 4 2 1
3 5 3 2 1
3 4 3 2 1
3 4 5 4 3
3 4 5 4 2
3 4 5 4 1
3 4 5 3 2
3 4 5 3 1
3 4 5 2 1
4 5 4 3 2
4 5 4 3 1
4 5 4 2 1
4 5 3 2 1
// Include namespace system
using System;
// Csharp program for
// Generate fixed bitonic sequence of n natural number
public class Combination
{
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 value,
int index,
int flag,
int n)
{
if (index == n)
{
// Display result
this.printResult(result, n);
}
else if (value > n || value < 1 ||
(index > 0 && result[index - 1] == value) ||
(index == n - 1 && value == n))
{
// When following conditions are occur
// ➀ When value is greater than n or
// ➁ When value is less than 1 or
// ➂ When consecutive elements are same or
// ➃ Last element is a nth natural number
return;
}
else
{
result[index] = value;
if (flag == 1)
{
this.sequence(result, n, index + 1, 0, n);
this.sequence(result, value + 1, index + 1, 1, n);
this.sequence(result, value + 1, index, 1, n);
}
else
{
this.sequence(result, value - 1, index + 1, 0, n);
this.sequence(result, value - 1, index, 0, n);
}
}
}
public void findSequence(int n)
{
if (n < 2)
{
return;
}
// Auxiliary array which is collect result
int[] result = new int[n];
Console.Write("\n Given N : " + n + "\n");
this.sequence(result, 1, 0, 1, n);
}
public static void Main(String[] args)
{
Combination task = new Combination();
int n = 3;
/*
1 3 2
1 3 1
1 2 1
2 3 2
2 3 1
--------
Possible bitonic sequence
*/
task.findSequence(n);
n = 5;
task.findSequence(n);
}
}
Output
Given N : 3
1 3 2
1 3 1
1 2 1
2 3 2
2 3 1
Given N : 5
1 5 4 3 2
1 5 4 3 1
1 5 4 2 1
1 5 3 2 1
1 4 3 2 1
1 2 5 4 3
1 2 5 4 2
1 2 5 4 1
1 2 5 3 2
1 2 5 3 1
1 2 5 2 1
1 2 4 3 2
1 2 4 3 1
1 2 4 2 1
1 2 3 2 1
1 2 3 5 4
1 2 3 5 3
1 2 3 5 2
1 2 3 5 1
1 2 3 4 3
1 2 3 4 2
1 2 3 4 1
1 2 4 5 4
1 2 4 5 3
1 2 4 5 2
1 2 4 5 1
1 3 5 4 3
1 3 5 4 2
1 3 5 4 1
1 3 5 3 2
1 3 5 3 1
1 3 5 2 1
1 3 4 3 2
1 3 4 3 1
1 3 4 2 1
1 3 4 5 4
1 3 4 5 3
1 3 4 5 2
1 3 4 5 1
1 4 5 4 3
1 4 5 4 2
1 4 5 4 1
1 4 5 3 2
1 4 5 3 1
1 4 5 2 1
2 5 4 3 2
2 5 4 3 1
2 5 4 2 1
2 5 3 2 1
2 4 3 2 1
2 3 5 4 3
2 3 5 4 2
2 3 5 4 1
2 3 5 3 2
2 3 5 3 1
2 3 5 2 1
2 3 4 3 2
2 3 4 3 1
2 3 4 2 1
2 3 4 5 4
2 3 4 5 3
2 3 4 5 2
2 3 4 5 1
2 4 5 4 3
2 4 5 4 2
2 4 5 4 1
2 4 5 3 2
2 4 5 3 1
2 4 5 2 1
3 5 4 3 2
3 5 4 3 1
3 5 4 2 1
3 5 3 2 1
3 4 3 2 1
3 4 5 4 3
3 4 5 4 2
3 4 5 4 1
3 4 5 3 2
3 4 5 3 1
3 4 5 2 1
4 5 4 3 2
4 5 4 3 1
4 5 4 2 1
4 5 3 2 1
package main
import "fmt"
// Go program for
// Generate fixed bitonic sequence of n natural number
type Combination struct {}
func getCombination() * Combination {
var me *Combination = &Combination {}
return me
}
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,
value int, index int,
flag int, n int) {
if index == n {
// Display result
this.printResult(result, n)
} else if value > n || value < 1 ||
(index > 0 && result[index - 1] == value) ||
(index == n - 1 && value == n) {
// When following conditions are occur
// ➀ When value is greater than n or
// ➁ When value is less than 1 or
// ➂ When consecutive elements are same or
// ➃ Last element is a nth natural number
return
} else {
result[index] = value
if flag == 1 {
this.sequence(result, n, index + 1, 0, n)
this.sequence(result, value + 1, index + 1, 1, n)
this.sequence(result, value + 1, index, 1, n)
} else {
this.sequence(result, value - 1, index + 1, 0, n)
this.sequence(result, value - 1, index, 0, n)
}
}
}
func(this Combination) findSequence(n int) {
if n < 2 {
return
}
// Auxiliary array which is collect result
var result = make([] int, n)
fmt.Print("\n Given N : ", n, "\n")
this.sequence(result, 1, 0, 1, n)
}
func main() {
var task * Combination = getCombination()
var n int = 3
/*
1 3 2
1 3 1
1 2 1
2 3 2
2 3 1
--------
Possible bitonic sequence
*/
task.findSequence(n)
n = 5
task.findSequence(n)
}
Output
Given N : 3
1 3 2
1 3 1
1 2 1
2 3 2
2 3 1
Given N : 5
1 5 4 3 2
1 5 4 3 1
1 5 4 2 1
1 5 3 2 1
1 4 3 2 1
1 2 5 4 3
1 2 5 4 2
1 2 5 4 1
1 2 5 3 2
1 2 5 3 1
1 2 5 2 1
1 2 4 3 2
1 2 4 3 1
1 2 4 2 1
1 2 3 2 1
1 2 3 5 4
1 2 3 5 3
1 2 3 5 2
1 2 3 5 1
1 2 3 4 3
1 2 3 4 2
1 2 3 4 1
1 2 4 5 4
1 2 4 5 3
1 2 4 5 2
1 2 4 5 1
1 3 5 4 3
1 3 5 4 2
1 3 5 4 1
1 3 5 3 2
1 3 5 3 1
1 3 5 2 1
1 3 4 3 2
1 3 4 3 1
1 3 4 2 1
1 3 4 5 4
1 3 4 5 3
1 3 4 5 2
1 3 4 5 1
1 4 5 4 3
1 4 5 4 2
1 4 5 4 1
1 4 5 3 2
1 4 5 3 1
1 4 5 2 1
2 5 4 3 2
2 5 4 3 1
2 5 4 2 1
2 5 3 2 1
2 4 3 2 1
2 3 5 4 3
2 3 5 4 2
2 3 5 4 1
2 3 5 3 2
2 3 5 3 1
2 3 5 2 1
2 3 4 3 2
2 3 4 3 1
2 3 4 2 1
2 3 4 5 4
2 3 4 5 3
2 3 4 5 2
2 3 4 5 1
2 4 5 4 3
2 4 5 4 2
2 4 5 4 1
2 4 5 3 2
2 4 5 3 1
2 4 5 2 1
3 5 4 3 2
3 5 4 3 1
3 5 4 2 1
3 5 3 2 1
3 4 3 2 1
3 4 5 4 3
3 4 5 4 2
3 4 5 4 1
3 4 5 3 2
3 4 5 3 1
3 4 5 2 1
4 5 4 3 2
4 5 4 3 1
4 5 4 2 1
4 5 3 2 1
<?php
// Php program for
// Generate fixed bitonic sequence of n natural number
class Combination
{
public function printResult($result, $n)
{
for ($i = 0; $i < $n; ++$i)
{
echo(" ".$result[$i]);
}
echo("\n");
}
public function sequence($result,
$value,
$index,
$flag,
$n)
{
if ($index == $n)
{
// Display result
$this->printResult($result, $n);
}
else if ($value > $n || $value < 1 ||
($index > 0 && $result[$index - 1] == $value) ||
($index == $n - 1 && $value == $n))
{
// When following conditions are occur
// ➀ When value is greater than n or
// ➁ When value is less than 1 or
// ➂ When consecutive elements are same or
// ➃ Last element is a nth natural number
return;
}
else
{
$result[$index] = $value;
if ($flag == 1)
{
$this->sequence($result, $n, $index + 1, 0, $n);
$this->sequence($result, $value + 1, $index + 1, 1, $n);
$this->sequence($result, $value + 1, $index, 1, $n);
}
else
{
$this->sequence($result, $value - 1, $index + 1, 0, $n);
$this->sequence($result, $value - 1, $index, 0, $n);
}
}
}
public function findSequence($n)
{
if ($n < 2)
{
return;
}
// Auxiliary array which is collect result
$result = array_fill(0, $n, 0);
echo("\n Given N : ".$n."\n");
$this->sequence($result, 1, 0, 1, $n);
}
}
function main()
{
$task = new Combination();
$n = 3;
/*
1 3 2
1 3 1
1 2 1
2 3 2
2 3 1
--------
Possible bitonic sequence
*/
$task->findSequence($n);
$n = 5;
$task->findSequence($n);
}
main();
Output
Given N : 3
1 3 2
1 3 1
1 2 1
2 3 2
2 3 1
Given N : 5
1 5 4 3 2
1 5 4 3 1
1 5 4 2 1
1 5 3 2 1
1 4 3 2 1
1 2 5 4 3
1 2 5 4 2
1 2 5 4 1
1 2 5 3 2
1 2 5 3 1
1 2 5 2 1
1 2 4 3 2
1 2 4 3 1
1 2 4 2 1
1 2 3 2 1
1 2 3 5 4
1 2 3 5 3
1 2 3 5 2
1 2 3 5 1
1 2 3 4 3
1 2 3 4 2
1 2 3 4 1
1 2 4 5 4
1 2 4 5 3
1 2 4 5 2
1 2 4 5 1
1 3 5 4 3
1 3 5 4 2
1 3 5 4 1
1 3 5 3 2
1 3 5 3 1
1 3 5 2 1
1 3 4 3 2
1 3 4 3 1
1 3 4 2 1
1 3 4 5 4
1 3 4 5 3
1 3 4 5 2
1 3 4 5 1
1 4 5 4 3
1 4 5 4 2
1 4 5 4 1
1 4 5 3 2
1 4 5 3 1
1 4 5 2 1
2 5 4 3 2
2 5 4 3 1
2 5 4 2 1
2 5 3 2 1
2 4 3 2 1
2 3 5 4 3
2 3 5 4 2
2 3 5 4 1
2 3 5 3 2
2 3 5 3 1
2 3 5 2 1
2 3 4 3 2
2 3 4 3 1
2 3 4 2 1
2 3 4 5 4
2 3 4 5 3
2 3 4 5 2
2 3 4 5 1
2 4 5 4 3
2 4 5 4 2
2 4 5 4 1
2 4 5 3 2
2 4 5 3 1
2 4 5 2 1
3 5 4 3 2
3 5 4 3 1
3 5 4 2 1
3 5 3 2 1
3 4 3 2 1
3 4 5 4 3
3 4 5 4 2
3 4 5 4 1
3 4 5 3 2
3 4 5 3 1
3 4 5 2 1
4 5 4 3 2
4 5 4 3 1
4 5 4 2 1
4 5 3 2 1
// Node JS program for
// Generate fixed bitonic sequence of n natural number
class Combination
{
printResult(result, n)
{
for (var i = 0; i < n; ++i)
{
process.stdout.write(" " + result[i]);
}
process.stdout.write("\n");
}
sequence(result, value, index, flag, n)
{
if (index == n)
{
// Display result
this.printResult(result, n);
}
else if (value > n || value < 1 ||
(index > 0 && result[index - 1] == value) ||
(index == n - 1 && value == n))
{
// When following conditions are occur
// ➀ When value is greater than n or
// ➁ When value is less than 1 or
// ➂ When consecutive elements are same or
// ➃ Last element is a nth natural number
return;
}
else
{
result[index] = value;
if (flag == 1)
{
this.sequence(result, n, index + 1, 0, n);
this.sequence(result, value + 1, index + 1, 1, n);
this.sequence(result, value + 1, index, 1, n);
}
else
{
this.sequence(result, value - 1, index + 1, 0, n);
this.sequence(result, value - 1, index, 0, n);
}
}
}
findSequence(n)
{
if (n < 2)
{
return;
}
// Auxiliary array which is collect result
var result = Array(n).fill(0);
process.stdout.write("\n Given N : " + n + "\n");
this.sequence(result, 1, 0, 1, n);
}
}
function main()
{
var task = new Combination();
var n = 3;
/*
1 3 2
1 3 1
1 2 1
2 3 2
2 3 1
--------
Possible bitonic sequence
*/
task.findSequence(n);
n = 5;
task.findSequence(n);
}
main();
Output
Given N : 3
1 3 2
1 3 1
1 2 1
2 3 2
2 3 1
Given N : 5
1 5 4 3 2
1 5 4 3 1
1 5 4 2 1
1 5 3 2 1
1 4 3 2 1
1 2 5 4 3
1 2 5 4 2
1 2 5 4 1
1 2 5 3 2
1 2 5 3 1
1 2 5 2 1
1 2 4 3 2
1 2 4 3 1
1 2 4 2 1
1 2 3 2 1
1 2 3 5 4
1 2 3 5 3
1 2 3 5 2
1 2 3 5 1
1 2 3 4 3
1 2 3 4 2
1 2 3 4 1
1 2 4 5 4
1 2 4 5 3
1 2 4 5 2
1 2 4 5 1
1 3 5 4 3
1 3 5 4 2
1 3 5 4 1
1 3 5 3 2
1 3 5 3 1
1 3 5 2 1
1 3 4 3 2
1 3 4 3 1
1 3 4 2 1
1 3 4 5 4
1 3 4 5 3
1 3 4 5 2
1 3 4 5 1
1 4 5 4 3
1 4 5 4 2
1 4 5 4 1
1 4 5 3 2
1 4 5 3 1
1 4 5 2 1
2 5 4 3 2
2 5 4 3 1
2 5 4 2 1
2 5 3 2 1
2 4 3 2 1
2 3 5 4 3
2 3 5 4 2
2 3 5 4 1
2 3 5 3 2
2 3 5 3 1
2 3 5 2 1
2 3 4 3 2
2 3 4 3 1
2 3 4 2 1
2 3 4 5 4
2 3 4 5 3
2 3 4 5 2
2 3 4 5 1
2 4 5 4 3
2 4 5 4 2
2 4 5 4 1
2 4 5 3 2
2 4 5 3 1
2 4 5 2 1
3 5 4 3 2
3 5 4 3 1
3 5 4 2 1
3 5 3 2 1
3 4 3 2 1
3 4 5 4 3
3 4 5 4 2
3 4 5 4 1
3 4 5 3 2
3 4 5 3 1
3 4 5 2 1
4 5 4 3 2
4 5 4 3 1
4 5 4 2 1
4 5 3 2 1
# Python 3 program for
# Generate fixed bitonic sequence of n natural number
class Combination :
def printResult(self, result, n) :
i = 0
while (i < n) :
print(" ", result[i], end = "")
i += 1
print(end = "\n")
def sequence(self, result, value, index, flag, n) :
if (index == n) :
# Display result
self.printResult(result, n)
elif (value > n or value < 1 or
(index > 0 and result[index - 1] == value) or
(index == n - 1 and value == n)) :
# When following conditions are occur
# ➀ When value is greater than n or
# ➁ When value is less than 1 or
# ➂ When consecutive elements are same or
# ➃ Last element is a nth natural number
return
else :
result[index] = value
if (flag == 1) :
self.sequence(result, n, index + 1, 0, n)
self.sequence(result, value + 1, index + 1, 1, n)
self.sequence(result, value + 1, index, 1, n)
else :
self.sequence(result, value - 1, index + 1, 0, n)
self.sequence(result, value - 1, index, 0, n)
def findSequence(self, n) :
if (n < 2) :
return
# Auxiliary list which is collect result
result = [0] * (n)
print("\n Given N : ", n )
self.sequence(result, 1, 0, 1, n)
def main() :
task = Combination()
n = 3
# 1 3 2
# 1 3 1
# 1 2 1
# 2 3 2
# 2 3 1
# --------
# Possible bitonic sequence
task.findSequence(n)
n = 5
task.findSequence(n)
if __name__ == "__main__": main()
Output
Given N : 3
1 3 2
1 3 1
1 2 1
2 3 2
2 3 1
Given N : 5
1 5 4 3 2
1 5 4 3 1
1 5 4 2 1
1 5 3 2 1
1 4 3 2 1
1 2 5 4 3
1 2 5 4 2
1 2 5 4 1
1 2 5 3 2
1 2 5 3 1
1 2 5 2 1
1 2 4 3 2
1 2 4 3 1
1 2 4 2 1
1 2 3 2 1
1 2 3 5 4
1 2 3 5 3
1 2 3 5 2
1 2 3 5 1
1 2 3 4 3
1 2 3 4 2
1 2 3 4 1
1 2 4 5 4
1 2 4 5 3
1 2 4 5 2
1 2 4 5 1
1 3 5 4 3
1 3 5 4 2
1 3 5 4 1
1 3 5 3 2
1 3 5 3 1
1 3 5 2 1
1 3 4 3 2
1 3 4 3 1
1 3 4 2 1
1 3 4 5 4
1 3 4 5 3
1 3 4 5 2
1 3 4 5 1
1 4 5 4 3
1 4 5 4 2
1 4 5 4 1
1 4 5 3 2
1 4 5 3 1
1 4 5 2 1
2 5 4 3 2
2 5 4 3 1
2 5 4 2 1
2 5 3 2 1
2 4 3 2 1
2 3 5 4 3
2 3 5 4 2
2 3 5 4 1
2 3 5 3 2
2 3 5 3 1
2 3 5 2 1
2 3 4 3 2
2 3 4 3 1
2 3 4 2 1
2 3 4 5 4
2 3 4 5 3
2 3 4 5 2
2 3 4 5 1
2 4 5 4 3
2 4 5 4 2
2 4 5 4 1
2 4 5 3 2
2 4 5 3 1
2 4 5 2 1
3 5 4 3 2
3 5 4 3 1
3 5 4 2 1
3 5 3 2 1
3 4 3 2 1
3 4 5 4 3
3 4 5 4 2
3 4 5 4 1
3 4 5 3 2
3 4 5 3 1
3 4 5 2 1
4 5 4 3 2
4 5 4 3 1
4 5 4 2 1
4 5 3 2 1
# Ruby program for
# Generate fixed bitonic sequence of n natural number
class Combination
def printResult(result, n)
i = 0
while (i < n)
print(" ", result[i])
i += 1
end
print("\n")
end
def sequence(result, value, index, flag, n)
if (index == n)
# Display result
self.printResult(result, n)
elsif (value > n || value < 1 ||
(index > 0 && result[index - 1] == value) ||
(index == n - 1 && value == n))
# When following conditions are occur
# ➀ When value is greater than n or
# ➁ When value is less than 1 or
# ➂ When consecutive elements are same or
# ➃ Last element is a nth natural number
return
else
result[index] = value
if (flag == 1)
self.sequence(result, n, index + 1, 0, n)
self.sequence(result, value + 1, index + 1, 1, n)
self.sequence(result, value + 1, index, 1, n)
else
self.sequence(result, value - 1, index + 1, 0, n)
self.sequence(result, value - 1, index, 0, n)
end
end
end
def findSequence(n)
if (n < 2)
return
end
# Auxiliary array which is collect result
result = Array.new(n) {0}
print("\n Given N : ", n ,"\n")
self.sequence(result, 1, 0, 1, n)
end
end
def main()
task = Combination.new()
n = 3
# 1 3 2
# 1 3 1
# 1 2 1
# 2 3 2
# 2 3 1
# --------
# Possible bitonic sequence
task.findSequence(n)
n = 5
task.findSequence(n)
end
main()
Output
Given N : 3
1 3 2
1 3 1
1 2 1
2 3 2
2 3 1
Given N : 5
1 5 4 3 2
1 5 4 3 1
1 5 4 2 1
1 5 3 2 1
1 4 3 2 1
1 2 5 4 3
1 2 5 4 2
1 2 5 4 1
1 2 5 3 2
1 2 5 3 1
1 2 5 2 1
1 2 4 3 2
1 2 4 3 1
1 2 4 2 1
1 2 3 2 1
1 2 3 5 4
1 2 3 5 3
1 2 3 5 2
1 2 3 5 1
1 2 3 4 3
1 2 3 4 2
1 2 3 4 1
1 2 4 5 4
1 2 4 5 3
1 2 4 5 2
1 2 4 5 1
1 3 5 4 3
1 3 5 4 2
1 3 5 4 1
1 3 5 3 2
1 3 5 3 1
1 3 5 2 1
1 3 4 3 2
1 3 4 3 1
1 3 4 2 1
1 3 4 5 4
1 3 4 5 3
1 3 4 5 2
1 3 4 5 1
1 4 5 4 3
1 4 5 4 2
1 4 5 4 1
1 4 5 3 2
1 4 5 3 1
1 4 5 2 1
2 5 4 3 2
2 5 4 3 1
2 5 4 2 1
2 5 3 2 1
2 4 3 2 1
2 3 5 4 3
2 3 5 4 2
2 3 5 4 1
2 3 5 3 2
2 3 5 3 1
2 3 5 2 1
2 3 4 3 2
2 3 4 3 1
2 3 4 2 1
2 3 4 5 4
2 3 4 5 3
2 3 4 5 2
2 3 4 5 1
2 4 5 4 3
2 4 5 4 2
2 4 5 4 1
2 4 5 3 2
2 4 5 3 1
2 4 5 2 1
3 5 4 3 2
3 5 4 3 1
3 5 4 2 1
3 5 3 2 1
3 4 3 2 1
3 4 5 4 3
3 4 5 4 2
3 4 5 4 1
3 4 5 3 2
3 4 5 3 1
3 4 5 2 1
4 5 4 3 2
4 5 4 3 1
4 5 4 2 1
4 5 3 2 1
// Scala program for
// Generate fixed bitonic sequence of n natural number
class Combination()
{
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],
value: Int, index: Int,
flag: Int, n: Int): Unit = {
if (index == n)
{
// Display result
printResult(result, n);
}
else if (value > n || value < 1 ||
(index > 0 && result(index - 1) == value) ||
(index == n - 1 && value == n))
{
// When following conditions are occur
// ➀ When value is greater than n or
// ➁ When value is less than 1 or
// ➂ When consecutive elements are same or
// ➃ Last element is a nth natural number
return;
}
else
{
result(index) = value;
if (flag == 1)
{
sequence(result, n, index + 1, 0, n);
sequence(result, value + 1, index + 1, 1, n);
sequence(result, value + 1, index, 1, n);
}
else
{
sequence(result, value - 1, index + 1, 0, n);
sequence(result, value - 1, index, 0, n);
}
}
}
def findSequence(n: Int): Unit = {
if (n < 2)
{
return;
}
// Auxiliary array which is collect result
var result: Array[Int] = Array.fill[Int](n)(0);
print("\n Given N : " + n + "\n");
sequence(result, 1, 0, 1, n);
}
}
object Main
{
def main(args: Array[String]): Unit = {
var task: Combination = new Combination();
var n: Int = 3;
/*
1 3 2
1 3 1
1 2 1
2 3 2
2 3 1
--------
Possible bitonic sequence
*/
task.findSequence(n);
n = 5;
task.findSequence(n);
}
}
Output
Given N : 3
1 3 2
1 3 1
1 2 1
2 3 2
2 3 1
Given N : 5
1 5 4 3 2
1 5 4 3 1
1 5 4 2 1
1 5 3 2 1
1 4 3 2 1
1 2 5 4 3
1 2 5 4 2
1 2 5 4 1
1 2 5 3 2
1 2 5 3 1
1 2 5 2 1
1 2 4 3 2
1 2 4 3 1
1 2 4 2 1
1 2 3 2 1
1 2 3 5 4
1 2 3 5 3
1 2 3 5 2
1 2 3 5 1
1 2 3 4 3
1 2 3 4 2
1 2 3 4 1
1 2 4 5 4
1 2 4 5 3
1 2 4 5 2
1 2 4 5 1
1 3 5 4 3
1 3 5 4 2
1 3 5 4 1
1 3 5 3 2
1 3 5 3 1
1 3 5 2 1
1 3 4 3 2
1 3 4 3 1
1 3 4 2 1
1 3 4 5 4
1 3 4 5 3
1 3 4 5 2
1 3 4 5 1
1 4 5 4 3
1 4 5 4 2
1 4 5 4 1
1 4 5 3 2
1 4 5 3 1
1 4 5 2 1
2 5 4 3 2
2 5 4 3 1
2 5 4 2 1
2 5 3 2 1
2 4 3 2 1
2 3 5 4 3
2 3 5 4 2
2 3 5 4 1
2 3 5 3 2
2 3 5 3 1
2 3 5 2 1
2 3 4 3 2
2 3 4 3 1
2 3 4 2 1
2 3 4 5 4
2 3 4 5 3
2 3 4 5 2
2 3 4 5 1
2 4 5 4 3
2 4 5 4 2
2 4 5 4 1
2 4 5 3 2
2 4 5 3 1
2 4 5 2 1
3 5 4 3 2
3 5 4 3 1
3 5 4 2 1
3 5 3 2 1
3 4 3 2 1
3 4 5 4 3
3 4 5 4 2
3 4 5 4 1
3 4 5 3 2
3 4 5 3 1
3 4 5 2 1
4 5 4 3 2
4 5 4 3 1
4 5 4 2 1
4 5 3 2 1
// Swift 4 program for
// Generate fixed bitonic sequence of n natural number
class Combination
{
func printResult(_ result: [Int], _ n: Int)
{
var i: Int = 0;
while (i < n)
{
print(" ", result[i], terminator: "");
i += 1;
}
print(terminator: "\n");
}
func sequence(
_ result: inout[Int],
_ value: Int,
_ index: Int,
_ flag: Int,
_ n: Int)
{
if (index == n)
{
// Display result
self.printResult(result, n);
}
else if (value > n || value < 1 ||
(index > 0 && result[index - 1] == value) ||
(index == n - 1 && value == n))
{
// When following conditions are occur
// ➀ When value is greater than n or
// ➁ When value is less than 1 or
// ➂ When consecutive elements are same or
// ➃ Last element is a nth natural number
return;
}
else
{
result[index] = value;
if (flag == 1)
{
self.sequence(&result, n, index + 1, 0, n);
self.sequence(&result, value + 1, index + 1, 1, n);
self.sequence(&result, value + 1, index, 1, n);
}
else
{
self.sequence(&result, value - 1, index + 1, 0, n);
self.sequence(&result, value - 1, index, 0, n);
}
}
}
func findSequence(_ n: Int)
{
if (n < 2)
{
return;
}
// Auxiliary array which is collect result
var result: [Int] = Array(repeating: 0, count: n);
print("\n Given N : ", n );
self.sequence(&result, 1, 0, 1, n);
}
}
func main()
{
let task: Combination = Combination();
var n: Int = 3;
/*
1 3 2
1 3 1
1 2 1
2 3 2
2 3 1
--------
Possible bitonic sequence
*/
task.findSequence(n);
n = 5;
task.findSequence(n);
}
main();
Output
Given N : 3
1 3 2
1 3 1
1 2 1
2 3 2
2 3 1
Given N : 5
1 5 4 3 2
1 5 4 3 1
1 5 4 2 1
1 5 3 2 1
1 4 3 2 1
1 2 5 4 3
1 2 5 4 2
1 2 5 4 1
1 2 5 3 2
1 2 5 3 1
1 2 5 2 1
1 2 4 3 2
1 2 4 3 1
1 2 4 2 1
1 2 3 2 1
1 2 3 5 4
1 2 3 5 3
1 2 3 5 2
1 2 3 5 1
1 2 3 4 3
1 2 3 4 2
1 2 3 4 1
1 2 4 5 4
1 2 4 5 3
1 2 4 5 2
1 2 4 5 1
1 3 5 4 3
1 3 5 4 2
1 3 5 4 1
1 3 5 3 2
1 3 5 3 1
1 3 5 2 1
1 3 4 3 2
1 3 4 3 1
1 3 4 2 1
1 3 4 5 4
1 3 4 5 3
1 3 4 5 2
1 3 4 5 1
1 4 5 4 3
1 4 5 4 2
1 4 5 4 1
1 4 5 3 2
1 4 5 3 1
1 4 5 2 1
2 5 4 3 2
2 5 4 3 1
2 5 4 2 1
2 5 3 2 1
2 4 3 2 1
2 3 5 4 3
2 3 5 4 2
2 3 5 4 1
2 3 5 3 2
2 3 5 3 1
2 3 5 2 1
2 3 4 3 2
2 3 4 3 1
2 3 4 2 1
2 3 4 5 4
2 3 4 5 3
2 3 4 5 2
2 3 4 5 1
2 4 5 4 3
2 4 5 4 2
2 4 5 4 1
2 4 5 3 2
2 4 5 3 1
2 4 5 2 1
3 5 4 3 2
3 5 4 3 1
3 5 4 2 1
3 5 3 2 1
3 4 3 2 1
3 4 5 4 3
3 4 5 4 2
3 4 5 4 1
3 4 5 3 2
3 4 5 3 1
3 4 5 2 1
4 5 4 3 2
4 5 4 3 1
4 5 4 2 1
4 5 3 2 1
// Kotlin program for
// Generate fixed bitonic sequence of n natural number
class Combination
{
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 > ,
value: Int,
index: Int,
flag: Int,
n: Int): Unit
{
if (index == n)
{
// Display result
this.printResult(result, n);
}
else if (value > n || value < 1 ||
(index > 0 && result[index - 1] == value) ||
(index == n - 1 && value == n))
{
// When following conditions are occur
// ➀ When value is greater than n or
// ➁ When value is less than 1 or
// ➂ When consecutive elements are same or
// ➃ Last element is a nth natural number
return;
}
else
{
result[index] = value;
if (flag == 1)
{
this.sequence(result, n, index + 1, 0, n);
this.sequence(result, value + 1, index + 1, 1, n);
this.sequence(result, value + 1, index, 1, n);
}
else
{
this.sequence(result, value - 1, index + 1, 0, n);
this.sequence(result, value - 1, index, 0, n);
}
}
}
fun findSequence(n: Int): Unit
{
if (n < 2)
{
return;
}
// Auxiliary array which is collect result
val result: Array < Int > = Array(n)
{
0
};
print("\n Given N : " + n + "\n");
this.sequence(result, 1, 0, 1, n);
}
}
fun main(args: Array < String > ): Unit
{
val task: Combination = Combination();
var n: Int = 3;
/*
1 3 2
1 3 1
1 2 1
2 3 2
2 3 1
--------
Possible bitonic sequence
*/
task.findSequence(n);
n = 5;
task.findSequence(n);
}
Output
Given N : 3
1 3 2
1 3 1
1 2 1
2 3 2
2 3 1
Given N : 5
1 5 4 3 2
1 5 4 3 1
1 5 4 2 1
1 5 3 2 1
1 4 3 2 1
1 2 5 4 3
1 2 5 4 2
1 2 5 4 1
1 2 5 3 2
1 2 5 3 1
1 2 5 2 1
1 2 4 3 2
1 2 4 3 1
1 2 4 2 1
1 2 3 2 1
1 2 3 5 4
1 2 3 5 3
1 2 3 5 2
1 2 3 5 1
1 2 3 4 3
1 2 3 4 2
1 2 3 4 1
1 2 4 5 4
1 2 4 5 3
1 2 4 5 2
1 2 4 5 1
1 3 5 4 3
1 3 5 4 2
1 3 5 4 1
1 3 5 3 2
1 3 5 3 1
1 3 5 2 1
1 3 4 3 2
1 3 4 3 1
1 3 4 2 1
1 3 4 5 4
1 3 4 5 3
1 3 4 5 2
1 3 4 5 1
1 4 5 4 3
1 4 5 4 2
1 4 5 4 1
1 4 5 3 2
1 4 5 3 1
1 4 5 2 1
2 5 4 3 2
2 5 4 3 1
2 5 4 2 1
2 5 3 2 1
2 4 3 2 1
2 3 5 4 3
2 3 5 4 2
2 3 5 4 1
2 3 5 3 2
2 3 5 3 1
2 3 5 2 1
2 3 4 3 2
2 3 4 3 1
2 3 4 2 1
2 3 4 5 4
2 3 4 5 3
2 3 4 5 2
2 3 4 5 1
2 4 5 4 3
2 4 5 4 2
2 4 5 4 1
2 4 5 3 2
2 4 5 3 1
2 4 5 2 1
3 5 4 3 2
3 5 4 3 1
3 5 4 2 1
3 5 3 2 1
3 4 3 2 1
3 4 5 4 3
3 4 5 4 2
3 4 5 4 1
3 4 5 3 2
3 4 5 3 1
3 4 5 2 1
4 5 4 3 2
4 5 4 3 1
4 5 4 2 1
4 5 3 2 1
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