Posted on by Kalkicode
Code Backtracking

Print all the sequences of length m that begin with n and the consecutive difference is less than k

Here given code implementation process.

// C Program
// Print all the sequences of length m that begin with n 
// and the consecutive difference is less than k
#include <stdio.h>
 // Display result
void printSequence(int result[], int n)
{
	for (int i = 0; i < n; ++i)
	{
		printf("  %d", result[i]);
	}
	printf("\n");
}
int absValue(int x)
{
	if (x < 0)
	{
		return -x;
	}
	return x;
}
void findCombination(int n, int current, int k, 
                     int result[], int index, int m)
{
	if (index == m)
	{
		// Display calculated result
		printSequence(result, index);
		return;
	}
	if (index >= m)
	{
		// Base case when stop process
		return;
	}
	for (int i = current; i <= n; ++i)
	{
		if (absValue(result[index - 1] - i) < k)
		{
			// When pair difference less than k
			// Collects resultant value
			result[index] = i;
			// Find other combination using recursion
			findCombination(n, current, k, 
                            result, index + 1, m);
		}
	}
}
void combination(int n, int k, int m)
{
	if (m <= 0 || k <= 0)
	{
		return;
	}
	// Display given values
	printf("\n Given Start   : %d", n);
	printf("\n Difference    : %d", k);
	printf("\n Given Length  : %d\n", m);
	// Collect result
	int result[m];
	// First value
	result[0] = n;
	// Test
	findCombination(n + (m *2), n - (k *2), k, result, 1, m);
}
int main()
{
	// Test A
	// Start  n     : 4
	// Difference k : 3
	// Length m     : 3
	combination(4, 3, 3);
	// Test B
	// Start  n     : 4
	// Difference k : 2
	// Length m     : 4
	combination(4, 2, 4);
	return 0;
}

Output

 Given Start   : 4
 Difference    : 3
 Given Length  : 3
  4  2  0
  4  2  1
  4  2  2
  4  2  3
  4  2  4
  4  3  1
  4  3  2
  4  3  3
  4  3  4
  4  3  5
  4  4  2
  4  4  3
  4  4  4
  4  4  5
  4  4  6
  4  5  3
  4  5  4
  4  5  5
  4  5  6
  4  5  7
  4  6  4
  4  6  5
  4  6  6
  4  6  7
  4  6  8

 Given Start   : 4
 Difference    : 2
 Given Length  : 4
  4  3  2  1
  4  3  2  2
  4  3  2  3
  4  3  3  2
  4  3  3  3
  4  3  3  4
  4  3  4  3
  4  3  4  4
  4  3  4  5
  4  4  3  2
  4  4  3  3
  4  4  3  4
  4  4  4  3
  4  4  4  4
  4  4  4  5
  4  4  5  4
  4  4  5  5
  4  4  5  6
  4  5  4  3
  4  5  4  4
  4  5  4  5
  4  5  5  4
  4  5  5  5
  4  5  5  6
  4  5  6  5
  4  5  6  6
  4  5  6  7
// Java Program 
// Print all the sequences of length m that begin with n 
// and the consecutive difference is less than k
public class Combinations
{
	
	// Display result
	public void printSequence(int[] result, int n)
	{
		for (int i = 0; i < n; ++i)
		{
			System.out.print(" " + result[i]);
		}
		System.out.print("\n");
	}
	public int absValue(int x)
	{
		if (x < 0)
		{
			return -x;
		}
		return x;
	}
	public void findCombination(int n, int current, 
			int k, int[] result, int index, int m)
	{
		if (index == m)
		{
			// Display calculated result
			printSequence(result, index);
			return;
		}
		if (index >= m)
		{
			// Base case when stop process
			return;
		}
		for (int i = current; i <= n; ++i)
		{
			if (absValue(result[index - 1] - i) < k)
			{
				// When pair difference less than k
				// Collects resultant value
				result[index] = i;
				// Find other combination using recursion
				findCombination(n, current, k, result, index + 1, m);
			}
		}
	}
	public void combination(int n, int k, int m)
	{
		if (m <= 0 || k <= 0)
		{
			return;
		}
		// Display given values
		System.out.print("\n Given Start : " + n );
		System.out.print("\n Difference : " + k );
		System.out.println("\n Given Length : " + m );
		// Collect result
		int[] result = new int[m];
		// First value
		result[0] = n;
		// Test
		findCombination(n + (m * 2), n - (k * 2), k, result, 1, m);
	}
	public static void main(String args[])
	{
		Combinations task = new Combinations();

		// Test A
		// Start  n     : 4
		// Difference k : 3
		// Length m     : 3
		task.combination(4, 3, 3);
		// Test B
		// Start  n     : 4
		// Difference k : 2
		// Length m     : 4
		task.combination(4, 2, 4);
	}
}

