Skip to main content

Print all non-increasing sequence of sum equal to a given number k

Here given code implementation process.

// C Program
// Print all non-increasing sequences of sum equal to a given number k
#include <stdio.h>

void subSequences(int result[], int start, int count, int sum, int k)
{
	if (sum == k)
	{
		// Display of resultant decreasing subsequence
		for (int i = count - 1; i >= 0; --i)
		{
			// Display element value
			printf(" %d", result[i]);
		}
		printf("\n");
	}
	else if (sum > k || count < 0)
	{
		return;
	}
	else
	{
		for (int i = start; i <= k; ++i)
		{
			// Collect elements 
			result[count] = i;
			// Find subsequence by using recursively
			subSequences(result, i, count + 1, sum + i, k);
		}
	}
}
// Handles the request to find resultant subsequences
void findSequence(int k)
{
	if (k <= 0)
	{
		return;
	}
	// Auxiliary array which is collect result
	int result[k];
  	printf("Given K : %d \n",k);
	subSequences(result, 1, 0, 0, k);
}
int main()
{
	// Test A
	int k = 6;
	findSequence(k);
	// Test B
	k = 4;
	findSequence(k);
	return 0;
}

Output

Given K : 6
 1 1 1 1 1 1
 2 1 1 1 1
 3 1 1 1
 2 2 1 1
 4 1 1
 3 2 1
 5 1
 2 2 2
 4 2
 3 3
 6
Given K : 4
 1 1 1 1
 2 1 1
 3 1
 2 2
 4
// Java program for
// Print all non-increasing sequences of sum equal to a given number k
public class Combination
{
	public void subSequences(int[] result, 
  		int start, 
  		int count, 
  		int sum, 
  		int k)
	{
		if (sum == k)
		{
			// Display of resultant decreasing subsequence
			for (int i = count - 1; i >= 0; --i)
			{
				// Display element value
				System.out.print(" " + result[i]);
			}
			System.out.print("\n");
		}
		else if (sum > k || count < 0)
		{
			return;
		}
		else
		{
			for (int i = start; i <= k; ++i)
			{
				// Collect elements 
				result[count] = i;
				// Find subsequence by using recursively
				subSequences(result, i, count + 1, sum + i, k);
			}
		}
	}
	// Handles the request to find resultant subsequences
	public void findSequence(int k)
	{
		if (k <= 0)
		{
			return;
		}
		// Auxiliary array which is collect result
		int[] result = new int[k];
		System.out.println("Given K : " + k);
		subSequences(result, 1, 0, 0, k);
	}
	public static void main(String[] args)
	{
		Combination task = new Combination();
		// Test A
		int k = 6;
		task.findSequence(k);
		// Test B
		k = 4;
		task.findSequence(k);
	}
}

Output

Given K : 6
 1 1 1 1 1 1
 2 1 1 1 1
 3 1 1 1
 2 2 1 1
 4 1 1
 3 2 1
 5 1
 2 2 2
 4 2
 3 3
 6
Given K : 4
 1 1 1 1
 2 1 1
 3 1
 2 2
 4
// Include header file
#include <iostream>

using namespace std;
// C++ program for
// Print all non-increasing sequences of sum equal to a given number k
class Combination
{
	public: void subSequences(
      int result[], int start, 
      int count, int sum, int k)
	{
		if (sum == k)
		{
			// Display of resultant decreasing subsequence
			for (int i = count - 1; i >= 0; --i)
			{
				// Display element value
				cout << " " << result[i];
			}
			cout << "\n";
		}
		else if (sum > k || count < 0)
		{
			return;
		}
		else
		{
			for (int i = start; i <= k; ++i)
			{
				// Collect elements 
				result[count] = i;
				// Find subsequence by using recursively
				this->subSequences(result, i, 
                                   count + 1, sum + i, k);
			}
		}
	}
	// Handles the request to find resultant subsequences
	void findSequence(int k)
	{
		if (k <= 0)
		{
			return;
		}
		// Auxiliary array which is collect result
		int result[k];
		cout << "Given K : " << k << endl;
		this->subSequences(result, 1, 0, 0, k);
	}
};
int main()
{
	Combination *task = new Combination();
	// Test A
	int k = 6;
	task->findSequence(k);
	// Test B
	k = 4;
	task->findSequence(k);
	return 0;
}

Output

Given K : 6
 1 1 1 1 1 1
 2 1 1 1 1
 3 1 1 1
 2 2 1 1
 4 1 1
 3 2 1
 5 1
 2 2 2
 4 2
 3 3
 6
Given K : 4
 1 1 1 1
 2 1 1
 3 1
 2 2
 4
