Posted on by Kalkicode
Code Backtracking

Generate all bitonic sequence of given range and size

Here given code implementation process.

// C Program
// Generate all bitonic sequence of given range and size
#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, 
          int s, 
            int e)
{
	if (index == n)
	{
		// Display result
		printResult(result, n);
		return;
	}
	else if (value > e || value < s || 
             (index > 0 && result[index - 1] == value) || 
             (index == n - 1 && value == e))
	{
		// When following conditions are occur
		// ➀  When value is greater than range  or  
		// ➁  When value is less than given range  or    
		// ➂  When consecutive elements are same  or
		// ➃  Last element is e  
		return;
	}
	result[index] = value;
	if (flag == 1)
	{
		sequence(result, e, index + 1, 0, n, s, e);
		sequence(result, value + 1, index + 1, 1, n, s, e);
		sequence(result, value + 1, index, 1, n, s, e);
	}
	else
	{
		sequence(result, value - 1, index + 1, 0, n, s, e);
		sequence(result, value - 1, index, 0, n, s, e);
	}
}
void findSequence(int n, int s, int e)
{
	if (n < 2)
	{
		return;
	}
	int result[n];
	printf("\n  Given N : %d\n", n);
	sequence(result, s, 0, 1, n, s, e);
}
int main(int argc, char const *argv[])
{
	// Size
	int n = 4;
	// Range (3..7)
	int s = 3;
	int e = 7;
	findSequence(n, s, e);
	return 0;
}

Output

  Given N : 4
  3  7  6  5
  3  7  6  4
  3  7  6  3
  3  7  5  4
  3  7  5  3
  3  7  4  3
  3  6  5  4
  3  6  5  3
  3  6  4  3
  3  5  4  3
  3  4  7  6
  3  4  7  5
  3  4  7  4
  3  4  7  3
  3  4  6  5
  3  4  6  4
  3  4  6  3
  3  4  5  4
  3  4  5  3
  3  4  5  6
  3  4  5  6
  3  5  7  6
  3  5  7  5
  3  5  7  4
  3  5  7  3
  3  5  6  5
  3  5  6  4
  3  5  6  3
  3  6  7  6
  3  6  7  5
  3  6  7  4
  3  6  7  3
  4  7  6  5
  4  7  6  4
  4  7  6  3
  4  7  5  4
  4  7  5  3
  4  7  4  3
  4  6  5  4
  4  6  5  3
  4  6  4  3
  4  5  4  3
  4  5  7  6
  4  5  7  5
  4  5  7  4
  4  5  7  3
  4  5  6  5
  4  5  6  4
  4  5  6  3
  4  6  7  6
  4  6  7  5
  4  6  7  4
  4  6  7  3
  5  7  6  5
  5  7  6  4
  5  7  6  3
  5  7  5  4
  5  7  5  3
  5  7  4  3
  5  6  5  4
  5  6  5  3
  5  6  4  3
  5  6  7  6
  5  6  7  5
  5  6  7  4
  5  6  7  3
  6  7  6  5
  6  7  6  4
  6  7  6  3
  6  7  5  4
  6  7  5  3
  6  7  4  3
// Java program for
// Generate all bitonic sequence of given range and size
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, 
                         int s, 
                         int e)
	{
		if (index == n)
		{
			// Display result
			printResult(result, n);
			return;
		}
		else if (value > e || value < s || 
                 (index > 0 && result[index - 1] == value) || 
                 (index == n - 1 && value == e))
		{
			// When following conditions are occur
			// ➀  When value is greater than range  or  
			// ➁  When value is less than given range  or    
			// ➂  When consecutive elements are same  or
			// ➃  Last element is e  
			return;
		}
		result[index] = value;
		if (flag == 1)
		{
			sequence(result, e, index + 1, 0, n, s, e);
			sequence(result, value + 1, index + 1, 1, n, s, e);
			sequence(result, value + 1, index, 1, n, s, e);
		}
		else
		{
			sequence(result, value - 1, index + 1, 0, n, s, e);
			sequence(result, value - 1, index, 0, n, s, e);
		}
	}
	public void findSequence(int n, int s, int e)
	{
		if (n < 2)
		{
			return;
		}
		int[] result = new int[n];
		System.out.print("\n Given N : " + n + "\n");
		sequence(result, s, 0, 1, n, s, e);
	}
	public static void main(String[] args)
	{
		Combination task = new Combination();
		// Size
		int n = 4;
		// Range (3..7)
		int s = 3;
		int e = 7;
		task.findSequence(n, s, e);
	}
}

