Skip to main content

Find possible group which contains one and two consecutive elements

Here given code implementation process.

// Include header file
#include <iostream>

using namespace std;
/*
    C++ program for
    Find possible group which contains one and two consecutive elements
*/
class Pairing
{
	public: void consecutivePairing(string output, int index, int n)
	{
		if (index <= n)
		{
			// Single element pairs
			this->consecutivePairing(output  +  " ("
				 +  to_string(index)  +  ")", index + 1, n);
			if (index + 1 <= n)
			{
				// Two element pairs
				this->consecutivePairing(output  +  " ("
					 +  to_string(index)  +  ","
					 +  to_string((index + 1))  +  ")", index + 2, n);
			}
		}
		else
		{
			// Display output
			cout << output << endl;
		}
	}
	// This is handle the request of printing consecutive elements group
	void pairGroup(int element)
	{
		this->consecutivePairing("", 1, element);
	}
};
int main()
{
	Pairing *task = new Pairing();
	int n = 6;
	task->pairGroup(n);
	return 0;
}

Output

 (1) (2) (3) (4) (5) (6)
 (1) (2) (3) (4) (5,6)
 (1) (2) (3) (4,5) (6)
 (1) (2) (3,4) (5) (6)
 (1) (2) (3,4) (5,6)
 (1) (2,3) (4) (5) (6)
 (1) (2,3) (4) (5,6)
 (1) (2,3) (4,5) (6)
 (1,2) (3) (4) (5) (6)
 (1,2) (3) (4) (5,6)
 (1,2) (3) (4,5) (6)
 (1,2) (3,4) (5) (6)
 (1,2) (3,4) (5,6)
/*
    Java program for
    Find possible group which contains one and two consecutive elements
*/
public class Pairing
{
	public void consecutivePairing(String output, int index, int n)
	{
		if (index <= n)
		{
			// Single element pairs
			consecutivePairing(output + 
                               " (" + index + ")", 
                               index + 1, n);
			if (index + 1 <= n)
			{
				// Two element pairs
				consecutivePairing(output + 
                                   " (" + index + "," + (index + 1) + ")", 
                                   index + 2, n);
			}
		}
		else
		{
			// Display output
			System.out.println(output);
		}
	}
	// This is handle the request of printing consecutive elements group
	public void pairGroup(int element)
	{
		consecutivePairing("", 1, element);
	}
	public static void main(String[] args)
	{
		Pairing task = new Pairing();
		int n = 6;
		task.pairGroup(n);
	}
}

Output

 (1) (2) (3) (4) (5) (6)
 (1) (2) (3) (4) (5,6)
 (1) (2) (3) (4,5) (6)
 (1) (2) (3,4) (5) (6)
 (1) (2) (3,4) (5,6)
 (1) (2,3) (4) (5) (6)
 (1) (2,3) (4) (5,6)
 (1) (2,3) (4,5) (6)
 (1,2) (3) (4) (5) (6)
 (1,2) (3) (4) (5,6)
 (1,2) (3) (4,5) (6)
 (1,2) (3,4) (5) (6)
 (1,2) (3,4) (5,6)
package main
import "strconv"
import "fmt"
/*
    Go program for
    Find possible group which contains one and two consecutive elements
*/

func consecutivePairing(output string, index int, n int) {
	if index <= n {
		// Single element pairs
		consecutivePairing(output + " (" + 
			strconv.Itoa(index) + ")", index + 1, n)
		if index + 1 <= n {
			// Two element pairs
			consecutivePairing(output + " (" + 
				strconv.Itoa(index) + "," + 
				strconv.Itoa((index + 1)) + ")", index + 2, n)
		}
	} else {
		// Display output
		fmt.Println(output)
	}
}
// This is handle the request of printing consecutive elements group
func pairGroup(element int) {
	consecutivePairing("", 1, element)
}
func main() {

	var n int = 6
	pairGroup(n)
}

