Skip to main content

Increasing subsequence of n natural number in k size

Here given code implementation process.

// C Program
// Increasing subsequence of n natural number in k size
#include <stdio.h>

// Display result elements
void printResult(int result[], int k)
{
	for (int i = 0; i < k; ++i)
	{
		printf(" %d", result[i]);
	}
	printf("\n");
}
void subSequences(int result[], int num, 
  				  int start, int count, int k)
{
	if (count == k)
	{
		// Display calculated result
		printResult(result, k);
		return;
	}
	else if (count > k)
	{
		return;
	}
	else
	{
		for (int i = start + 1; i <= num; ++i)
		{
			// Collect elements 
			result[count] = i;
			subSequences(result, num, i, count + 1, k);
		}
	}
}
// Handles the request to find resultant subsequences
void findSequence(int n, int k)
{
	if (n <= 0 || k <= 0)
	{
		return;
	}
	// Auxiliary array which is collect result
	int result[k];
	// Find resultant subsequences
	subSequences(result, n, 0, 0, k);
}
int main()
{
	int n = 5;
	int k = 3;
	// Test A
	// n >= k
	// length k = 3
	// number (1..5)
	/*
	    1 2 3
	    1 2 4
	    1 2 5
	    1 3 4
	    1 3 5
	    1 4 5
	    2 3 4
	    2 3 5
	    2 4 5
	    3 4 5
	--------------
	    All Unique result
	*/
	findSequence(n, k);
	return 0;
}

Output

 1 2 3
 1 2 4
 1 2 5
 1 3 4
 1 3 5
 1 4 5
 2 3 4
 2 3 5
 2 4 5
 3 4 5
// Java program for
// Increasing subsequence of n natural number in k size
public class Combination
{
	// Display result elements
	public void printResult(int[] result, int k)
	{
		for (int i = 0; i < k; ++i)
		{
			System.out.print(" " + result[i]);
		}
		System.out.print("\n");
	}
	public void subSequences(int[] result, 
                             int num, int start, 
                             int count, int k)
	{
		if (count == k)
		{
			// Display calculated result
			printResult(result, k);
			return;
		}
		else if (count > k)
		{
			return;
		}
		else
		{
			for (int i = start + 1; i <= num; ++i)
			{
				// Collect elements
				result[count] = i;
				subSequences(result, num, i, count + 1, k);
			}
		}
	}
	// Handles the request to find resultant subsequences
	public void findSequence(int n, int k)
	{
		if (n <= 0 || k <= 0)
		{
			return;
		}
		// Auxiliary array which is collect result
		int[] result = new int[k];
		// Find resultant subsequences
		subSequences(result, n, 0, 0, k);
	}
	public static void main(String[] args)
	{
		Combination task = new Combination();
		int n = 5;
		int k = 3;
		// Test A
		// n >= k
		// length k = 3
		// number (1..5)
		/*
		    1 2 3
		    1 2 4
		    1 2 5
		    1 3 4
		    1 3 5
		    1 4 5
		    2 3 4
		    2 3 5
		    2 4 5
		    3 4 5
		  --------------
		    All Unique result
		*/
		task.findSequence(n, k);
	}
}

Output

 1 2 3
 1 2 4
 1 2 5
 1 3 4
 1 3 5
 1 4 5
 2 3 4
 2 3 5
 2 4 5
 3 4 5
// Include header file
#include <iostream>

using namespace std;
// C++ program for
// Increasing subsequence of n natural number in k size
class Combination
{
	public:
		// Display result elements
		void printResult(int result[], int k)
		{
			for (int i = 0; i < k; ++i)
			{
				cout << " " << result[i];
			}
			cout << "\n";
		}
	void subSequences(int result[], 
      				  int num, 
                      int start, 
                      int count, 
                      int k)
	{
		if (count == k)
		{
			// Display calculated result
			this->printResult(result, k);
			return;
		}
		else if (count > k)
		{
			return;
		}
		else
		{
			for (int i = start + 1; i <= num; ++i)
			{
				// Collect elements
				result[count] = i;
				this->subSequences(result, num, i, count + 1, k);
			}
		}
	}
	// Handles the request to find resultant subsequences
	void findSequence(int n, int k)
	{
		if (n <= 0 || k <= 0)
		{
			return;
		}
		// Auxiliary array which is collect result
		int result[k];
		// Find resultant subsequences
		this->subSequences(result, n, 0, 0, k);
	}
};
int main()
{
	Combination *task = new Combination();
	int n = 5;
	int k = 3;
	// Test A
	// n >= k
	// length k = 3
	// number (1..5)
	/*
	    1 2 3
	    1 2 4
	    1 2 5
	    1 3 4
	    1 3 5
	    1 4 5
	    2 3 4
	    2 3 5
	    2 4 5
	    3 4 5
	  --------------
	    All Unique result
	*/
	task->findSequence(n, k);
	return 0;
}

