Skip to main content

Generate all distinct combinations of N numbers whose sum is X

Here given code implementation process.

// C Program
// Generate all distinct combinations of N numbers whose sum is X
#include <stdio.h>

// Display result
void printSequence(int result[], int n)
{
	for (int i = 0; i < n; ++i)
	{
		printf("  %d", result[i]);
	}
	printf("\n");
}
void findCombination(int value, 
                     int result[], 
  					 int index, 
                     int x, 
                     int sum,
                     int n)
{
	if (index == n && x == sum)
	{
		// Display calculated result
		printSequence(result, index);
		return;
	}
	if (index >= n || sum > x)
	{
		// Base case when stop process
		return;
	}
	for (int i = value; i < x; ++i)
	{
		// Collects resultant value
		result[index] = i;
		findCombination(i + 1,
                        result,
                        index + 1,
                        x,
                        sum + i,
                        n);
	}
}
// Handles the request to find combination
void combination(int x, int n)
{
	if (n <= 0)
	{
		return;
	}
	// Collect result
	int result[n];
	// Test
	findCombination(1, result, 0, x, 0, n);
}
int main()
{
	// x = 15 sum
	// n = 4 length
	//-----------------
	//  1 + 2 + 3 + 9  = 15
	//  1 + 2 + 4 + 8  = 15
	//  1 + 2 + 5 + 7  = 15
	//  1 + 3 + 4 + 7  = 15
	//  1 + 3 + 5 + 6  = 15
	//  2 + 3 + 4 + 6  = 15
	//-----------------
	combination(15, 4);
	return 0;
}

Output

  1  2  3  9
  1  2  4  8
  1  2  5  7
  1  3  4  7
  1  3  5  6
  2  3  4  6
// Java Program 
// Generate all distinct combinations of N numbers whose sum is X
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 void findCombination(int value, 
                                int[] result, 
      							int index, 
        						int x, 
          						int sum, 
            					int n)
	{
		if (index == n && x == sum)
		{
			// Display calculated result
			printSequence(result, index);
			return;
		}
		if (index >= n || sum > x)
		{
			// Base case when stop process
			return;
		}
		for (int i = value; i < x; ++i)
		{
			// Collects resultant value
			result[index] = i;
			// Find next combination
			findCombination(i + 1, result, 
                            index + 1, 
                            x, 
                            sum + i, 
                            n);
		}
	}
	// Handles the request to find combination
	public void combination(int x, int n)
	{
		if (n <= 0)
		{
			return;
		}
		// Collect result
		int[] result = new int[n];
		// Test
		findCombination(1, result, 0, x, 0, n);
	}
	public static void main(String args[])
	{
		Combinations task = new Combinations();
		// x = 15 sum
		// n = 4 length
		//-----------------
		//  1 + 2 + 3 + 9  = 15
		//  1 + 2 + 4 + 8  = 15
		//  1 + 2 + 5 + 7  = 15
		//  1 + 3 + 4 + 7  = 15
		//  1 + 3 + 5 + 6  = 15
		//  2 + 3 + 4 + 6  = 15
		//-----------------
		task.combination(15, 4);
	}
}

Output

 1 2 3 9
 1 2 4 8
 1 2 5 7
 1 3 4 7
 1 3 5 6
 2 3 4 6