Output

 Given N : 4
 3 7 6 5
 3 7 6 4
 3 7 6 3
 3 7 5 4
 3 7 5 3
 3 7 4 3
 3 6 5 4
 3 6 5 3
 3 6 4 3
 3 5 4 3
 3 4 7 6
 3 4 7 5
 3 4 7 4
 3 4 7 3
 3 4 6 5
 3 4 6 4
 3 4 6 3
 3 4 5 4
 3 4 5 3
 3 4 5 6
 3 4 5 6
 3 5 7 6
 3 5 7 5
 3 5 7 4
 3 5 7 3
 3 5 6 5
 3 5 6 4
 3 5 6 3
 3 6 7 6
 3 6 7 5
 3 6 7 4
 3 6 7 3
 4 7 6 5
 4 7 6 4
 4 7 6 3
 4 7 5 4
 4 7 5 3
 4 7 4 3
 4 6 5 4
 4 6 5 3
 4 6 4 3
 4 5 4 3
 4 5 7 6
 4 5 7 5
 4 5 7 4
 4 5 7 3
 4 5 6 5
 4 5 6 4
 4 5 6 3
 4 6 7 6
 4 6 7 5
 4 6 7 4
 4 6 7 3
 5 7 6 5
 5 7 6 4
 5 7 6 3
 5 7 5 4
 5 7 5 3
 5 7 4 3
 5 6 5 4
 5 6 5 3
 5 6 4 3
 5 6 7 6
 5 6 7 5
 5 6 7 4
 5 6 7 3
 6 7 6 5
 6 7 6 4
 6 7 6 3
 6 7 5 4
 6 7 5 3
 6 7 4 3
// Include header file
#include <iostream>

using namespace std;
// C++ program for
// Generate all bitonic sequence of given range and size
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, 
            int s, 
              int e)
	{
		if (index == n)
		{
			// Display result
			this->printResult(result, n);
			return;
		}
		else if (value > e || value < s || 
                 (index > 0 && result[index - 1] == value) || 
                 (index == n - 1 && value == e))
		{
			// When following conditions are occur
			// ➀  When value is greater than range  or  
			// ➁  When value is less than given range  or    
			// ➂  When consecutive elements are same  or
			// ➃  Last element is e  
			return;
		}
		result[index] = value;
		if (flag == 1)
		{
			this->sequence(result, e, index + 1, 0, n, s, e);
			this->sequence(result, value + 1, index + 1, 1, n, s, e);
			this->sequence(result, value + 1, index, 1, n, s, e);
		}
		else
		{
			this->sequence(result, value - 1, index + 1, 0, n, s, e);
			this->sequence(result, value - 1, index, 0, n, s, e);
		}
	}
	void findSequence(int n, int s, int e)
	{
		if (n < 2)
		{
			return;
		}
		int result[n];
		cout << "\n Given N : " << n << "\n";
		this->sequence(result, s, 0, 1, n, s, e);
	}
};
int main()
{
	Combination *task = new Combination();
	// Size
	int n = 4;
	// Range (3..7)
	int s = 3;
	int e = 7;
	task->findSequence(n, s, e);
	return 0;
}

Output

 Given N : 4
 3 7 6 5
 3 7 6 4
 3 7 6 3
 3 7 5 4
 3 7 5 3
 3 7 4 3
 3 6 5 4
 3 6 5 3
 3 6 4 3
 3 5 4 3
 3 4 7 6
 3 4 7 5
 3 4 7 4
 3 4 7 3
 3 4 6 5
 3 4 6 4
 3 4 6 3
 3 4 5 4
 3 4 5 3
 3 4 5 6
 3 4 5 6
 3 5 7 6
 3 5 7 5
 3 5 7 4
 3 5 7 3
 3 5 6 5
 3 5 6 4
 3 5 6 3
 3 6 7 6
 3 6 7 5
 3 6 7 4
 3 6 7 3
 4 7 6 5
 4 7 6 4
 4 7 6 3
 4 7 5 4
 4 7 5 3
 4 7 4 3
 4 6 5 4
 4 6 5 3
 4 6 4 3
 4 5 4 3
 4 5 7 6
 4 5 7 5
 4 5 7 4
 4 5 7 3
 4 5 6 5
 4 5 6 4
 4 5 6 3
 4 6 7 6
 4 6 7 5
 4 6 7 4
 4 6 7 3
 5 7 6 5
 5 7 6 4
 5 7 6 3
 5 7 5 4
 5 7 5 3
 5 7 4 3
 5 6 5 4
 5 6 5 3
 5 6 4 3
 5 6 7 6
 5 6 7 5
 5 6 7 4
 5 6 7 3
 6 7 6 5
 6 7 6 4
 6 7 6 3
 6 7 5 4
 6 7 5 3
 6 7 4 3