Output

 1 2 3
 1 2 4
 1 2 5
 1 3 4
 1 3 5
 1 4 5
 2 3 4
 2 3 5
 2 4 5
 3 4 5
// Include namespace system
using System;
// Csharp program for
// Increasing subsequence of n natural number in k size
public class Combination
{
	// Display result elements
	public void printResult(int[] result, int k)
	{
		for (int i = 0; i < k; ++i)
		{
			Console.Write(" " + result[i]);
		}
		Console.Write("\n");
	}
	public void subSequences(int[] result, 
                             int num, 
                             int start, 
                             int count, int k)
	{
		if (count == k)
		{
			// Display calculated result
			this.printResult(result, k);
			return;
		}
		else if (count > k)
		{
			return;
		}
		else
		{
			for (int i = start + 1; i <= num; ++i)
			{
				// Collect elements
				result[count] = i;
				this.subSequences(result, num, i, count + 1, k);
			}
		}
	}
	// Handles the request to find resultant subsequences
	public void findSequence(int n, int k)
	{
		if (n <= 0 || k <= 0)
		{
			return;
		}
		// Auxiliary array which is collect result
		int[] result = new int[k];
		// Find resultant subsequences
		this.subSequences(result, n, 0, 0, k);
	}
	public static void Main(String[] args)
	{
		Combination task = new Combination();
		int n = 5;
		int k = 3;
		// Test A
		// n >= k
		// length k = 3
		// number (1..5)
		/*
		    1 2 3
		    1 2 4
		    1 2 5
		    1 3 4
		    1 3 5
		    1 4 5
		    2 3 4
		    2 3 5
		    2 4 5
		    3 4 5
		  --------------
		    All Unique result
		*/
		task.findSequence(n, k);
	}
}

Output

 1 2 3
 1 2 4
 1 2 5
 1 3 4
 1 3 5
 1 4 5
 2 3 4
 2 3 5
 2 4 5
 3 4 5
package main
import "fmt"
// Go program for
// Increasing subsequence of n natural number in k size
type Combination struct {}
func getCombination() * Combination {
	var me *Combination = &Combination {}
	return me
}
// Display result elements
func(this Combination) printResult(result[] int, k int) {
	for i := 0 ; i < k ; i++ {
		fmt.Print(" ", result[i])
	}
	fmt.Print("\n")
}
func(this Combination) subSequences(result[] int, 
	num int, 
	start int, 
	count int, 
	k int) {
	if count == k {
		// Display calculated result
		this.printResult(result, k)
		return
	} else if count > k {
		return
	} else {
		for i := start + 1 ; i <= num ; i++ {
			// Collect elements
			result[count] = i
			this.subSequences(result, 
							num, 
							i, 
							count + 1, 
							k)
		}
	}
}
// Handles the request to find resultant subsequences
func(this Combination) findSequence(n, k int) {
	if n <= 0 || k <= 0 {
		return
	}
	// Auxiliary array which is collect result
	var result = make([] int, k)
	// Find resultant subsequences
	this.subSequences(result, n, 0, 0, k)
}
func main() {
	var task * Combination = getCombination()
	var n int = 5
	var k int = 3
	// Test A
	// n >= k
	// length k = 3
	// number (1..5)
	/*
	    1 2 3
	    1 2 4
	    1 2 5
	    1 3 4
	    1 3 5
	    1 4 5
	    2 3 4
	    2 3 5
	    2 4 5
	    3 4 5
	  --------------
	    All Unique result
	*/
	task.findSequence(n, k)
}