// Include header file
#include <iostream>
using namespace std;
// C++ Program 
// Generate all distinct combinations of N numbers whose sum is X
class Combinations
{
	public:
		// Display result
		void printSequence(int result[], int n)
		{
			for (int i = 0; i < n; ++i)
			{
				cout << " " << result[i];
			}
			cout << "\n";
		}
	void findCombination(int value, int result[], 
      int index, int x, int sum, int n)
	{
		if (index == n && x == sum)
		{
			// Display calculated result
			this->printSequence(result, index);
			return;
		}
		if (index >= n || sum > x)
		{
			// Base case when stop process
			return;
		}
		for (int i = value; i < x; ++i)
		{
			// Collects resultant value
			result[index] = i;
			// Find next combination
			this->findCombination(i + 1, result, 
                                  index + 1, x, 
                                  sum + i, n);
		}
	}
	// Handles the request to find combination
	void combination(int x, int n)
	{
		if (n <= 0)
		{
			return;
		}
		// Collect result
		int result[n];
		// Test
		this->findCombination(1, result, 0, x, 0, n);
	}
};
int main()
{
	Combinations *task = new Combinations();
	// x = 15 sum
	// n = 4 length
	//-----------------
	//  1 + 2 + 3 + 9  = 15
	//  1 + 2 + 4 + 8  = 15
	//  1 + 2 + 5 + 7  = 15
	//  1 + 3 + 4 + 7  = 15
	//  1 + 3 + 5 + 6  = 15
	//  2 + 3 + 4 + 6  = 15
	//-----------------
	task->combination(15, 4);
	return 0;
}

Output

 1 2 3 9
 1 2 4 8
 1 2 5 7
 1 3 4 7
 1 3 5 6
 2 3 4 6
// Include namespace system
using System;
// Csharp Program 
// Generate all distinct combinations of N numbers whose sum is X
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 void findCombination(int value, 
                                int[] result, 
      int index, int x, int sum, int n)
	{
		if (index == n && x == sum)
		{
			// Display calculated result
			this.printSequence(result, index);
			return;
		}
		if (index >= n || sum > x)
		{
			// Base case when stop process
			return;
		}
		for (int i = value; i < x; ++i)
		{
			// Collects resultant value
			result[index] = i;
			// Find next combination
			this.findCombination(i + 1, 
                                 result, 
                                 index + 1, 
                                 x, sum + i, n);
		}
	}
	// Handles the request to find combination
	public void combination(int x, int n)
	{
		if (n <= 0)
		{
			return;
		}
		// Collect result
		int[] result = new int[n];
		// Test
		this.findCombination(1, result, 0, x, 0, n);
	}
	public static void Main(String[] args)
	{
		Combinations task = new Combinations();
		// x = 15 sum
		// n = 4 length
		//-----------------
		//  1 + 2 + 3 + 9  = 15
		//  1 + 2 + 4 + 8  = 15
		//  1 + 2 + 5 + 7  = 15
		//  1 + 3 + 4 + 7  = 15
		//  1 + 3 + 5 + 6  = 15
		//  2 + 3 + 4 + 6  = 15
		//-----------------
		task.combination(15, 4);
	}
}

Output

 1 2 3 9
 1 2 4 8
 1 2 5 7
 1 3 4 7
 1 3 5 6
 2 3 4 6
package main
import "fmt"
// Go Program 
// Generate all distinct combinations of N numbers whose sum is X
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) findCombination(value int, result[] int, 
	index int, x int, sum int, n int) {
	if index == n && x == sum {
		// Display calculated result
		this.printSequence(result, index)
		return
	}
	if index >= n || sum > x {
		// Base case when stop process
		return
	}
	for i := value ; i < x ; i++ {
		// Collects resultant value
		result[index] = i
		// Find next combination
		this.findCombination(i + 1, result, index + 1, x, sum + i, n)
	}
}
// Handles the request to find combination
func(this Combinations) combination(x, n int) {
	if n <= 0 {
		return
	}
	// Collect result
	var result = make([] int, n)
	// Test
	this.findCombination(1, result, 0, x, 0, n)
}
func main() {
	var task * Combinations = getCombinations()
	// x = 15 sum
	// n = 4 length
	//-----------------
	//  1 + 2 + 3 + 9  = 15
	//  1 + 2 + 4 + 8  = 15
	//  1 + 2 + 5 + 7  = 15
	//  1 + 3 + 4 + 7  = 15
	//  1 + 3 + 5 + 6  = 15
	//  2 + 3 + 4 + 6  = 15
	//-----------------
	task.combination(15, 4)
}

Output

 1 2 3 9
 1 2 4 8
 1 2 5 7
 1 3 4 7
 1 3 5 6
 2 3 4 6