// Include namespace system
using System;
// Csharp program for
// Generate all bitonic sequence of given range and size
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, 
                         int s, 
                         int e)
	{
		if (index == n)
		{
			// Display result
			this.printResult(result, n);
			return;
		}
		else if (value > e || value < s || 
                 (index > 0 && result[index - 1] == value) || 
                 (index == n - 1 && value == e))
		{
			// When following conditions are occur
			// ➀  When value is greater than range  or  
			// ➁  When value is less than given range  or    
			// ➂  When consecutive elements are same  or
			// ➃  Last element is e  
			return;
		}
		result[index] = value;
		if (flag == 1)
		{
			this.sequence(result, e, index + 1, 0, n, s, e);
			this.sequence(result, value + 1, index + 1, 1, n, s, e);
			this.sequence(result, value + 1, index, 1, n, s, e);
		}
		else
		{
			this.sequence(result, value - 1, index + 1, 0, n, s, e);
			this.sequence(result, value - 1, index, 0, n, s, e);
		}
	}
	public void findSequence(int n, int s, int e)
	{
		if (n < 2)
		{
			return;
		}
		int[] result = new int[n];
		Console.Write("\n Given N : " + n + "\n");
		this.sequence(result, s, 0, 1, n, s, e);
	}
	public static void Main(String[] args)
	{
		Combination task = new Combination();
		// Size
		int n = 4;
		// Range (3..7)
		int s = 3;
		int e = 7;
		task.findSequence(n, s, e);
	}
}

Output

 Given N : 4
 3 7 6 5
 3 7 6 4
 3 7 6 3
 3 7 5 4
 3 7 5 3
 3 7 4 3
 3 6 5 4
 3 6 5 3
 3 6 4 3
 3 5 4 3
 3 4 7 6
 3 4 7 5
 3 4 7 4
 3 4 7 3
 3 4 6 5
 3 4 6 4
 3 4 6 3
 3 4 5 4
 3 4 5 3
 3 4 5 6
 3 4 5 6
 3 5 7 6
 3 5 7 5
 3 5 7 4
 3 5 7 3
 3 5 6 5
 3 5 6 4
 3 5 6 3
 3 6 7 6
 3 6 7 5
 3 6 7 4
 3 6 7 3
 4 7 6 5
 4 7 6 4
 4 7 6 3
 4 7 5 4
 4 7 5 3
 4 7 4 3
 4 6 5 4
 4 6 5 3
 4 6 4 3
 4 5 4 3
 4 5 7 6
 4 5 7 5
 4 5 7 4
 4 5 7 3
 4 5 6 5
 4 5 6 4
 4 5 6 3
 4 6 7 6
 4 6 7 5
 4 6 7 4
 4 6 7 3
 5 7 6 5
 5 7 6 4
 5 7 6 3
 5 7 5 4
 5 7 5 3
 5 7 4 3
 5 6 5 4
 5 6 5 3
 5 6 4 3
 5 6 7 6
 5 6 7 5
 5 6 7 4
 5 6 7 3
 6 7 6 5
 6 7 6 4
 6 7 6 3
 6 7 5 4
 6 7 5 3
 6 7 4 3
package main
import "fmt"
// Go program for
// Generate all bitonic sequence of given range and size
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, 
	s int, e int) {
	if index == n {
		// Display result
		this.printResult(result, n)
		return
	} else if value > e || value < s || 
	(index > 0 && result[index - 1] == value) || 
	(index == n - 1 && value == e) {
		// When following conditions are occur
		// ➀  When value is greater than range  or  
		// ➁  When value is less than given range  or    
		// ➂  When consecutive elements are same  or
		// ➃  Last element is e  
		return
	}
	result[index] = value
	if flag == 1 {
		this.sequence(result, e, index + 1, 0, n, s, e)
		this.sequence(result, value + 1, index + 1, 1, n, s, e)
		this.sequence(result, value + 1, index, 1, n, s, e)
	} else {
		this.sequence(result, value - 1, index + 1, 0, n, s, e)
		this.sequence(result, value - 1, index, 0, n, s, e)
	}
}
func(this Combination) findSequence(n, s, e int) {
	if n < 2 {
		return
	}
	var result = make([] int, n)
	fmt.Print("\n Given N : ", n, "\n")
	this.sequence(result, s, 0, 1, n, s, e)
}
func main() {
	var task * Combination = getCombination()
	// Size
	var n int = 4
	// Range (3..7)
	var s int = 3
	var e int = 7
	task.findSequence(n, s, e)
}