Output

 1 2 3
 1 2 4
 1 2 5
 1 3 4
 1 3 5
 1 4 5
 2 3 4
 2 3 5
 2 4 5
 3 4 5
<?php
// Php program for
// Increasing subsequence of n natural number in k size
class Combination
{
	// Display result elements
	public	function printResult($result, $k)
	{
		for ($i = 0; $i < $k; ++$i)
		{
			echo(" ".$result[$i]);
		}
		echo("\n");
	}
	public	function subSequences($result, 
                                   $num, 
                                   $start, 
                                   $count, $k)
	{
		if ($count == $k)
		{
			// Display calculated result
			$this->printResult($result, $k);
			return;
		}
		else if ($count > $k)
		{
			return;
		}
		else
		{
			for ($i = $start + 1; $i <= $num; ++$i)
			{
				// Collect elements
				$result[$count] = $i;
				$this->subSequences($result, 
                                    $num, $i, 
                                    $count + 1, $k);
			}
		}
	}
	// Handles the request to find resultant subsequences
	public	function findSequence($n, $k)
	{
		if ($n <= 0 || $k <= 0)
		{
			return;
		}
		// Auxiliary array which is collect result
		$result = array_fill(0, $k, 0);
		// Find resultant subsequences
		$this->subSequences($result, $n, 0, 0, $k);
	}
}

function main()
{
	$task = new Combination();
	$n = 5;
	$k = 3;
	// Test A
	// n >= k
	// length k = 3
	// number (1..5)
	/*
	    1 2 3
	    1 2 4
	    1 2 5
	    1 3 4
	    1 3 5
	    1 4 5
	    2 3 4
	    2 3 5
	    2 4 5
	    3 4 5
	  --------------
	    All Unique result
	*/
	$task->findSequence($n, $k);
}
main();

Output

 1 2 3
 1 2 4
 1 2 5
 1 3 4
 1 3 5
 1 4 5
 2 3 4
 2 3 5
 2 4 5
 3 4 5
// Node JS program for
// Increasing subsequence of n natural number in k size
class Combination
{
	// Display result elements
	printResult(result, k)
	{
		for (var i = 0; i < k; ++i)
		{
			process.stdout.write(" " + result[i]);
		}
		process.stdout.write("\n");
	}
	subSequences(result, num, start, count, k)
	{
		if (count == k)
		{
			// Display calculated result
			this.printResult(result, k);
			return;
		}
		else if (count > k)
		{
			return;
		}
		else
		{
			for (var i = start + 1; i <= num; ++i)
			{
				// Collect elements
				result[count] = i;
				this.subSequences(result, 
                                  num, i, 
                                  count + 1, k);
			}
		}
	}
	// Handles the request to find resultant subsequences
	findSequence(n, k)
	{
		if (n <= 0 || k <= 0)
		{
			return;
		}
		// Auxiliary array which is collect result
		var result = Array(k).fill(0);
		// Find resultant subsequences
		this.subSequences(result, n, 0, 0, k);
	}
}

function main()
{
	var task = new Combination();
	var n = 5;
	var k = 3;
	// Test A
	// n >= k
	// length k = 3
	// number (1..5)
	/*
	    1 2 3
	    1 2 4
	    1 2 5
	    1 3 4
	    1 3 5
	    1 4 5
	    2 3 4
	    2 3 5
	    2 4 5
	    3 4 5
	  --------------
	    All Unique result
	*/
	task.findSequence(n, k);
}
main();

Output

 1 2 3
 1 2 4
 1 2 5
 1 3 4
 1 3 5
 1 4 5
 2 3 4
 2 3 5
 2 4 5
 3 4 5