<?php
// Php Program 
// Generate all distinct combinations of N numbers whose sum is X
class Combinations
{
	// Display result
	public	function printSequence($result, $n)
	{
		for ($i = 0; $i < $n; ++$i)
		{
			echo(" ".$result[$i]);
		}
		echo("\n");
	}
	public	function findCombination($value, 
                                      $result, 
                                      $index, 
                                      $x, 
                                      $sum, 
                                      $n)
	{
		if ($index == $n && $x == $sum)
		{
			// Display calculated result
			$this->printSequence($result, $index);
			return;
		}
		if ($index >= $n || $sum > $x)
		{
			// Base case when stop process
			return;
		}
		for ($i = $value; $i < $x; ++$i)
		{
			// Collects resultant value
			$result[$index] = $i;
			// Find next combination
			$this->findCombination($i + 1, 
                                   $result, 
                                   $index + 1, 
                                   $x, 
                                   $sum + $i, 
                                   $n);
		}
	}
	// Handles the request to find combination
	public	function combination($x, $n)
	{
		if ($n <= 0)
		{
			return;
		}
		// Collect result
		$result = array_fill(0, $n, 0);
		// Test
		$this->findCombination(1, $result, 0, $x, 0, $n);
	}
}

function main()
{
	$task = new Combinations();
	// x = 15 sum
	// n = 4 length
	//-----------------
	//  1 + 2 + 3 + 9  = 15
	//  1 + 2 + 4 + 8  = 15
	//  1 + 2 + 5 + 7  = 15
	//  1 + 3 + 4 + 7  = 15
	//  1 + 3 + 5 + 6  = 15
	//  2 + 3 + 4 + 6  = 15
	//-----------------
	$task->combination(15, 4);
}
main();

Output

 1 2 3 9
 1 2 4 8
 1 2 5 7
 1 3 4 7
 1 3 5 6
 2 3 4 6
// Node JS Program 
// Generate all distinct combinations of N numbers whose sum is X
class Combinations
{
	// Display result
	printSequence(result, n)
	{
		for (var i = 0; i < n; ++i)
		{
			process.stdout.write(" " + result[i]);
		}
		process.stdout.write("\n");
	}
	findCombination(value, result, index, x, sum, n)
	{
		if (index == n && x == sum)
		{
			// Display calculated result
			this.printSequence(result, index);
			return;
		}
		if (index >= n || sum > x)
		{
			// Base case when stop process
			return;
		}
		for (var i = value; i < x; ++i)
		{
			// Collects resultant value
			result[index] = i;
			// Find next combination
			this.findCombination(i + 1, result, 
                                 index + 1, x, sum + i, n);
		}
	}
	// Handles the request to find combination
	combination(x, n)
	{
		if (n <= 0)
		{
			return;
		}
		// Collect result
		var result = Array(n).fill(0);
		// Test
		this.findCombination(1, result, 0, x, 0, n);
	}
}

function main()
{
	var task = new Combinations();
	// x = 15 sum
	// n = 4 length
	//-----------------
	//  1 + 2 + 3 + 9  = 15
	//  1 + 2 + 4 + 8  = 15
	//  1 + 2 + 5 + 7  = 15
	//  1 + 3 + 4 + 7  = 15
	//  1 + 3 + 5 + 6  = 15
	//  2 + 3 + 4 + 6  = 15
	//-----------------
	task.combination(15, 4);
}
main();

Output

 1 2 3 9
 1 2 4 8
 1 2 5 7
 1 3 4 7
 1 3 5 6
 2 3 4 6