Output

 Given Start : 4
 Difference : 3
 Given Length : 3
 4 2 0
 4 2 1
 4 2 2
 4 2 3
 4 2 4
 4 3 1
 4 3 2
 4 3 3
 4 3 4
 4 3 5
 4 4 2
 4 4 3
 4 4 4
 4 4 5
 4 4 6
 4 5 3
 4 5 4
 4 5 5
 4 5 6
 4 5 7
 4 6 4
 4 6 5
 4 6 6
 4 6 7
 4 6 8

 Given Start : 4
 Difference : 2
 Given Length : 4
 4 3 2 1
 4 3 2 2
 4 3 2 3
 4 3 3 2
 4 3 3 3
 4 3 3 4
 4 3 4 3
 4 3 4 4
 4 3 4 5
 4 4 3 2
 4 4 3 3
 4 4 3 4
 4 4 4 3
 4 4 4 4
 4 4 4 5
 4 4 5 4
 4 4 5 5
 4 4 5 6
 4 5 4 3
 4 5 4 4
 4 5 4 5
 4 5 5 4
 4 5 5 5
 4 5 5 6
 4 5 6 5
 4 5 6 6
 4 5 6 7
// Include header file
#include <iostream>
using namespace std;
// C++ Program 
// Print all the sequences of length m that begin with n 
// and the consecutive difference is less than k
class Combinations
{
	public:
		// Display result
		void printSequence(int result[], int n)
		{
			for (int i = 0; i < n; ++i)
			{
				cout << " " << result[i];
			}
			cout << "\n";
		}
	int absValue(int x)
	{
		if (x < 0)
		{
			return -x;
		}
		return x;
	}
	void findCombination(int n, 
                         int current, 
                         int k, 
                         int result[], 
      int index, int m)
	{
		if (index == m)
		{
			// Display calculated result
			this->printSequence(result, index);
			return;
		}
		if (index >= m)
		{
			// Base case when stop process
			return;
		}
		for (int i = current; i <= n; ++i)
		{
			if (this->absValue(result[index - 1] - i) < k)
			{
				// When pair difference less than k
				// Collects resultant value
				result[index] = i;
				// Find other combination using recursion
				this->findCombination(n, current, 
                                      k, result, 
                                      index + 1, m);
			}
		}
	}
	void combination(int n, int k, int m)
	{
		if (m <= 0 || k <= 0)
		{
			return;
		}
		// Display given values
		cout << "\n Given Start : " << n;
		cout << "\n Difference : " << k;
		cout << "\n Given Length : " << m << endl;
		// Collect result
		int result[m];
		// First value
		result[0] = n;
		// Test
		this->findCombination(n + (m * 2), 
                              n - (k * 2), 
                              k, result, 
                              1, m);
	}
};
int main()
{
	Combinations *task = new Combinations();
	// Test A
	// Start  n     : 4
	// Difference k : 3
	// Length m     : 3
	task->combination(4, 3, 3);
	// Test B
	// Start  n     : 4
	// Difference k : 2
	// Length m     : 4
	task->combination(4, 2, 4);
	return 0;
}

Output

 Given Start : 4
 Difference : 3
 Given Length : 3
 4 2 0
 4 2 1
 4 2 2
 4 2 3
 4 2 4
 4 3 1
 4 3 2
 4 3 3
 4 3 4
 4 3 5
 4 4 2
 4 4 3
 4 4 4
 4 4 5
 4 4 6
 4 5 3
 4 5 4
 4 5 5
 4 5 6
 4 5 7
 4 6 4
 4 6 5
 4 6 6
 4 6 7
 4 6 8

 Given Start : 4
 Difference : 2
 Given Length : 4
 4 3 2 1
 4 3 2 2
 4 3 2 3
 4 3 3 2
 4 3 3 3
 4 3 3 4
 4 3 4 3
 4 3 4 4
 4 3 4 5
 4 4 3 2
 4 4 3 3
 4 4 3 4
 4 4 4 3
 4 4 4 4
 4 4 4 5
 4 4 5 4
 4 4 5 5
 4 4 5 6
 4 5 4 3
 4 5 4 4
 4 5 4 5
 4 5 5 4
 4 5 5 5
 4 5 5 6
 4 5 6 5
 4 5 6 6
 4 5 6 7