#  Python 3 program for
#  Increasing subsequence of n natural number in k size
class Combination :
	#  Display result elements
	def printResult(self, result, k) :
		i = 0
		while (i < k) :
			print(" ", result[i], end = "")
			i += 1
		
		print(end = "\n")
	
	def subSequences(self, result, 
                     num, 
                     start, 
                     count, 
                     k) :
		if (count == k) :
			#  Display calculated result
			self.printResult(result, k)
			return
		elif (count > k) :
			return
		else :
			i = start + 1
			while (i <= num) :
				#  Collect elements
				result[count] = i
				self.subSequences(result, 
                                  num, i, 
                                  count + 1, k)
				i += 1
			
		
	
	#  Handles the request to find resultant subsequences
	def findSequence(self, n, k) :
		if (n <= 0 or k <= 0) :
			return
		
		#  Auxiliary list which is collect result
		result = [0] * (k)
		#  Find resultant subsequences
		self.subSequences(result, n, 0, 0, k)
	

def main() :
	task = Combination()
	n = 5
	k = 3
	#  Test A
	#  n >= k
	#  length k = 3
	#  number (1..5)
	#    1 2 3
	#    1 2 4
	#    1 2 5
	#    1 3 4
	#    1 3 5
	#    1 4 5
	#    2 3 4
	#    2 3 5
	#    2 4 5
	#    3 4 5
	#  --------------
	#    All Unique result
	task.findSequence(n, k)

if __name__ == "__main__": main()

Output

  1  2  3
  1  2  4
  1  2  5
  1  3  4
  1  3  5
  1  4  5
  2  3  4
  2  3  5
  2  4  5
  3  4  5
#  Ruby program for
#  Increasing subsequence of n natural number in k size
class Combination 
	#  Display result elements
	def printResult(result, k) 
		i = 0
		while (i < k) 
			print(" ", result[i])
			i += 1
		end

		print("\n")
	end

	def subSequences(result, num, start, count, k) 
		if (count == k) 
			#  Display calculated result
			self.printResult(result, k)
			return
		elsif (count > k) 
			return
		else
 
			i = start + 1
			while (i <= num) 
				#  Collect elements
				result[count] = i
				self.subSequences(result, num, i, count + 1, k)
				i += 1
			end

		end

	end

	#  Handles the request to find resultant subsequences
	def findSequence(n, k) 
		if (n <= 0 || k <= 0) 
			return
		end

		#  Auxiliary array which is collect result
		result = Array.new(k) {0}
		#  Find resultant subsequences
		self.subSequences(result, n, 0, 0, k)
	end

end

def main() 
	task = Combination.new()
	n = 5
	k = 3
	#  Test A
	#  n >= k
	#  length k = 3
	#  number (1..5)
	#    1 2 3
	#    1 2 4
	#    1 2 5
	#    1 3 4
	#    1 3 5
	#    1 4 5
	#    2 3 4
	#    2 3 5
	#    2 4 5
	#    3 4 5
	#  --------------
	#    All Unique result
	task.findSequence(n, k)
end

main()

Output

 1 2 3
 1 2 4
 1 2 5
 1 3 4
 1 3 5
 1 4 5
 2 3 4
 2 3 5
 2 4 5
 3 4 5
// Scala program for
// Increasing subsequence of n natural number in k size
class Combination()
{
	// Display result elements
	def printResult(result: Array[Int], k: Int): Unit = {
		var i: Int = 0;
		while (i < k)
		{
			print(" " + result(i));
			i += 1;
		}
		print("\n");
	}
	def subSequences(result: Array[Int], 
      	num: Int, start: Int, 
        count: Int, k: Int): Unit = {
		if (count == k)
		{
			// Display calculated result
			printResult(result, k);
			return;
		}
		else if (count > k)
		{
			return;
		}
		else
		{
			var i: Int = start + 1;
			while (i <= num)
			{
				// Collect elements
				result(count) = i;
				subSequences(result, num, i, count + 1, k);
				i += 1;
			}
		}
	}
	// Handles the request to find resultant subsequences
	def findSequence(n: Int, k: Int): Unit = {
		if (n <= 0 || k <= 0)
		{
			return;
		}
		// Auxiliary array which is collect result
		var result: Array[Int] = Array.fill[Int](k)(0);
		// Find resultant subsequences
		subSequences(result, n, 0, 0, k);
	}
}
object Main
{
	def main(args: Array[String]): Unit = {
		var task: Combination = new Combination();
		var n: Int = 5;
		var k: Int = 3;
		// Test A
		// n >= k
		// length k = 3
		// number (1..5)
		/*
		    1 2 3
		    1 2 4
		    1 2 5
		    1 3 4
		    1 3 5
		    1 4 5
		    2 3 4
		    2 3 5
		    2 4 5
		    3 4 5
		  --------------
		    All Unique result
		*/
		task.findSequence(n, k);
	}
}