Output

 Given N : 4
 3 7 6 5
 3 7 6 4
 3 7 6 3
 3 7 5 4
 3 7 5 3
 3 7 4 3
 3 6 5 4
 3 6 5 3
 3 6 4 3
 3 5 4 3
 3 4 7 6
 3 4 7 5
 3 4 7 4
 3 4 7 3
 3 4 6 5
 3 4 6 4
 3 4 6 3
 3 4 5 4
 3 4 5 3
 3 4 5 6
 3 4 5 6
 3 5 7 6
 3 5 7 5
 3 5 7 4
 3 5 7 3
 3 5 6 5
 3 5 6 4
 3 5 6 3
 3 6 7 6
 3 6 7 5
 3 6 7 4
 3 6 7 3
 4 7 6 5
 4 7 6 4
 4 7 6 3
 4 7 5 4
 4 7 5 3
 4 7 4 3
 4 6 5 4
 4 6 5 3
 4 6 4 3
 4 5 4 3
 4 5 7 6
 4 5 7 5
 4 5 7 4
 4 5 7 3
 4 5 6 5
 4 5 6 4
 4 5 6 3
 4 6 7 6
 4 6 7 5
 4 6 7 4
 4 6 7 3
 5 7 6 5
 5 7 6 4
 5 7 6 3
 5 7 5 4
 5 7 5 3
 5 7 4 3
 5 6 5 4
 5 6 5 3
 5 6 4 3
 5 6 7 6
 5 6 7 5
 5 6 7 4
 5 6 7 3
 6 7 6 5
 6 7 6 4
 6 7 6 3
 6 7 5 4
 6 7 5 3
 6 7 4 3
<?php
// Php program for
// Generate all bitonic sequence of given range and size
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, 
                               $s, 
                               $e)
	{
		if ($index == $n)
		{
			// Display result
			$this->printResult($result, $n);
			return;
		}
		else if ($value > $e || $value < $s || 
                 ($index > 0 && $result[$index - 1] == $value) || 
                 ($index == $n - 1 && $value == $e))
		{
			// When following conditions are occur
			// ➀  When value is greater than range  or  
			// ➁  When value is less than given range  or    
			// ➂  When consecutive elements are same  or
			// ➃  Last element is e  
			return;
		}
		$result[$index] = $value;
		if ($flag == 1)
		{
			$this->sequence($result, 
                            $e, $index + 1, 0, $n, $s, $e);
			$this->sequence($result, 
                            $value + 1, $index + 1, 1, $n, $s, $e);
			$this->sequence($result, 
                            $value + 1, $index, 1, $n, $s, $e);
		}
		else
		{
			$this->sequence($result, 
                            $value - 1, $index + 1, 0, $n, $s, $e);
			$this->sequence($result, 
                            $value - 1, $index, 0, $n, $s, $e);
		}
	}
	public	function findSequence($n, $s, $e)
	{
		if ($n < 2)
		{
			return;
		}
		$result = array_fill(0, $n, 0);
		echo("\n Given N : ".$n."\n");
		$this->sequence($result, $s, 0, 1, $n, $s, $e);
	}
}

function main()
{
	$task = new Combination();
	// Size
	$n = 4;
	// Range (3..7)
	$s = 3;
	$e = 7;
	$task->findSequence($n, $s, $e);
}
main();

Output

 Given N : 4
 3 7 6 5
 3 7 6 4
 3 7 6 3
 3 7 5 4
 3 7 5 3
 3 7 4 3
 3 6 5 4
 3 6 5 3
 3 6 4 3
 3 5 4 3
 3 4 7 6
 3 4 7 5
 3 4 7 4
 3 4 7 3
 3 4 6 5
 3 4 6 4
 3 4 6 3
 3 4 5 4
 3 4 5 3
 3 4 5 6
 3 4 5 6
 3 5 7 6
 3 5 7 5
 3 5 7 4
 3 5 7 3
 3 5 6 5
 3 5 6 4
 3 5 6 3
 3 6 7 6
 3 6 7 5
 3 6 7 4
 3 6 7 3
 4 7 6 5
 4 7 6 4
 4 7 6 3
 4 7 5 4
 4 7 5 3
 4 7 4 3
 4 6 5 4
 4 6 5 3
 4 6 4 3
 4 5 4 3
 4 5 7 6
 4 5 7 5
 4 5 7 4
 4 5 7 3
 4 5 6 5
 4 5 6 4
 4 5 6 3
 4 6 7 6
 4 6 7 5
 4 6 7 4
 4 6 7 3
 5 7 6 5
 5 7 6 4
 5 7 6 3
 5 7 5 4
 5 7 5 3
 5 7 4 3
 5 6 5 4
 5 6 5 3
 5 6 4 3
 5 6 7 6
 5 6 7 5
 5 6 7 4
 5 6 7 3
 6 7 6 5
 6 7 6 4
 6 7 6 3
 6 7 5 4
 6 7 5 3
 6 7 4 3