Output

 (1) (2) (3) (4) (5) (6)
 (1) (2) (3) (4) (5,6)
 (1) (2) (3) (4,5) (6)
 (1) (2) (3,4) (5) (6)
 (1) (2) (3,4) (5,6)
 (1) (2,3) (4) (5) (6)
 (1) (2,3) (4) (5,6)
 (1) (2,3) (4,5) (6)
 (1,2) (3) (4) (5) (6)
 (1,2) (3) (4) (5,6)
 (1,2) (3) (4,5) (6)
 (1,2) (3,4) (5) (6)
 (1,2) (3,4) (5,6)
// Include namespace system
using System;
/*
    Csharp program for
    Find possible group which contains one and two consecutive elements
*/
public class Pairing
{
	public void consecutivePairing(String output, int index, int n)
	{
		if (index <= n)
		{
			// Single element pairs
			this.consecutivePairing(output + " (" + index + ")", index + 1, n);
			if (index + 1 <= n)
			{
				// Two element pairs
				this.consecutivePairing(output + " (" + index + "," + (index + 1) + ")", index + 2, n);
			}
		}
		else
		{
			// Display output
			Console.WriteLine(output);
		}
	}
	// This is handle the request of printing consecutive elements group
	public void pairGroup(int element)
	{
		this.consecutivePairing("", 1, element);
	}
	public static void Main(String[] args)
	{
		Pairing task = new Pairing();
		int n = 6;
		task.pairGroup(n);
	}
}

Output

 (1) (2) (3) (4) (5) (6)
 (1) (2) (3) (4) (5,6)
 (1) (2) (3) (4,5) (6)
 (1) (2) (3,4) (5) (6)
 (1) (2) (3,4) (5,6)
 (1) (2,3) (4) (5) (6)
 (1) (2,3) (4) (5,6)
 (1) (2,3) (4,5) (6)
 (1,2) (3) (4) (5) (6)
 (1,2) (3) (4) (5,6)
 (1,2) (3) (4,5) (6)
 (1,2) (3,4) (5) (6)
 (1,2) (3,4) (5,6)
<?php
/*
    Php program for
    Find possible group which contains one and two consecutive elements
*/
class Pairing
{
	public	function consecutivePairing($output, $index, $n)
	{
		if ($index <= $n)
		{
			// Single element pairs
			$this->consecutivePairing($output.
				" (".strval($index).
				")", $index + 1, $n);
			if ($index + 1 <= $n)
			{
				// Two element pairs
				$this->consecutivePairing($output.
					" (".strval($index).
					",".strval(($index + 1)).
					")", $index + 2, $n);
			}
		}
		else
		{
			// Display output
			echo($output.
				"\n");
		}
	}
	// This is handle the request of printing consecutive elements group
	public	function pairGroup($element)
	{
		$this->consecutivePairing("", 1, $element);
	}
}

function main()
{
	$task = new Pairing();
	$n = 6;
	$task->pairGroup($n);
}
main();

Output

 (1) (2) (3) (4) (5) (6)
 (1) (2) (3) (4) (5,6)
 (1) (2) (3) (4,5) (6)
 (1) (2) (3,4) (5) (6)
 (1) (2) (3,4) (5,6)
 (1) (2,3) (4) (5) (6)
 (1) (2,3) (4) (5,6)
 (1) (2,3) (4,5) (6)
 (1,2) (3) (4) (5) (6)
 (1,2) (3) (4) (5,6)
 (1,2) (3) (4,5) (6)
 (1,2) (3,4) (5) (6)
 (1,2) (3,4) (5,6)
/*
    Node JS program for
    Find possible group which contains one and two consecutive elements
*/
class Pairing
{
	consecutivePairing(output, index, n)
	{
		if (index <= n)
		{
			// Single element pairs
			this.consecutivePairing(output + 
                                    " (" + index + ")", index + 1, n);
			if (index + 1 <= n)
			{
				// Two element pairs
				this.consecutivePairing(output + 
                                        " (" + index + "," + (index + 1) + ")",
                                        index + 2, n);
			}
		}
		else
		{
			// Display output
			console.log(output);
		}
	}
	// This is handle the request of printing consecutive elements group
	pairGroup(element)
	{
		this.consecutivePairing("", 1, element);
	}
}