// Include namespace system
using System;
// Csharp Program 
// Print all the sequences of length m that begin with n 
// and the consecutive difference is less than k
public class Combinations
{
	// Display result
	public void printSequence(int[] result, int n)
	{
		for (int i = 0; i < n; ++i)
		{
			Console.Write(" " + result[i]);
		}
		Console.Write("\n");
	}
	public int absValue(int x)
	{
		if (x < 0)
		{
			return -x;
		}
		return x;
	}
	public void findCombination(int n, 
                                int current, int k, 
                                int[] result, int index, int m)
	{
		if (index == m)
		{
			// Display calculated result
			this.printSequence(result, index);
			return;
		}
		if (index >= m)
		{
			// Base case when stop process
			return;
		}
		for (int i = current; i <= n; ++i)
		{
			if (this.absValue(result[index - 1] - i) < k)
			{
				// When pair difference less than k
				// Collects resultant value
				result[index] = i;
				// Find other combination using recursion
				this.findCombination(n, current, 
                                     k, result, index + 1, m);
			}
		}
	}
	public void combination(int n, int k, int m)
	{
		if (m <= 0 || k <= 0)
		{
			return;
		}
		// Display given values
		Console.Write("\n Given Start : " + n);
		Console.Write("\n Difference : " + k);
		Console.WriteLine("\n Given Length : " + m);
		// Collect result
		int[] result = new int[m];
		// First value
		result[0] = n;
		// Test
		this.findCombination(n + (m * 2), 
                             n - (k * 2), k, result, 1, m);
	}
	public static void Main(String[] args)
	{
		Combinations task = new Combinations();
		// Test A
		// Start  n     : 4
		// Difference k : 3
		// Length m     : 3
		task.combination(4, 3, 3);
		// Test B
		// Start  n     : 4
		// Difference k : 2
		// Length m     : 4
		task.combination(4, 2, 4);
	}
}

Output

 Given Start : 4
 Difference : 3
 Given Length : 3
 4 2 0
 4 2 1
 4 2 2
 4 2 3
 4 2 4
 4 3 1
 4 3 2
 4 3 3
 4 3 4
 4 3 5
 4 4 2
 4 4 3
 4 4 4
 4 4 5
 4 4 6
 4 5 3
 4 5 4
 4 5 5
 4 5 6
 4 5 7
 4 6 4
 4 6 5
 4 6 6
 4 6 7
 4 6 8

 Given Start : 4
 Difference : 2
 Given Length : 4
 4 3 2 1
 4 3 2 2
 4 3 2 3
 4 3 3 2
 4 3 3 3
 4 3 3 4
 4 3 4 3
 4 3 4 4
 4 3 4 5
 4 4 3 2
 4 4 3 3
 4 4 3 4
 4 4 4 3
 4 4 4 4
 4 4 4 5
 4 4 5 4
 4 4 5 5
 4 4 5 6
 4 5 4 3
 4 5 4 4
 4 5 4 5
 4 5 5 4
 4 5 5 5
 4 5 5 6
 4 5 6 5
 4 5 6 6
 4 5 6 7
package main
import "fmt"
// Go Program 
// Print all the sequences of length m that begin with n 
// and the consecutive difference is less than k
type Combinations struct {}
func getCombinations() * Combinations {
	var me *Combinations = &Combinations {}
	return me
}
// Display result
func(this Combinations) printSequence(result[] int, n int) {
	for i := 0 ; i < n ; i++ {
		fmt.Print(" ", result[i])
	}
	fmt.Print("\n")
}
func(this Combinations) absValue(x int) int {
	if x < 0 {
		return -x
	}
	return x
}
func(this Combinations) findCombination(n int, 
	current int, k int, result[] int, index int, m int) {
	if index == m {
		// Display calculated result
		this.printSequence(result, index)
		return
	}
	if index >= m {
		// Base case when stop process
		return
	}
	for i := current ; i <= n ; i++ {
		if this.absValue(result[index - 1] - i) < k {
			// When pair difference less than k
			// Collects resultant value
			result[index] = i
			// Find other combination using recursion
			this.findCombination(n, current, 
				k, result, index + 1, m)
		}
	}
}
func(this Combinations) combination(n, k, m int) {
	if m <= 0 || k <= 0 {
		return
	}
	// Display given values
	fmt.Print("\n Given Start : ", n)
	fmt.Print("\n Difference : ", k)
	fmt.Println("\n Given Length : ", m)
	// Collect result
	var result = make([] int, m)
	// First value
	result[0] = n
	// Test
	this.findCombination(n + (m * 2), 
		n - (k * 2), k, result, 1, m)
}
func main() {
	var task * Combinations = getCombinations()
	// Test A
	// Start  n     : 4
	// Difference k : 3
	// Length m     : 3
	task.combination(4, 3, 3)
	// Test B
	// Start  n     : 4
	// Difference k : 2
	// Length m     : 4
	task.combination(4, 2, 4)
}