// Node JS program for
// Generate all bitonic sequence of given range and size
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, s, e)
	{
		if (index == n)
		{
			// Display result
			this.printResult(result, n);
			return;
		}
		else if (value > e || value < s || 
                 (index > 0 && result[index - 1] == value) || 
                 (index == n - 1 && value == e))
		{
			// When following conditions are occur
			// ➀  When value is greater than range  or  
			// ➁  When value is less than given range  or    
			// ➂  When consecutive elements are same  or
			// ➃  Last element is e  
			return;
		}
		result[index] = value;
		if (flag == 1)
		{
			this.sequence(result, e, index + 1, 0, n, s, e);
			this.sequence(result, value + 1, index + 1, 1, n, s, e);
			this.sequence(result, value + 1, index, 1, n, s, e);
		}
		else
		{
			this.sequence(result, value - 1, index + 1, 0, n, s, e);
			this.sequence(result, value - 1, index, 0, n, s, e);
		}
	}
	findSequence(n, s, e)
	{
		if (n < 2)
		{
			return;
		}
		var result = Array(n).fill(0);
		process.stdout.write("\n Given N : " + n + "\n");
		this.sequence(result, s, 0, 1, n, s, e);
	}
}

function main()
{
	var task = new Combination();
	// Size
	var n = 4;
	// Range (3..7)
	var s = 3;
	var e = 7;
	task.findSequence(n, s, e);
}
main();

Output

 Given N : 4
 3 7 6 5
 3 7 6 4
 3 7 6 3
 3 7 5 4
 3 7 5 3
 3 7 4 3
 3 6 5 4
 3 6 5 3
 3 6 4 3
 3 5 4 3
 3 4 7 6
 3 4 7 5
 3 4 7 4
 3 4 7 3
 3 4 6 5
 3 4 6 4
 3 4 6 3
 3 4 5 4
 3 4 5 3
 3 4 5 6
 3 4 5 6
 3 5 7 6
 3 5 7 5
 3 5 7 4
 3 5 7 3
 3 5 6 5
 3 5 6 4
 3 5 6 3
 3 6 7 6
 3 6 7 5
 3 6 7 4
 3 6 7 3
 4 7 6 5
 4 7 6 4
 4 7 6 3
 4 7 5 4
 4 7 5 3
 4 7 4 3
 4 6 5 4
 4 6 5 3
 4 6 4 3
 4 5 4 3
 4 5 7 6
 4 5 7 5
 4 5 7 4
 4 5 7 3
 4 5 6 5
 4 5 6 4
 4 5 6 3
 4 6 7 6
 4 6 7 5
 4 6 7 4
 4 6 7 3
 5 7 6 5
 5 7 6 4
 5 7 6 3
 5 7 5 4
 5 7 5 3
 5 7 4 3
 5 6 5 4
 5 6 5 3
 5 6 4 3
 5 6 7 6
 5 6 7 5
 5 6 7 4
 5 6 7 3
 6 7 6 5
 6 7 6 4
 6 7 6 3
 6 7 5 4
 6 7 5 3
 6 7 4 3
#  Python 3 program for
#  Generate all bitonic sequence of given range and size
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, s, e) :
		if (index == n) :
			#  Display result
			self.printResult(result, n)
			return
		elif (value > e or value < s or
              (index > 0 and result[index - 1] == value) or
        (index == n - 1 and value == e)) :
			#  When following conditions are occur
			#  ➀  When value is greater than range  or  
			#  ➁  When value is less than given range  or    
			#  ➂  When consecutive elements are same  or
			#  ➃  Last element is e  
			return
		
		result[index] = value
		if (flag == 1) :
			self.sequence(result, e, index + 1, 0, n, s, e)
			self.sequence(result, value + 1, index + 1, 1, n, s, e)
			self.sequence(result, value + 1, index, 1, n, s, e)
		else :
			self.sequence(result, value - 1, index + 1, 0, n, s, e)
			self.sequence(result, value - 1, index, 0, n, s, e)
		
	
	def findSequence(self, n, s, e) :
		if (n < 2) :
			return
		
		result = [0] * (n)
		print("\n Given N : ", n )
		self.sequence(result, s, 0, 1, n, s, e)
	

def main() :
	task = Combination()
	#  Size
	n = 4
	#  Range (3..7)
	s = 3
	e = 7
	task.findSequence(n, s, e)

if __name__ == "__main__": main()