function main()
{
	var task = new Pairing();
	var n = 6;
	task.pairGroup(n);
}
main();

Output

 (1) (2) (3) (4) (5) (6)
 (1) (2) (3) (4) (5,6)
 (1) (2) (3) (4,5) (6)
 (1) (2) (3,4) (5) (6)
 (1) (2) (3,4) (5,6)
 (1) (2,3) (4) (5) (6)
 (1) (2,3) (4) (5,6)
 (1) (2,3) (4,5) (6)
 (1,2) (3) (4) (5) (6)
 (1,2) (3) (4) (5,6)
 (1,2) (3) (4,5) (6)
 (1,2) (3,4) (5) (6)
 (1,2) (3,4) (5,6)
#    Python 3 program for
#    Find possible group which contains one and two consecutive elements
class Pairing :
	def consecutivePairing(self, output, index, n) :
		if (index <= n) :
			#  Single element pairs
			self.consecutivePairing(output + " ("
				+ str(index) + ")", index + 1, n)
			if (index + 1 <= n) :
				#  Two element pairs
				self.consecutivePairing(output + " ("
					+ str(index) + ","
					+ str((index + 1)) + ")", index + 2, n)
			
		else :
			#  Display output
			print(output)
		
	
	#  This is handle the request of printing consecutive elements group
	def pairGroup(self, element) :
		self.consecutivePairing("", 1, element)
	

def main() :
	task = Pairing()
	n = 6
	task.pairGroup(n)

if __name__ == "__main__": main()

Output

 (1) (2) (3) (4) (5) (6)
 (1) (2) (3) (4) (5,6)
 (1) (2) (3) (4,5) (6)
 (1) (2) (3,4) (5) (6)
 (1) (2) (3,4) (5,6)
 (1) (2,3) (4) (5) (6)
 (1) (2,3) (4) (5,6)
 (1) (2,3) (4,5) (6)
 (1,2) (3) (4) (5) (6)
 (1,2) (3) (4) (5,6)
 (1,2) (3) (4,5) (6)
 (1,2) (3,4) (5) (6)
 (1,2) (3,4) (5,6)
#    Ruby program for
#    Find possible group which contains one and two consecutive elements
class Pairing 
	def consecutivePairing(output, index, n) 
		if (index <= n) 
			#  Single element pairs
			self.consecutivePairing(output + " (" + 
                                    index.to_s + ")", index + 1, n)
			if (index + 1 <= n) 
				#  Two element pairs
				self.consecutivePairing(output + " (" + index.to_s + "," +
					(index + 1).to_s + ")", index + 2, n)
			end

		else
 
			#  Display output
			print(output, "\n")
		end

	end

	#  This is handle the request of printing consecutive elements group
	def pairGroup(element) 
		self.consecutivePairing("", 1, element)
	end

end

def main() 
	task = Pairing.new()
	n = 6
	task.pairGroup(n)
end

main()

Output

 (1) (2) (3) (4) (5) (6)
 (1) (2) (3) (4) (5,6)
 (1) (2) (3) (4,5) (6)
 (1) (2) (3,4) (5) (6)
 (1) (2) (3,4) (5,6)
 (1) (2,3) (4) (5) (6)
 (1) (2,3) (4) (5,6)
 (1) (2,3) (4,5) (6)
 (1,2) (3) (4) (5) (6)
 (1,2) (3) (4) (5,6)
 (1,2) (3) (4,5) (6)
 (1,2) (3,4) (5) (6)
 (1,2) (3,4) (5,6)
/*
    Scala program for
    Find possible group which contains one and two consecutive elements
*/
class Pairing()
{
	def consecutivePairing(output: String, index: Int, n: Int): Unit = {
		if (index <= n)
		{
			// Single element pairs
			consecutivePairing(output + " (" + 
                               index.toString() + ")", 
                               index + 1, n);
			if (index + 1 <= n)
			{
				// Two element pairs
				consecutivePairing(output + " (" + 
                                   index.toString() + "," + 
                                   (index + 1).toString() + ")", 
                                   index + 2, n);
			}
		}
		else
		{
			// Display output
			println(output);
		}
	}
	// This is handle the request of printing consecutive elements group
	def pairGroup(element: Int): Unit = {
		consecutivePairing("", 1, element);
	}
}
object Main
{
	def main(args: Array[String]): Unit = {
		var task: Pairing = new Pairing();
		var n: Int = 6;
		task.pairGroup(n);
	}
}