Output

 Given Start : 4
 Difference : 3
 Given Length : 3
 4 2 0
 4 2 1
 4 2 2
 4 2 3
 4 2 4
 4 3 1
 4 3 2
 4 3 3
 4 3 4
 4 3 5
 4 4 2
 4 4 3
 4 4 4
 4 4 5
 4 4 6
 4 5 3
 4 5 4
 4 5 5
 4 5 6
 4 5 7
 4 6 4
 4 6 5
 4 6 6
 4 6 7
 4 6 8

 Given Start : 4
 Difference : 2
 Given Length : 4
 4 3 2 1
 4 3 2 2
 4 3 2 3
 4 3 3 2
 4 3 3 3
 4 3 3 4
 4 3 4 3
 4 3 4 4
 4 3 4 5
 4 4 3 2
 4 4 3 3
 4 4 3 4
 4 4 4 3
 4 4 4 4
 4 4 4 5
 4 4 5 4
 4 4 5 5
 4 4 5 6
 4 5 4 3
 4 5 4 4
 4 5 4 5
 4 5 5 4
 4 5 5 5
 4 5 5 6
 4 5 6 5
 4 5 6 6
 4 5 6 7
<?php
// Php Program 
// Print all the sequences of length m that begin with n 
// and the consecutive difference is less than k
class Combinations
{
	// Display result
	public	function printSequence($result, $n)
	{
		for ($i = 0; $i < $n; ++$i)
		{
			echo(" ".$result[$i]);
		}
		echo("\n");
	}
	public	function absValue($x)
	{
		if ($x < 0)
		{
			return -$x;
		}
		return $x;
	}
	public	function findCombination( $n, 
                                      $current, 
                                      $k, 
                                      $result, 
                                      $index, 
                                      $m)
	{
		if ($index == $m)
		{
			// Display calculated result
			$this->printSequence($result, $index);
			return;
		}
		if ($index >= $m)
		{
			// Base case when stop process
			return;
		}
		for ($i = $current; $i <= $n; ++$i)
		{
			if ($this->absValue($result[$index - 1] - $i) < $k)
			{
				// When pair difference less than k
				// Collects resultant value
				$result[$index] = $i;
				// Find other combination using recursion
				$this->findCombination($n, 
                                       $current, $k, 
                                       $result, $index + 1, $m);
			}
		}
	}
	public	function combination($n, $k, $m)
	{
		if ($m <= 0 || $k <= 0)
		{
			return;
		}
		// Display given values
		echo("\n Given Start : ".$n);
		echo("\n Difference : ".$k);
		echo("\n Given Length : ".$m.
			"\n");
		// Collect result
		$result = array_fill(0, $m, 0);
		// First value
		$result[0] = $n;
		// Test
		$this->findCombination($n + ($m * 2), 
                               $n - ($k * 2), 
                               $k, $result, 1, $m);
	}
}

function main()
{
	$task = new Combinations();
	// Test A
	// Start  n     : 4
	// Difference k : 3
	// Length m     : 3
	$task->combination(4, 3, 3);
	// Test B
	// Start  n     : 4
	// Difference k : 2
	// Length m     : 4
	$task->combination(4, 2, 4);
}
main();

Output

 Given Start : 4
 Difference : 3
 Given Length : 3
 4 2 0
 4 2 1
 4 2 2
 4 2 3
 4 2 4
 4 3 1
 4 3 2
 4 3 3
 4 3 4
 4 3 5
 4 4 2
 4 4 3
 4 4 4
 4 4 5
 4 4 6
 4 5 3
 4 5 4
 4 5 5
 4 5 6
 4 5 7
 4 6 4
 4 6 5
 4 6 6
 4 6 7
 4 6 8

 Given Start : 4
 Difference : 2
 Given Length : 4
 4 3 2 1
 4 3 2 2
 4 3 2 3
 4 3 3 2
 4 3 3 3
 4 3 3 4
 4 3 4 3
 4 3 4 4
 4 3 4 5
 4 4 3 2
 4 4 3 3
 4 4 3 4
 4 4 4 3
 4 4 4 4
 4 4 4 5
 4 4 5 4
 4 4 5 5
 4 4 5 6
 4 5 4 3
 4 5 4 4
 4 5 4 5
 4 5 5 4
 4 5 5 5
 4 5 5 6
 4 5 6 5
 4 5 6 6
 4 5 6 7