Output

 1 2 3
 1 2 4
 1 2 5
 1 3 4
 1 3 5
 1 4 5
 2 3 4
 2 3 5
 2 4 5
 3 4 5
// Swift 4 program for
// Increasing subsequence of n natural number in k size
class Combination
{
	// Display result elements
	func printResult(_ result: [Int], _ k: Int)
	{
		var i: Int = 0;
		while (i < k)
		{
			print(" ", result[i], terminator: "");
			i += 1;
		}
		print(terminator: "\n");
	}
	func subSequences(_ result: inout[Int], 
      _ num: Int, _ start: Int, 
      _ count: Int, _ k: Int)
	{
		if (count == k)
		{
			// Display calculated result
			self.printResult(result, k);
			return;
		}
		else if (count > k)
		{
			return;
		}
		else
		{
			var i: Int = start + 1;
			while (i <= num)
			{
				// Collect elements
				result[count] = i;
				self.subSequences(&result, num, i, count + 1, k);
				i += 1;
			}
		}
	}
	// Handles the request to find resultant subsequences
	func findSequence(_ n: Int, _ k: Int)
	{
		if (n <= 0 || k <= 0)
		{
			return;
		}
		// Auxiliary array which is collect result
		var result: [Int] = Array(repeating: 0, count: k);
		// Find resultant subsequences
		self.subSequences(&result, n, 0, 0, k);
	}
}
func main()
{
	let task: Combination = Combination();
	let n: Int = 5;
	let k: Int = 3;
	// Test A
	// n >= k
	// length k = 3
	// number (1..5)
	/*
	    1 2 3
	    1 2 4
	    1 2 5
	    1 3 4
	    1 3 5
	    1 4 5
	    2 3 4
	    2 3 5
	    2 4 5
	    3 4 5
	  --------------
	    All Unique result
	*/
	task.findSequence(n, k);
}
main();

Output

  1  2  3
  1  2  4
  1  2  5
  1  3  4
  1  3  5
  1  4  5
  2  3  4
  2  3  5
  2  4  5
  3  4  5
// Kotlin program for
// Increasing subsequence of n natural number in k size
class Combination
{
	// Display result elements
	fun printResult(result: Array < Int > , k: Int): Unit
	{
		var i: Int = 0;
		while (i < k)
		{
			print(" " + result[i]);
			i += 1;
		}
		print("\n");
	}
	fun subSequences(
      result: Array < Int > , 
      num: Int, 
      start: Int, 
      count: Int, 
      k: Int): Unit
	{
		if (count == k)
		{
			// Display calculated result
			this.printResult(result, k);
			return;
		}
		else if (count > k)
		{
			return;
		}
		else
		{
			var i: Int = start + 1;
			while (i <= num)
			{
				// Collect elements
				result[count] = i;
				this.subSequences(result, num, i, count + 1, k);
				i += 1;
			}
		}
	}
	// Handles the request to find resultant subsequences
	fun findSequence(n: Int, k: Int): Unit
	{
		if (n <= 0 || k <= 0)
		{
			return;
		}
		// Auxiliary array which is collect result
		val result: Array < Int > = Array(k)
		{
			0
		};
		// Find resultant subsequences
		this.subSequences(result, n, 0, 0, k);
	}
}
fun main(args: Array < String > ): Unit
{
	val task: Combination = Combination();
	val n: Int = 5;
	val k: Int = 3;
	// Test A
	// n >= k
	// length k = 3
	// number (1..5)
	/*
	    1 2 3
	    1 2 4
	    1 2 5
	    1 3 4
	    1 3 5
	    1 4 5
	    2 3 4
	    2 3 5
	    2 4 5
	    3 4 5
	  --------------
	    All Unique result
	*/
	task.findSequence(n, k);
}

Output

 1 2 3
 1 2 4
 1 2 5
 1 3 4
 1 3 5
 1 4 5
 2 3 4
 2 3 5
 2 4 5
 3 4 5




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