// Include namespace system
using System;
// Csharp program for
// Print all non-increasing sequence of sum equal to a given number k
public class Combination
{
	public void subSequences(
  		int[] result, int start, 
        int count, int sum, int k)
	{
		if (sum == k)
		{
			// Display of resultant decreasing subsequence
			for (int i = count - 1; i >= 0; --i)
			{
				// Display element value
				Console.Write(" " + result[i]);
			}
			Console.Write("\n");
		}
		else if (sum > k || count < 0)
		{
			return;
		}
		else
		{
			for (int i = start; i <= k; ++i)
			{
				// Collect elements 
				result[count] = i;
				// Find subsequence by using recursively
				this.subSequences(result, i, count + 1, sum + i, k);
			}
		}
	}
	// Handles the request to find resultant subsequences
	public void findSequence(int k)
	{
		if (k <= 0)
		{
			return;
		}
		// Auxiliary array which is collect result
		int[] result = new int[k];
		Console.WriteLine("Given K : " + k);
		this.subSequences(result, 1, 0, 0, k);
	}
	public static void Main(String[] args)
	{
		Combination task = new Combination();
		// Test A
		int k = 6;
		task.findSequence(k);
		// Test B
		k = 4;
		task.findSequence(k);
	}
}

Output

Given K : 6
 1 1 1 1 1 1
 2 1 1 1 1
 3 1 1 1
 2 2 1 1
 4 1 1
 3 2 1
 5 1
 2 2 2
 4 2
 3 3
 6
Given K : 4
 1 1 1 1
 2 1 1
 3 1
 2 2
 4
package main
import "fmt"
// Go program for
// Print all non-increasing sequence of 
// sum equal to a given number k
type Combination struct {}
func getCombination() * Combination {
	var me *Combination = &Combination {}
	return me
}
func(this Combination) subSequences(result[] int, 
	start int, count int, 
	sum int, k int) {
	if sum == k {
		// Display of resultant decreasing subsequence
		for i := count - 1 ; i >= 0 ; i-- {
			// Display element value
			fmt.Print(" ", result[i])
		}
		fmt.Print("\n")
	} else if sum > k || count < 0 {
		return
	} else {
		for i := start ; i <= k ; i++ {
			// Collect elements 
			result[count] = i
			// Find subsequence by using recursively
			this.subSequences(result, i, 
				count + 1, sum + i, k)
		}
	}
}
// Handles the request to find resultant subsequences
func(this Combination) findSequence(k int) {
	if k <= 0 {
		return
	}
	// Auxiliary array which is collect result
	var result = make([] int, k)
	fmt.Println("Given K : ", k)
	this.subSequences(result, 1, 0, 0, k)
}
func main() {
	var task * Combination = getCombination()
	// Test A
	var k int = 6
	task.findSequence(k)
	// Test B
	k = 4
	task.findSequence(k)
}

Output

Given K : 6
 1 1 1 1 1 1
 2 1 1 1 1
 3 1 1 1
 2 2 1 1
 4 1 1
 3 2 1
 5 1
 2 2 2
 4 2
 3 3
 6
Given K : 4
 1 1 1 1
 2 1 1
 3 1
 2 2
 4
<?php
// Php program for
// Print all non-increasing sequence of sum equal to a given number k
class Combination
{
	public	function subSequences($result, $start, $count, $sum, $k)
	{
		if ($sum == $k)
		{
			// Display of resultant decreasing subsequence
			for ($i = $count - 1; $i >= 0; --$i)
			{
				// Display element value
				echo(" ".$result[$i]);
			}
			echo("\n");
		}
		else if ($sum > $k || $count < 0)
		{
			return;
		}
		else
		{
			for ($i = $start; $i <= $k; ++$i)
			{
				// Collect elements 
				$result[$count] = $i;
				// Find subsequence by using recursively
				$this->subSequences($result, $i, 
                                    $count + 1, 
                                    $sum + $i, $k);
			}
		}
	}
	// Handles the request to find resultant subsequences
	public	function findSequence($k)
	{
		if ($k <= 0)
		{
			return;
		}
		// Auxiliary array which is collect result
		$result = array_fill(0, $k, 0);
		echo("Given K : ".$k."\n");
		$this->subSequences($result, 1, 0, 0, $k);
	}
}

function main()
{
	$task = new Combination();
	// Test A
	$k = 6;
	$task->findSequence($k);
	// Test B
	$k = 4;
	$task->findSequence($k);
}
main();

Output

Given K : 6
 1 1 1 1 1 1
 2 1 1 1 1
 3 1 1 1
 2 2 1 1
 4 1 1
 3 2 1
 5 1
 2 2 2
 4 2
 3 3
 6