// Node JS Program 
// Print all the sequences of length m that begin with n 
// and the consecutive difference is less than k
class Combinations
{
	// Display result
	printSequence(result, n)
	{
		for (var i = 0; i < n; ++i)
		{
			process.stdout.write(" " + result[i]);
		}
		process.stdout.write("\n");
	}
	absValue(x)
	{
		if (x < 0)
		{
			return -x;
		}
		return x;
	}
	findCombination(n, current, k, result, index, m)
	{
		if (index == m)
		{
			// Display calculated result
			this.printSequence(result, index);
			return;
		}
		if (index >= m)
		{
			// Base case when stop process
			return;
		}
		for (var i = current; i <= n; ++i)
		{
			if (this.absValue(result[index - 1] - i) < k)
			{
				// When pair difference less than k
				// Collects resultant value
				result[index] = i;
				// Find other combination using recursion
				this.findCombination(n, current, 
                                     k, result, index + 1, m);
			}
		}
	}
	combination(n, k, m)
	{
		if (m <= 0 || k <= 0)
		{
			return;
		}
		// Display given values
		process.stdout.write("\n Given Start : " + n);
		process.stdout.write("\n Difference : " + k);
		console.log("\n Given Length : " + m);
		// Collect result
		var result = Array(m).fill(0);
		// First value
		result[0] = n;
		// Test
		this.findCombination(n + (m * 2), 
                             n - (k * 2), k, 
                             result, 1, m);
	}
}

function main()
{
	var task = new Combinations();
	// Test A
	// Start  n     : 4
	// Difference k : 3
	// Length m     : 3
	task.combination(4, 3, 3);
	// Test B
	// Start  n     : 4
	// Difference k : 2
	// Length m     : 4
	task.combination(4, 2, 4);
}
main();

Output

 Given Start : 4
 Difference : 3
 Given Length : 3
 4 2 0
 4 2 1
 4 2 2
 4 2 3
 4 2 4
 4 3 1
 4 3 2
 4 3 3
 4 3 4
 4 3 5
 4 4 2
 4 4 3
 4 4 4
 4 4 5
 4 4 6
 4 5 3
 4 5 4
 4 5 5
 4 5 6
 4 5 7
 4 6 4
 4 6 5
 4 6 6
 4 6 7
 4 6 8

 Given Start : 4
 Difference : 2
 Given Length : 4
 4 3 2 1
 4 3 2 2
 4 3 2 3
 4 3 3 2
 4 3 3 3
 4 3 3 4
 4 3 4 3
 4 3 4 4
 4 3 4 5
 4 4 3 2
 4 4 3 3
 4 4 3 4
 4 4 4 3
 4 4 4 4
 4 4 4 5
 4 4 5 4
 4 4 5 5
 4 4 5 6
 4 5 4 3
 4 5 4 4
 4 5 4 5
 4 5 5 4
 4 5 5 5
 4 5 5 6
 4 5 6 5
 4 5 6 6
 4 5 6 7
#  Python 3 Program 
#  Print all the sequences of length m that begin with n 
#  and the consecutive difference is less than k
class Combinations :
	#  Display result
	def printSequence(self, result, n) :
		i = 0
		while (i < n) :
			print(" ", result[i], end = "")
			i += 1
		
		print(end = "\n")
	
	def absValue(self, x) :
		if (x < 0) :
			return -x
		
		return x
	
	def findCombination(self, n, current, k, result, index, m) :
		if (index == m) :
			#  Display calculated result
			self.printSequence(result, index)
			return
		
		if (index >= m) :
			#  Base case when stop process
			return
		
		i = current
		while (i <= n) :
			if (self.absValue(result[index - 1] - i) < k) :
				#  When pair difference less than k
				#  Collects resultant value
				result[index] = i
				#  Find other combination using recursion
				self.findCombination(n, current, 
                                     k, result, 
                                     index + 1, m)
			
			i += 1
		
	
	def combination(self, n, k, m) :
		if (m <= 0 or k <= 0) :
			return
		
		#  Display given values
		print("\n Given Start : ", n, end = "")
		print("\n Difference : ", k, end = "")
		print("\n Given Length : ", m)
		#  Collect result
		result = [0] * (m)
		#  First value
		result[0] = n
		#  Test
		self.findCombination(n + (m * 2), 
                             n - (k * 2), k, result, 1, m)
	

def main() :
	task = Combinations()
	#  Test A
	#  Start  n     : 4
	#  Difference k : 3
	#  Length m     : 3
	task.combination(4, 3, 3)
	#  Test B
	#  Start  n     : 4
	#  Difference k : 2
	#  Length m     : 4
	task.combination(4, 2, 4)