Output

 Given N :  4
  3  7  6  5
  3  7  6  4
  3  7  6  3
  3  7  5  4
  3  7  5  3
  3  7  4  3
  3  6  5  4
  3  6  5  3
  3  6  4  3
  3  5  4  3
  3  4  7  6
  3  4  7  5
  3  4  7  4
  3  4  7  3
  3  4  6  5
  3  4  6  4
  3  4  6  3
  3  4  5  4
  3  4  5  3
  3  4  5  6
  3  4  5  6
  3  5  7  6
  3  5  7  5
  3  5  7  4
  3  5  7  3
  3  5  6  5
  3  5  6  4
  3  5  6  3
  3  6  7  6
  3  6  7  5
  3  6  7  4
  3  6  7  3
  4  7  6  5
  4  7  6  4
  4  7  6  3
  4  7  5  4
  4  7  5  3
  4  7  4  3
  4  6  5  4
  4  6  5  3
  4  6  4  3
  4  5  4  3
  4  5  7  6
  4  5  7  5
  4  5  7  4
  4  5  7  3
  4  5  6  5
  4  5  6  4
  4  5  6  3
  4  6  7  6
  4  6  7  5
  4  6  7  4
  4  6  7  3
  5  7  6  5
  5  7  6  4
  5  7  6  3
  5  7  5  4
  5  7  5  3
  5  7  4  3
  5  6  5  4
  5  6  5  3
  5  6  4  3
  5  6  7  6
  5  6  7  5
  5  6  7  4
  5  6  7  3
  6  7  6  5
  6  7  6  4
  6  7  6  3
  6  7  5  4
  6  7  5  3
  6  7  4  3
#  Ruby program for
#  Generate all bitonic sequence of given range and size
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, s, e) 
		if (index == n) 
			#  Display result
			self.printResult(result, n)
			return
		elsif (value > e || value < s || 
               (index > 0 && result[index - 1] == value) || 
               (index == n - 1 && value == e)) 
			#  When following conditions are occur
			#  ➀  When value is greater than range  or  
			#  ➁  When value is less than given range  or    
			#  ➂  When consecutive elements are same  or
			#  ➃  Last element is e  
			return
		end

		result[index] = value
		if (flag == 1) 
			self.sequence(result, e, index + 1, 0, n, s, e)
			self.sequence(result, value + 1, index + 1, 1, n, s, e)
			self.sequence(result, value + 1, index, 1, n, s, e)
		else
 
			self.sequence(result, value - 1, index + 1, 0, n, s, e)
			self.sequence(result, value - 1, index, 0, n, s, e)
		end

	end

	def findSequence(n, s, e) 
		if (n < 2) 
			return
		end

		result = Array.new(n) {0}
		print("\n Given N : ", n ,"\n")
		self.sequence(result, s, 0, 1, n, s, e)
	end

end

def main() 
	task = Combination.new()
	#  Size
	n = 4
	#  Range (3..7)
	s = 3
	e = 7
	task.findSequence(n, s, e)
end

main()

Output

 Given N : 4
 3 7 6 5
 3 7 6 4
 3 7 6 3
 3 7 5 4
 3 7 5 3
 3 7 4 3
 3 6 5 4
 3 6 5 3
 3 6 4 3
 3 5 4 3
 3 4 7 6
 3 4 7 5
 3 4 7 4
 3 4 7 3
 3 4 6 5
 3 4 6 4
 3 4 6 3
 3 4 5 4
 3 4 5 3
 3 4 5 6
 3 4 5 6
 3 5 7 6
 3 5 7 5
 3 5 7 4
 3 5 7 3
 3 5 6 5
 3 5 6 4
 3 5 6 3
 3 6 7 6
 3 6 7 5
 3 6 7 4
 3 6 7 3
 4 7 6 5
 4 7 6 4
 4 7 6 3
 4 7 5 4
 4 7 5 3
 4 7 4 3
 4 6 5 4
 4 6 5 3
 4 6 4 3
 4 5 4 3
 4 5 7 6
 4 5 7 5
 4 5 7 4
 4 5 7 3
 4 5 6 5
 4 5 6 4
 4 5 6 3
 4 6 7 6
 4 6 7 5
 4 6 7 4
 4 6 7 3
 5 7 6 5
 5 7 6 4
 5 7 6 3
 5 7 5 4
 5 7 5 3
 5 7 4 3
 5 6 5 4
 5 6 5 3
 5 6 4 3
 5 6 7 6
 5 6 7 5
 5 6 7 4
 5 6 7 3
 6 7 6 5
 6 7 6 4
 6 7 6 3
 6 7 5 4
 6 7 5 3
 6 7 4 3