Given K : 4
 1 1 1 1
 2 1 1
 3 1
 2 2
 4
// Node JS program for
// Print all non-increasing sequence of sum equal to a given number k
class Combination
{
	subSequences(result, start, count, sum, k)
	{
		if (sum == k)
		{
			// Display of resultant decreasing subsequence
			for (var i = count - 1; i >= 0; --i)
			{
				// Display element value
				process.stdout.write(" " + result[i]);
			}
			process.stdout.write("\n");
		}
		else if (sum > k || count < 0)
		{
			return;
		}
		else
		{
			for (var i = start; i <= k; ++i)
			{
				// Collect elements 
				result[count] = i;
				// Find subsequence by using recursively
				this.subSequences(result, i, 
                                  count + 1, 
                                  sum + i, 
                                  k);
			}
		}
	}
	// Handles the request to find resultant subsequences
	findSequence(k)
	{
		if (k <= 0)
		{
			return;
		}
		// Auxiliary array which is collect result
		var result = Array(k).fill(0);
		console.log("Given K : " + k);
		this.subSequences(result, 1, 0, 0, k);
	}
}

function main()
{
	var task = new Combination();
	// Test A
	var k = 6;
	task.findSequence(k);
	// Test B
	k = 4;
	task.findSequence(k);
}
main();

Output

Given K : 6
 1 1 1 1 1 1
 2 1 1 1 1
 3 1 1 1
 2 2 1 1
 4 1 1
 3 2 1
 5 1
 2 2 2
 4 2
 3 3
 6
Given K : 4
 1 1 1 1
 2 1 1
 3 1
 2 2
 4
#  Python 3 program for
#  Print all non-increasing sequence of sum equal to a given number k
class Combination :
	def subSequences(self, result, start, count, sum, k) :
		if (sum == k) :
			i = count - 1
			#  Display of resultant decreasing subsequence
			while (i >= 0) :
				#  Display element value
				print(" ", result[i], end = "")
				i -= 1
			
			print(end = "\n")
		elif (sum > k or count < 0) :
			return
		else :
			i = start
			while (i <= k) :
				#  Collect elements 
				result[count] = i
				#  Find subsequence by using recursively
				self.subSequences(result, i, count + 1, sum + i, k)
				i += 1
			
		
	
	#  Handles the request to find resultant subsequences
	def findSequence(self, k) :
		if (k <= 0) :
			return
		
		#  Auxiliary list which is collect result
		result = [0] * (k)
		print("Given K : ", k)
		self.subSequences(result, 1, 0, 0, k)
	

def main() :
	task = Combination()
	#  Test A
	k = 6
	task.findSequence(k)
	#  Test B
	k = 4
	task.findSequence(k)

if __name__ == "__main__": main()

Output

Given K :  6
  1  1  1  1  1  1
  2  1  1  1  1
  3  1  1  1
  2  2  1  1
  4  1  1
  3  2  1
  5  1
  2  2  2
  4  2
  3  3
  6
Given K :  4
  1  1  1  1
  2  1  1
  3  1
  2  2
  4
#  Ruby program for
#  Print all non-increasing sequence of sum equal to a given number k
class Combination 
	def subSequences(result, start, count, sum, k) 
		if (sum == k) 
			i = count - 1
			#  Display of resultant decreasing subsequence
			while (i >= 0) 
				#  Display element value
				print(" ", result[i])
				i -= 1
			end

			print("\n")
		elsif (sum > k || count < 0) 
			return
		else
 
			i = start
			while (i <= k) 
				#  Collect elements 
				result[count] = i
				#  Find subsequence by using recursively
				self.subSequences(result, i, count + 1, sum + i, k)
				i += 1
			end

		end

	end

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

		#  Auxiliary array which is collect result
		result = Array.new(k) {0}
		print("Given K : ", k, "\n")
		self.subSequences(result, 1, 0, 0, k)
	end

end

def main() 
	task = Combination.new()
	#  Test A
	k = 6
	task.findSequence(k)
	#  Test B
	k = 4
	task.findSequence(k)
end

main()

Output

Given K : 6
 1 1 1 1 1 1
 2 1 1 1 1
 3 1 1 1
 2 2 1 1
 4 1 1
 3 2 1
 5 1
 2 2 2
 4 2
 3 3
 6
Given K : 4
 1 1 1 1
 2 1 1
 3 1
 2 2
 4