if __name__ == "__main__": main()

Output

 Given Start :  4
 Difference :  3
 Given Length :  3
  4  2  0
  4  2  1
  4  2  2
  4  2  3
  4  2  4
  4  3  1
  4  3  2
  4  3  3
  4  3  4
  4  3  5
  4  4  2
  4  4  3
  4  4  4
  4  4  5
  4  4  6
  4  5  3
  4  5  4
  4  5  5
  4  5  6
  4  5  7
  4  6  4
  4  6  5
  4  6  6
  4  6  7
  4  6  8

 Given Start :  4
 Difference :  2
 Given Length :  4
  4  3  2  1
  4  3  2  2
  4  3  2  3
  4  3  3  2
  4  3  3  3
  4  3  3  4
  4  3  4  3
  4  3  4  4
  4  3  4  5
  4  4  3  2
  4  4  3  3
  4  4  3  4
  4  4  4  3
  4  4  4  4
  4  4  4  5
  4  4  5  4
  4  4  5  5
  4  4  5  6
  4  5  4  3
  4  5  4  4
  4  5  4  5
  4  5  5  4
  4  5  5  5
  4  5  5  6
  4  5  6  5
  4  5  6  6
  4  5  6  7
#  Ruby Program 
#  Print all the sequences of length m that begin with n 
#  and the consecutive difference is less than k
class Combinations 
	#  Display result
	def printSequence(result, n) 
		i = 0
		while (i < n) 
			print(" ", result[i])
			i += 1
		end

		print("\n")
	end

	def absValue(x) 
		if (x < 0) 
			return -x
		end

		return x
	end

	def findCombination(n, current, k, result, index, m) 
		if (index == m) 
			#  Display calculated result
			self.printSequence(result, index)
			return
		end

		if (index >= m) 
			#  Base case when stop process
			return
		end

		i = current
		while (i <= n) 
			if (self.absValue(result[index - 1] - i) < k) 
				#  When pair difference less than k
				#  Collects resultant value
				result[index] = i
				#  Find other combination using recursion
				self.findCombination(n, current, 
                                     k, result, index + 1, m)
			end

			i += 1
		end

	end

	def combination(n, k, m) 
		if (m <= 0 || k <= 0) 
			return
		end

		#  Display given values
		print("\n Given Start : ", n)
		print("\n Difference : ", k)
		print("\n Given Length : ", m, "\n")
		#  Collect result
		result = Array.new(m) {0}
		#  First value
		result[0] = n
		#  Test
		self.findCombination(n + (m * 2), 
                             n - (k * 2), k, result, 1, m)
	end

end

def main() 
	task = Combinations.new()
	#  Test A
	#  Start  n     : 4
	#  Difference k : 3
	#  Length m     : 3
	task.combination(4, 3, 3)
	#  Test B
	#  Start  n     : 4
	#  Difference k : 2
	#  Length m     : 4
	task.combination(4, 2, 4)
end

main()

Output

 Given Start : 4
 Difference : 3
 Given Length : 3
 4 2 0
 4 2 1
 4 2 2
 4 2 3
 4 2 4
 4 3 1
 4 3 2
 4 3 3
 4 3 4
 4 3 5
 4 4 2
 4 4 3
 4 4 4
 4 4 5
 4 4 6
 4 5 3
 4 5 4
 4 5 5
 4 5 6
 4 5 7
 4 6 4
 4 6 5
 4 6 6
 4 6 7
 4 6 8

 Given Start : 4
 Difference : 2
 Given Length : 4
 4 3 2 1
 4 3 2 2
 4 3 2 3
 4 3 3 2
 4 3 3 3
 4 3 3 4
 4 3 4 3
 4 3 4 4
 4 3 4 5
 4 4 3 2
 4 4 3 3
 4 4 3 4
 4 4 4 3
 4 4 4 4
 4 4 4 5
 4 4 5 4
 4 4 5 5
 4 4 5 6
 4 5 4 3
 4 5 4 4
 4 5 4 5
 4 5 5 4
 4 5 5 5
 4 5 5 6
 4 5 6 5
 4 5 6 6
 4 5 6 7