Output

 (1) (2) (3) (4) (5) (6)
 (1) (2) (3) (4) (5,6)
 (1) (2) (3) (4,5) (6)
 (1) (2) (3,4) (5) (6)
 (1) (2) (3,4) (5,6)
 (1) (2,3) (4) (5) (6)
 (1) (2,3) (4) (5,6)
 (1) (2,3) (4,5) (6)
 (1,2) (3) (4) (5) (6)
 (1,2) (3) (4) (5,6)
 (1,2) (3) (4,5) (6)
 (1,2) (3,4) (5) (6)
 (1,2) (3,4) (5,6)
/*
    Swift 4 program for
    Find possible group which contains one and two consecutive elements
*/
class Pairing
{
	func consecutivePairing(_ output: String, _ index: Int, _ n: Int)
	{
		if (index <= n)
		{
			// Single element pairs
			self.consecutivePairing(output + " ("
				+ String(index) + ")", index + 1, n);
			if (index + 1 <= n)
			{
				// Two element pairs
				self.consecutivePairing(output + " ("
					+ String(index) + ","
					+ String((index + 1)) + ")", index + 2, n);
			}
		}
		else
		{
			// Display output
			print(output);
		}
	}
	// This is handle the request of printing consecutive elements group
	func pairGroup(_ element: Int)
	{
		self.consecutivePairing("", 1, element);
	}
}
func main()
{
	let task: Pairing = Pairing();
	let n: Int = 6;
	task.pairGroup(n);
}
main();

Output

 (1) (2) (3) (4) (5) (6)
 (1) (2) (3) (4) (5,6)
 (1) (2) (3) (4,5) (6)
 (1) (2) (3,4) (5) (6)
 (1) (2) (3,4) (5,6)
 (1) (2,3) (4) (5) (6)
 (1) (2,3) (4) (5,6)
 (1) (2,3) (4,5) (6)
 (1,2) (3) (4) (5) (6)
 (1,2) (3) (4) (5,6)
 (1,2) (3) (4,5) (6)
 (1,2) (3,4) (5) (6)
 (1,2) (3,4) (5,6)
/*
    Kotlin program for
    Find possible group which contains one and two consecutive elements
*/
class Pairing
{
	fun consecutivePairing(output: String, index: Int, n: Int): Unit
	{
		if (index <= n)
		{
			// Single element pairs
			this.consecutivePairing(output + " (" + 
                                    index.toString() + ")", index + 1, n);
			if (index + 1 <= n)
			{
				// Two element pairs
				this.consecutivePairing(output + " (" + 
                                        index.toString() + "," + 
                                        (index + 1).toString() + ")", 
                                        index + 2, n);
			}
		}
		else
		{
			// Display output
			println(output);
		}
	}
	// This is handle the request of printing consecutive elements group
	fun pairGroup(element: Int): Unit
	{
		this.consecutivePairing("", 1, element);
	}
}
fun main(args: Array < String > ): Unit
{
	val task: Pairing = Pairing();
	val n: Int = 6;
	task.pairGroup(n);
}

Output

 (1) (2) (3) (4) (5) (6)
 (1) (2) (3) (4) (5,6)
 (1) (2) (3) (4,5) (6)
 (1) (2) (3,4) (5) (6)
 (1) (2) (3,4) (5,6)
 (1) (2,3) (4) (5) (6)
 (1) (2,3) (4) (5,6)
 (1) (2,3) (4,5) (6)
 (1,2) (3) (4) (5) (6)
 (1,2) (3) (4) (5,6)
 (1,2) (3) (4,5) (6)
 (1,2) (3,4) (5) (6)
 (1,2) (3,4) (5,6)




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