// Scala program for
// Generate all bitonic sequence of given range and size
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, 
              s: Int, 
                e: Int): Unit = {
		if (index == n)
		{
			// Display result
			printResult(result, n);
			return;
		}
		else if (value > e || value < s || 
                  (index > 0 && result(index - 1) == value) || 
                    (index == n - 1 && value == e))
		{
			// When following conditions are occur
			// ➀  When value is greater than range  or  
			// ➁  When value is less than given range  or    
			// ➂  When consecutive elements are same  or
			// ➃  Last element is e  
			return;
		}
		result(index) = value;
		if (flag == 1)
		{
			sequence(result, e, index + 1, 0, n, s, e);
			sequence(result, value + 1, index + 1, 1, n, s, e);
			sequence(result, value + 1, index, 1, n, s, e);
		}
		else
		{
			sequence(result, value - 1, index + 1, 0, n, s, e);
			sequence(result, value - 1, index, 0, n, s, e);
		}
	}
	def findSequence(n: Int, s: Int, e: Int): Unit = {
		if (n < 2)
		{
			return;
		}
		var result: Array[Int] = Array.fill[Int](n)(0);
		print("\n Given N : " + n + "\n");
		sequence(result, s, 0, 1, n, s, e);
	}
}
object Main
{
	def main(args: Array[String]): Unit = {
		var task: Combination = new Combination();
		// Size
		var n: Int = 4;
		// Range (3..7)
		var s: Int = 3;
		var e: Int = 7;
		task.findSequence(n, s, e);
	}
}

Output

 Given N : 4
 3 7 6 5
 3 7 6 4
 3 7 6 3
 3 7 5 4
 3 7 5 3
 3 7 4 3
 3 6 5 4
 3 6 5 3
 3 6 4 3
 3 5 4 3
 3 4 7 6
 3 4 7 5
 3 4 7 4
 3 4 7 3
 3 4 6 5
 3 4 6 4
 3 4 6 3
 3 4 5 4
 3 4 5 3
 3 4 5 6
 3 4 5 6
 3 5 7 6
 3 5 7 5
 3 5 7 4
 3 5 7 3
 3 5 6 5
 3 5 6 4
 3 5 6 3
 3 6 7 6
 3 6 7 5
 3 6 7 4
 3 6 7 3
 4 7 6 5
 4 7 6 4
 4 7 6 3
 4 7 5 4
 4 7 5 3
 4 7 4 3
 4 6 5 4
 4 6 5 3
 4 6 4 3
 4 5 4 3
 4 5 7 6
 4 5 7 5
 4 5 7 4
 4 5 7 3
 4 5 6 5
 4 5 6 4
 4 5 6 3
 4 6 7 6
 4 6 7 5
 4 6 7 4
 4 6 7 3
 5 7 6 5
 5 7 6 4
 5 7 6 3
 5 7 5 4
 5 7 5 3
 5 7 4 3
 5 6 5 4
 5 6 5 3
 5 6 4 3
 5 6 7 6
 5 6 7 5
 5 6 7 4
 5 6 7 3
 6 7 6 5
 6 7 6 4
 6 7 6 3
 6 7 5 4
 6 7 5 3
 6 7 4 3
// Swift 4 program for
// Generate all bitonic sequence of given range and size
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, 
              _ s: Int, 
                _ e: Int)
	{
		if (index == n)
		{
			// Display result
			self.printResult(result, n);
			return;
		}
		else if (value > e || value < s || 
                 (index > 0 && result[index - 1] == value) || 
                 (index == n - 1 && value == e))
		{
			// When following conditions are occur
			// ➀  When value is greater than range  or  
			// ➁  When value is less than given range  or    
			// ➂  When consecutive elements are same  or
			// ➃  Last element is e  
			return;
		}
		result[index] = value;
		if (flag == 1)
		{
			self.sequence(&result, e, index + 1, 0, n, s, e);
			self.sequence(&result, value + 1, index + 1, 1, n, s, e);
			self.sequence(&result, value + 1, index, 1, n, s, e);
		}
		else
		{
			self.sequence(&result, value - 1, index + 1, 0, n, s, e);
			self.sequence(&result, value - 1, index, 0, n, s, e);
		}
	}
	func findSequence(_ n: Int, _ s: Int, _ e: Int)
	{
		if (n < 2)
		{
			return;
		}
		var result: [Int] = Array(repeating: 0, count: n);
		print("\n Given N : ", n );
		self.sequence(&result, s, 0, 1, n, s, e);
	}
}
func main()
{
	let task: Combination = Combination();
	// Size
	let n: Int = 4;
	// Range (3..7)
	let s: Int = 3;
	let e: Int = 7;
	task.findSequence(n, s, e);
}
main();