// Scala Program 
// Print all the sequences of length m that begin with n 
// and the consecutive difference is less than k
class Combinations()
{
	// Display result
	def printSequence(result: Array[Int], n: Int): Unit = {
		var i: Int = 0;
		while (i < n)
		{
			print(" " + result(i));
			i += 1;
		}
		print("\n");
	}
	def absValue(x: Int): Int = {
		if (x < 0)
		{
			return -x;
		}
		return x;
	}
	def findCombination(n: Int, current: Int, 
                        k: Int, result: Array[Int], 
      index: Int, m: Int): Unit = {
		if (index == m)
		{
			// Display calculated result
			printSequence(result, index);
			return;
		}
		if (index >= m)
		{
			// Base case when stop process
			return;
		}
		var i: Int = current;
		while (i <= n)
		{
			if (absValue(result(index - 1) - i) < k)
			{
				// When pair difference less than k
				// Collects resultant value
				result(index) = i;
				// Find other combination using recursion
				findCombination(n, current, 
                                k, result, index + 1, m);
			}
			i += 1;
		}
	}
	def combination(n: Int, k: Int, m: Int): Unit = {
		if (m <= 0 || k <= 0)
		{
			return;
		}
		// Display given values
		print("\n Given Start : " + n);
		print("\n Difference : " + k);
		println("\n Given Length : " + m);
		// Collect result
		var result: Array[Int] = Array.fill[Int](m)(0);
		// First value
		result(0) = n;
		// Test
		findCombination(n + (m * 2), 
                        n - (k * 2), k, result, 1, m);
	}
}
object Main
{
	def main(args: Array[String]): Unit = {
		var task: Combinations = new Combinations();
		// Test A
		// Start  n     : 4
		// Difference k : 3
		// Length m     : 3
		task.combination(4, 3, 3);
		// Test B
		// Start  n     : 4
		// Difference k : 2
		// Length m     : 4
		task.combination(4, 2, 4);
	}
}

Output

 Given Start : 4
 Difference : 3
 Given Length : 3
 4 2 0
 4 2 1
 4 2 2
 4 2 3
 4 2 4
 4 3 1
 4 3 2
 4 3 3
 4 3 4
 4 3 5
 4 4 2
 4 4 3
 4 4 4
 4 4 5
 4 4 6
 4 5 3
 4 5 4
 4 5 5
 4 5 6
 4 5 7
 4 6 4
 4 6 5
 4 6 6
 4 6 7
 4 6 8

 Given Start : 4
 Difference : 2
 Given Length : 4
 4 3 2 1
 4 3 2 2
 4 3 2 3
 4 3 3 2
 4 3 3 3
 4 3 3 4
 4 3 4 3
 4 3 4 4
 4 3 4 5
 4 4 3 2
 4 4 3 3
 4 4 3 4
 4 4 4 3
 4 4 4 4
 4 4 4 5
 4 4 5 4
 4 4 5 5
 4 4 5 6
 4 5 4 3
 4 5 4 4
 4 5 4 5
 4 5 5 4
 4 5 5 5
 4 5 5 6
 4 5 6 5
 4 5 6 6
 4 5 6 7
// Swift 4 Program 
// Print all the sequences of length m that begin with n 
// and the consecutive difference is less than k
class Combinations
{
	// Display result
	func printSequence(_ result: [Int], _ n: Int)
	{
		var i: Int = 0;
		while (i < n)
		{
			print(" ", result[i], terminator: "");
			i += 1;
		}
		print(terminator: "\n");
	}
	func absValue(_ x: Int) -> Int
	{
		if (x < 0)
		{
			return -x;
		}
		return x;
	}
	func findCombination(_ n: Int, 
                         _ current: Int, 
                         _ k: Int, 
                         _ result: inout[Int], 
      					 _ index: Int, 
                         _ m: Int)
	{
		if (index == m)
		{
			// Display calculated result
			self.printSequence(result, index);
			return;
		}
		if (index >= m)
		{
			// Base case when stop process
			return;
		}
		var i: Int = current;
		while (i <= n)
		{
			if (self.absValue(result[index - 1] - i) < k)
			{
				// When pair difference less than k
				// Collects resultant value
				result[index] = i;
				// Find other combination using recursion
				self.findCombination(n, current, k, &result, index + 1, m);
			}
			i += 1;
		}
	}
	func combination(_ n: Int, _ k: Int, _ m: Int)
	{
		if (m <= 0 || k <= 0)
		{
			return;
		}
		// Display given values
		print("\n Given Start : ", n, terminator: "");
		print("\n Difference : ", k, terminator: "");
		print("\n Given Length : ", m);
		// Collect result
		var result: [Int] = Array(repeating: 0, count: m);
		// First value
		result[0] = n;
		// Test
		self.findCombination(n + (m * 2), 
                             n - (k * 2), k, &result, 1, m);
	}
}
func main()
{
	let task: Combinations = Combinations();
	// Test A
	// Start  n     : 4
	// Difference k : 3
	// Length m     : 3
	task.combination(4, 3, 3);
	// Test B
	// Start  n     : 4
	// Difference k : 2
	// Length m     : 4
	task.combination(4, 2, 4);
}
main();