// Scala program for
// Print all non-increasing sequence of sum equal to a given number k
class Combination()
{
	def subSequences(result: Array[Int], 
        start: Int, count: Int, 
        sum: Int, k: Int): Unit = {
		if (sum == k)
		{
			var i: Int = count - 1;
			// Display of resultant decreasing subsequence
			while (i >= 0)
			{
				// Display element value
				print(" " + result(i));
				i -= 1;
			}
			print("\n");
		}
		else if (sum > k || count < 0)
		{
			return;
		}
		else
		{
			var i: Int = start;
			while (i <= k)
			{
				// Collect elements 
				result(count) = i;
				// Find subsequence by using recursively
				subSequences(result, i, count + 1, sum + i, k);
				i += 1;
			}
		}
	}
	// Handles the request to find resultant subsequences
	def findSequence(k: Int): Unit = {
		if (k <= 0)
		{
			return;
		}
		// Auxiliary array which is collect result
		var result: Array[Int] = Array.fill[Int](k)(0);
		println("Given K : " + k);
		subSequences(result, 1, 0, 0, k);
	}
}
object Main
{
	def main(args: Array[String]): Unit = {
		var task: Combination = new Combination();
		// Test A
		var k: Int = 6;
		task.findSequence(k);
		// Test B
		k = 4;
		task.findSequence(k);
	}
}

Output

Given K : 6
 1 1 1 1 1 1
 2 1 1 1 1
 3 1 1 1
 2 2 1 1
 4 1 1
 3 2 1
 5 1
 2 2 2
 4 2
 3 3
 6
Given K : 4
 1 1 1 1
 2 1 1
 3 1
 2 2
 4
// Swift 4 program for
// Print all non-increasing sequence of sum equal to a given number k
class Combination
{
	func subSequences(_ result: inout[Int], 
  		_ start: Int, 
        _ count: Int, 
        _ sum: Int, 
        _ k: Int)
	{
		if (sum == k)
		{
			var i: Int = count - 1;
			// Display of resultant decreasing subsequence
			while (i >= 0)
			{
				// Display element value
				print(" ", result[i], terminator: "");
				i -= 1;
			}
			print(terminator: "\n");
		}
		else if (sum > k || count < 0)
		{
			return;
		}
		else
		{
			var i: Int = start;
			while (i <= k)
			{
				// Collect elements 
				result[count] = i;
				// Find subsequence by using recursively
				self.subSequences(&result, i, count + 1, sum + i, k);
				i += 1;
			}
		}
	}
	// Handles the request to find resultant subsequences
	func findSequence(_ k: Int)
	{
		if (k <= 0)
		{
			return;
		}
		// Auxiliary array which is collect result
		var result: [Int] = Array(repeating: 0, count: k);
		print("Given K : ", k);
		self.subSequences(&result, 1, 0, 0, k);
	}
}
func main()
{
	let task: Combination = Combination();
	// Test A
	var k: Int = 6;
	task.findSequence(k);
	// Test B
	k = 4;
	task.findSequence(k);
}
main();

Output

Given K :  6
  1  1  1  1  1  1
  2  1  1  1  1
  3  1  1  1
  2  2  1  1
  4  1  1
  3  2  1
  5  1
  2  2  2
  4  2
  3  3
  6
Given K :  4
  1  1  1  1
  2  1  1
  3  1
  2  2
  4
// Kotlin program for
// Print all non-increasing sequence of sum equal to a given number k
class Combination
{
	fun subSequences(result: Array < Int > , 
                      start: Int, count: Int, 
                      sum: Int, k: Int): Unit
	{
		if (sum == k)
		{
			var i: Int = count - 1;
			// Display of resultant decreasing subsequence
			while (i >= 0)
			{
				// Display element value
				print(" " + result[i]);
				i -= 1;
			}
			print("\n");
		}
		else if (sum > k || count < 0)
		{
			return;
		}
		else
		{
			var i: Int = start;
			while (i <= k)
			{
				// Collect elements 
				result[count] = i;
				// Find subsequence by using recursively
				this.subSequences(result, i, count + 1, sum + i, k);
				i += 1;
			}
		}
	}
	// Handles the request to find resultant subsequences
	fun findSequence(k: Int): Unit
	{
		if (k <= 0)
		{
			return;
		}
		// Auxiliary array which is collect result
		var result: Array < Int > = Array(k)
		{
			0
		};
		println("Given K : " + k);
		this.subSequences(result, 1, 0, 0, k);
	}
}
fun main(args: Array < String > ): Unit
{
	val task: Combination = Combination();
	// Test A
	var k: Int = 6;
	task.findSequence(k);
	// Test B
	k = 4;
	task.findSequence(k);
}

Output

Given K : 6
 1 1 1 1 1 1
 2 1 1 1 1
 3 1 1 1
 2 2 1 1
 4 1 1
 3 2 1
 5 1
 2 2 2
 4 2
 3 3
 6
Given K : 4
 1 1 1 1
 2 1 1
 3 1
 2 2
 4




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