Output

 Given N :  4
  3  7  6  5
  3  7  6  4
  3  7  6  3
  3  7  5  4
  3  7  5  3
  3  7  4  3
  3  6  5  4
  3  6  5  3
  3  6  4  3
  3  5  4  3
  3  4  7  6
  3  4  7  5
  3  4  7  4
  3  4  7  3
  3  4  6  5
  3  4  6  4
  3  4  6  3
  3  4  5  4
  3  4  5  3
  3  4  5  6
  3  4  5  6
  3  5  7  6
  3  5  7  5
  3  5  7  4
  3  5  7  3
  3  5  6  5
  3  5  6  4
  3  5  6  3
  3  6  7  6
  3  6  7  5
  3  6  7  4
  3  6  7  3
  4  7  6  5
  4  7  6  4
  4  7  6  3
  4  7  5  4
  4  7  5  3
  4  7  4  3
  4  6  5  4
  4  6  5  3
  4  6  4  3
  4  5  4  3
  4  5  7  6
  4  5  7  5
  4  5  7  4
  4  5  7  3
  4  5  6  5
  4  5  6  4
  4  5  6  3
  4  6  7  6
  4  6  7  5
  4  6  7  4
  4  6  7  3
  5  7  6  5
  5  7  6  4
  5  7  6  3
  5  7  5  4
  5  7  5  3
  5  7  4  3
  5  6  5  4
  5  6  5  3
  5  6  4  3
  5  6  7  6
  5  6  7  5
  5  6  7  4
  5  6  7  3
  6  7  6  5
  6  7  6  4
  6  7  6  3
  6  7  5  4
  6  7  5  3
  6  7  4  3
// Kotlin program for
// Generate all bitonic sequence of given range and size
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, 
                 s: Int, e: Int): Unit
	{
		if (index == n)
		{
			// Display result
			this.printResult(result, n);
			return;
		}
		else if (value > e || value < s || 
                 (index > 0 && result[index - 1] == value) || 
                 (index == n - 1 && value == e))
		{
			// When following conditions are occur
			// ➀  When value is greater than range  or  
			// ➁  When value is less than given range  or    
			// ➂  When consecutive elements are same  or
			// ➃  Last element is e  
			return;
		}
		result[index] = value;
		if (flag == 1)
		{
			this.sequence(result, e, index + 1, 0, n, s, e);
			this.sequence(result, value + 1, index + 1, 1, n, s, e);
			this.sequence(result, value + 1, index, 1, n, s, e);
		}
		else
		{
			this.sequence(result, value - 1, index + 1, 0, n, s, e);
			this.sequence(result, value - 1, index, 0, n, s, e);
		}
	}
	fun findSequence(n: Int, s: Int, e: Int): Unit
	{
		if (n < 2)
		{
			return;
		}
		var result: Array < Int > = Array(n)
		{
			0
		};
		print("\n Given N : " + n + "\n");
		this.sequence(result, s, 0, 1, n, s, e);
	}
}
fun main(args: Array < String > ): Unit
{
	val task: Combination = Combination();
	// Size
	val n: Int = 4;
	// Range (3..7)
	val s: Int = 3;
	val e: Int = 7;
	task.findSequence(n, s, e);
}

Output

 Given N : 4
 3 7 6 5
 3 7 6 4
 3 7 6 3
 3 7 5 4
 3 7 5 3
 3 7 4 3
 3 6 5 4
 3 6 5 3
 3 6 4 3
 3 5 4 3
 3 4 7 6
 3 4 7 5
 3 4 7 4
 3 4 7 3
 3 4 6 5
 3 4 6 4
 3 4 6 3
 3 4 5 4
 3 4 5 3
 3 4 5 6
 3 4 5 6
 3 5 7 6
 3 5 7 5
 3 5 7 4
 3 5 7 3
 3 5 6 5
 3 5 6 4
 3 5 6 3
 3 6 7 6
 3 6 7 5
 3 6 7 4
 3 6 7 3
 4 7 6 5
 4 7 6 4
 4 7 6 3
 4 7 5 4
 4 7 5 3
 4 7 4 3
 4 6 5 4
 4 6 5 3
 4 6 4 3
 4 5 4 3
 4 5 7 6
 4 5 7 5
 4 5 7 4
 4 5 7 3
 4 5 6 5
 4 5 6 4
 4 5 6 3
 4 6 7 6
 4 6 7 5
 4 6 7 4
 4 6 7 3
 5 7 6 5
 5 7 6 4
 5 7 6 3
 5 7 5 4
 5 7 5 3
 5 7 4 3
 5 6 5 4
 5 6 5 3
 5 6 4 3
 5 6 7 6
 5 6 7 5
 5 6 7 4
 5 6 7 3
 6 7 6 5
 6 7 6 4
 6 7 6 3
 6 7 5 4
 6 7 5 3
 6 7 4 3

Comment

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