Output

 Given Start :  4
 Difference :  3
 Given Length :  3
  4  2  0
  4  2  1
  4  2  2
  4  2  3
  4  2  4
  4  3  1
  4  3  2
  4  3  3
  4  3  4
  4  3  5
  4  4  2
  4  4  3
  4  4  4
  4  4  5
  4  4  6
  4  5  3
  4  5  4
  4  5  5
  4  5  6
  4  5  7
  4  6  4
  4  6  5
  4  6  6
  4  6  7
  4  6  8

 Given Start :  4
 Difference :  2
 Given Length :  4
  4  3  2  1
  4  3  2  2
  4  3  2  3
  4  3  3  2
  4  3  3  3
  4  3  3  4
  4  3  4  3
  4  3  4  4
  4  3  4  5
  4  4  3  2
  4  4  3  3
  4  4  3  4
  4  4  4  3
  4  4  4  4
  4  4  4  5
  4  4  5  4
  4  4  5  5
  4  4  5  6
  4  5  4  3
  4  5  4  4
  4  5  4  5
  4  5  5  4
  4  5  5  5
  4  5  5  6
  4  5  6  5
  4  5  6  6
  4  5  6  7
// Kotlin Program 
// Print all the sequences of length m that begin with n 
// and the consecutive difference is less than k
class Combinations
{
	// Display result
	fun printSequence(result: Array < Int > , n: Int): Unit
	{
		var i: Int = 0;
		while (i < n)
		{
			print(" " + result[i]);
			i += 1;
		}
		print("\n");
	}
	fun absValue(x: Int): Int
	{
		if (x < 0)
		{
			return -x;
		}
		return x;
	}
	fun findCombination(n: Int, 
                        current: Int, 
                        k: Int, 
                        result: Array < Int > , 
                        index: Int, 
                        m: Int): Unit
	{
		if (index == m)
		{
			// Display calculated result
			this.printSequence(result, index);
			return;
		}
		if (index >= m)
		{
			// Base case when stop process
			return;
		}
		var i: Int = current;
		while (i <= n)
		{
			if (this.absValue(result[index - 1] - i) < k)
			{
				// When pair difference less than k
				// Collects resultant value
				result[index] = i;
				// Find other combination using recursion
				this.findCombination(n, 
                                     current, 
                                     k, result, 
                                     index + 1, m);
			}
			i += 1;
		}
	}
	fun combination(n: Int, k: Int, m: Int): Unit
	{
		if (m <= 0 || k <= 0)
		{
			return;
		}
		// Display given values
		print("\n Given Start : " + n);
		print("\n Difference : " + k);
		println("\n Given Length : " + m);
		// Collect result
		val result: Array < Int > = Array(m)
		{
			0
		};
		// First value
		result[0] = n;
		// Test
		this.findCombination(n + (m * 2), 
                             n - (k * 2), k, result, 1, m);
	}
}
fun main(args: Array < String > ): Unit
{
	val task: Combinations = Combinations();
	// Test A
	// Start  n     : 4
	// Difference k : 3
	// Length m     : 3
	task.combination(4, 3, 3);
	// Test B
	// Start  n     : 4
	// Difference k : 2
	// Length m     : 4
	task.combination(4, 2, 4);
}

Output

 Given Start : 4
 Difference : 3
 Given Length : 3
 4 2 0
 4 2 1
 4 2 2
 4 2 3
 4 2 4
 4 3 1
 4 3 2
 4 3 3
 4 3 4
 4 3 5
 4 4 2
 4 4 3
 4 4 4
 4 4 5
 4 4 6
 4 5 3
 4 5 4
 4 5 5
 4 5 6
 4 5 7
 4 6 4
 4 6 5
 4 6 6
 4 6 7
 4 6 8

 Given Start : 4
 Difference : 2
 Given Length : 4
 4 3 2 1
 4 3 2 2
 4 3 2 3
 4 3 3 2
 4 3 3 3
 4 3 3 4
 4 3 4 3
 4 3 4 4
 4 3 4 5
 4 4 3 2
 4 4 3 3
 4 4 3 4
 4 4 4 3
 4 4 4 4
 4 4 4 5
 4 4 5 4
 4 4 5 5
 4 4 5 6
 4 5 4 3
 4 5 4 4
 4 5 4 5
 4 5 5 4
 4 5 5 5
 4 5 5 6
 4 5 6 5
 4 5 6 6
 4 5 6 7

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