#  Python 3 Program 
#  Generate all distinct combinations of N numbers whose sum is X
class Combinations :
	#  Display result
	def printSequence(self, result, n) :
		i = 0
		while (i < n) :
			print(" ", result[i], end = "")
			i += 1
		
		print(end = "\n")
	
	def findCombination(self, value, 
                        result, 
                        index, 
                        x, 
                        sum, 
                        n) :
		if (index == n and x == sum) :
			#  Display calculated result
			self.printSequence(result, index)
			return
		
		if (index >= n or sum > x) :
			#  Base case when stop process
			return
		
		i = value
		while (i < x) :
			#  Collects resultant value
			result[index] = i
			#  Find next combination
			self.findCombination(i + 1, 
                                 result, 
                                 index + 1, 
                                 x, 
                                 sum + i, 
                                 n)
			i += 1
		
	
	#  Handles the request to find combination
	def combination(self, x, n) :
		if (n <= 0) :
			return
		
		#  Collect result
		result = [0] * (n)
		#  Test
		self.findCombination(1, result, 0, x, 0, n)
	

def main() :
	task = Combinations()
	#  x = 15 sum
	#  n = 4 length
	# -----------------
	#   1 + 2 + 3 + 9  = 15
	#   1 + 2 + 4 + 8  = 15
	#   1 + 2 + 5 + 7  = 15
	#   1 + 3 + 4 + 7  = 15
	#   1 + 3 + 5 + 6  = 15
	#   2 + 3 + 4 + 6  = 15
	# -----------------
	task.combination(15, 4)

if __name__ == "__main__": main()

Output

  1  2  3  9
  1  2  4  8
  1  2  5  7
  1  3  4  7
  1  3  5  6
  2  3  4  6
#  Ruby Program 
#  Generate all distinct combinations of N numbers whose sum is X
class Combinations 
	#  Display result
	def printSequence(result, n) 
		i = 0
		while (i < n) 
			print(" ", result[i])
			i += 1
		end

		print("\n")
	end

	def findCombination(value, result, index, x, sum, n) 
		if (index == n && x == sum) 
			#  Display calculated result
			self.printSequence(result, index)
			return
		end

		if (index >= n || sum > x) 
			#  Base case when stop process
			return
		end

		i = value
		while (i < x) 
			#  Collects resultant value
			result[index] = i
			#  Find next combination
			self.findCombination(i + 1, result, 
                                 index + 1, x, 
                                 sum + i, n)
			i += 1
		end

	end

	#  Handles the request to find combination
	def combination(x, n) 
		if (n <= 0) 
			return
		end

		#  Collect result
		result = Array.new(n) {0}
		#  Test
		self.findCombination(1, result, 0, x, 0, n)
	end

end

def main() 
	task = Combinations.new()
	#  x = 15 sum
	#  n = 4 length
	# -----------------
	#   1 + 2 + 3 + 9  = 15
	#   1 + 2 + 4 + 8  = 15
	#   1 + 2 + 5 + 7  = 15
	#   1 + 3 + 4 + 7  = 15
	#   1 + 3 + 5 + 6  = 15
	#   2 + 3 + 4 + 6  = 15
	# -----------------
	task.combination(15, 4)
end

main()

Output

 1 2 3 9
 1 2 4 8
 1 2 5 7
 1 3 4 7
 1 3 5 6
 2 3 4 6
// Scala Program 
// Generate all distinct combinations of N numbers whose sum is X
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 findCombination(value: Int, result: Array[Int], 
      index: Int, x: Int, sum: Int, n: Int): Unit = {
		if (index == n && x == sum)
		{
			// Display calculated result
			printSequence(result, index);
			return;
		}
		if (index >= n || sum > x)
		{
			// Base case when stop process
			return;
		}
		var i: Int = value;
		while (i < x)
		{
			// Collects resultant value
			result(index) = i;
			// Find next combination
			findCombination(i + 1, result, 
                            index + 1, x, sum + i, n);
			i += 1;
		}
	}
	// Handles the request to find combination
	def combination(x: Int, n: Int): Unit = {
		if (n <= 0)
		{
			return;
		}
		// Collect result
		var result: Array[Int] = Array.fill[Int](n)(0);
		// Test
		findCombination(1, result, 0, x, 0, n);
	}
}
object Main
{
	def main(args: Array[String]): Unit = {
		var task: Combinations = new Combinations();
		// x = 15 sum
		// n = 4 length
		//-----------------
		//  1 + 2 + 3 + 9  = 15
		//  1 + 2 + 4 + 8  = 15
		//  1 + 2 + 5 + 7  = 15
		//  1 + 3 + 4 + 7  = 15
		//  1 + 3 + 5 + 6  = 15
		//  2 + 3 + 4 + 6  = 15
		//-----------------
		task.combination(15, 4);
	}
}

Output

 1 2 3 9
 1 2 4 8
 1 2 5 7
 1 3 4 7
 1 3 5 6
 2 3 4 6
// Swift 4 Program 
// Generate all distinct combinations of N numbers whose sum is X
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 findCombination(_ value: Int, 
                         _ result: inout[Int], 
      _ index: Int, 
        _ x: Int, 
          _ sum: Int,
            _ n: Int)
	{
		if (index == n && x == sum)
		{
			// Display calculated result
			self.printSequence(result, index);
			return;
		}
		if (index >= n || sum > x)
		{
			// Base case when stop process
			return;
		}
		var i: Int = value;
		while (i < x)
		{
			// Collects resultant value
			result[index] = i;
			// Find next combination
			self.findCombination(i + 1, &result, 
                                 index + 1, 
                                 x, 
                                 sum + i, 
                                 n);
			i += 1;
		}
	}
	// Handles the request to find combination
	func combination(_ x: Int, _ n: Int)
	{
		if (n <= 0)
		{
			return;
		}
		// Collect result
		var result: [Int] = Array(repeating: 0, count: n);
		// Test
		self.findCombination(1, &result, 0, x, 0, n);
	}
}
func main()
{
	let task: Combinations = Combinations();
	// x = 15 sum
	// n = 4 length
	//-----------------
	//  1 + 2 + 3 + 9  = 15
	//  1 + 2 + 4 + 8  = 15
	//  1 + 2 + 5 + 7  = 15
	//  1 + 3 + 4 + 7  = 15
	//  1 + 3 + 5 + 6  = 15
	//  2 + 3 + 4 + 6  = 15
	//-----------------
	task.combination(15, 4);
}
main();

Output

  1  2  3  9
  1  2  4  8
  1  2  5  7
  1  3  4  7
  1  3  5  6
  2  3  4  6
// Kotlin Program 
// Generate all distinct combinations of N numbers whose sum is X
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 findCombination(value: Int, 
                        result: Array < Int > , 
                        index: Int, x: Int, 
                        sum: Int, n: Int): Unit
	{
		if (index == n && x == sum)
		{
			// Display calculated result
			this.printSequence(result, index);
			return;
		}
		if (index >= n || sum > x)
		{
			// Base case when stop process
			return;
		}
		var i: Int = value;
		while (i < x)
		{
			// Collects resultant value
			result[index] = i;
			// Find next combination
			this.findCombination(i + 1, result, 
                                 index + 1, x, 
                                 sum + i, n);
			i += 1;
		}
	}
	// Handles the request to find combination
	fun combination(x: Int, n: Int): Unit
	{
		if (n <= 0)
		{
			return;
		}
		// Collect result
		val result: Array < Int > = Array(n)
		{
			0
		};
		// Test
		this.findCombination(1, result, 0, x, 0, n);
	}
}
fun main(args: Array < String > ): Unit
{
	val task: Combinations = Combinations();
	// x = 15 sum
	// n = 4 length
	//-----------------
	//  1 + 2 + 3 + 9  = 15
	//  1 + 2 + 4 + 8  = 15
	//  1 + 2 + 5 + 7  = 15
	//  1 + 3 + 4 + 7  = 15
	//  1 + 3 + 5 + 6  = 15
	//  2 + 3 + 4 + 6  = 15
	//-----------------
	task.combination(15, 4);
}

Output

 1 2 3 9
 1 2 4 8
 1 2 5 7
 1 3 4 7
 1 3 5 6
 2 3 4 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