Print all subsequences of x whose sum is combination of even numbers

Here given code implementation process.

// C Program
// Print all subsequences of x whose sum is combination of even numbers
#include <stdio.h>

void subSequences(int result[], int start, int index, int x, int sum)
{
	if (sum == x)
	{
		// Display resultant sequence
		for (int i = 0; i < index; ++i)
		{
			printf("  %d", result[i]);
		}
		printf("\n");
	}
	else if (index >= x || sum > x)
	{
		return;
	}
	else
	{
		for (int i = start; i <= x; i += 2)
		{
			// Collect elements 
			result[index] = i;
			subSequences(result, start, 
                         index + 1, x, sum + i);
		}
	}
}
void evenSumX(int x)
{
	if (x <= 0 || x % 2 != 0)
	{
		return;
	}
	// Auxiliary array which is collect result
	int result[x];
	subSequences(result, 2, 0, x, 0);
}
int main()
{
	int x = 8;
	/*
	    x = 8
	--------------------
	    2  2  2  2
	    2  2  4
	    2  4  2
	    2  6
	    4  2  2
	    4  4
	    6  2
	    8
	---------------
	   Possible subsequences
	*/
	evenSumX(x);
	return 0;
}

Output

  2  2  2  2
  2  2  4
  2  4  2
  2  6
  4  2  2
  4  4
  6  2
  8
// Java program for
// Print all subsequences of x whose 
// Sum is combination of even numbers
public class Combination
{
    public void subSequences(int[] result, 
  			int start, 
  			int index, 
  			int x, 
  			int sum)
    {
        if (sum == x)
        {
            // Display resultant sequence
            for (int i = 0; i < index; ++i)
            {
                System.out.print(" " + result[i] );
            }
            System.out.print("\n");
        }
        else if (index >= x || sum > x)
        {
            return;
        }
        else
        {
            for (int i = start; i <= x; i += 2)
            {
                // Collect elements 
                result[index] = i;
                subSequences(result, 
                             start, 
                             index + 1, 
                             x, 
                             sum + i);
            }
        }
    }
    public void evenSumX(int x)
    {
        if (x <= 0 || x % 2 != 0)
        {
            return;
        }
        // Auxiliary array which is collect result
        int[] result = new int[x];
        subSequences(result, 2, 0, x, 0);
    }
    public static void main(String[] args)
    {
        Combination task = new Combination();
        int x = 8;
        /*
            x = 8
          --------------------
            2  2  2  2
            2  2  4
            2  4  2
            2  6
            4  2  2
            4  4
            6  2
            8
          ---------------
           Possible subsequences
        */
        task.evenSumX(x);
    }
}

Output

 2 2 2 2
 2 2 4
 2 4 2
 2 6
 4 2 2
 4 4
 6 2
 8
// Include header file
#include <iostream>

using namespace std;
// C++ program for
// Print all subsequences of x whose 
// Sum is combination of even numbers
class Combination
{
	public: void subSequences(int result[], 
      int start, 
        int index, 
          int x, 
            int sum)
	{
		if (sum == x)
		{
			// Display resultant sequence
			for (int i = 0; i < index; ++i)
			{
				cout << " " << result[i];
			}
			cout << "\n";
		}
		else if (index >= x || sum > x)
		{
			return;
		}
		else
		{
			for (int i = start; i <= x; i += 2)
			{
				// Collect elements 
				result[index] = i;
				this->subSequences(result, 
                                   start, 
                                   index + 1, 
                                   x, 
                                   sum + i);
			}
		}
	}
	void evenSumX(int x)
	{
		if (x <= 0 || x % 2 != 0)
		{
			return;
		}
		// Auxiliary array which is collect result
		int result[x];
		this->subSequences(result, 2, 0, x, 0);
	}
};
int main()
{
	Combination *task = new Combination();
	int x = 8;
	/*
	            x = 8
	          --------------------
	            2  2  2  2
	            2  2  4
	            2  4  2
	            2  6
	            4  2  2
	            4  4
	            6  2
	            8
	          ---------------
	           Possible subsequences
	        */
	task->evenSumX(x);
	return 0;
}

Output

 2 2 2 2
 2 2 4
 2 4 2
 2 6
 4 2 2
 4 4
 6 2
 8
// Include namespace system
using System;
// Csharp program for
// Print all subsequences of x whose 
// Sum is combination of even numbers
public class Combination
{
    public void subSequences(int[] result, 
    int start, 
    int index, 
    int x, 
    int sum)
    {
        if (sum == x)
        {
            // Display resultant sequence
            for (int i = 0; i < index; ++i)
            {
                Console.Write(" " + result[i]);
            }
            Console.Write("\n");
        }
        else if (index >= x || sum > x)
        {
            return;
        }
        else
        {
            for (int i = start; i <= x; i += 2)
            {
                // Collect elements 
                result[index] = i;
                this.subSequences(result, 
                                  start, 
                                  index + 1, 
                                  x, 
                                  sum + i);
            }
        }
    }
    public void evenSumX(int x)
    {
        if (x <= 0 || x % 2 != 0)
        {
            return;
        }
        // Auxiliary array which is collect result
        int[] result = new int[x];
        this.subSequences(result, 2, 0, x, 0);
    }
    public static void Main(String[] args)
    {
        Combination task = new Combination();
        int x = 8;
        /*
            x = 8
          --------------------
            2  2  2  2
            2  2  4
            2  4  2
            2  6
            4  2  2
            4  4
            6  2
            8
          ---------------
           Possible subsequences
        */
        task.evenSumX(x);
    }
}

Output

 2 2 2 2
 2 2 4
 2 4 2
 2 6
 4 2 2
 4 4
 6 2
 8
package main
import "fmt"
// Go program for
// Print all subsequences of x whose
// Sum is combination of even numbers
type Combination struct {}
func getCombination() * Combination {
	var me *Combination = &Combination {}
	return me
}
func(this Combination) subSequences(result[] int, 
	start int, 
	index int, 
	x int, 
	sum int) {
	if sum == x {
		// Display resultant sequence
		for i := 0 ; i < index ; i++ {
			fmt.Print(" ", result[i])
		}
		fmt.Print("\n")
	} else if index >= x || sum > x {
		return
	} else {
		for i := start ; i <= x ; i += 2 {
			// Collect elements
			result[index] = i
			this.subSequences(result, 
				start, 
				index + 1, 
				x, 
				sum + i)
		}
	}
}
func(this Combination) evenSumX(x int) {
	if x <= 0 || x % 2 != 0 {
		return
	}
	// Auxiliary array which is collect result
	var result = make([] int, x)
	this.subSequences(result, 2, 0, x, 0)
}
func main() {
	var task * Combination = getCombination()
	var x int = 8
	/*
	    x = 8
	  --------------------
	    2  2  2  2
	    2  2  4
	    2  4  2
	    2  6
	    4  2  2
	    4  4
	    6  2
	    8
	  ---------------
	   Possible subsequences
	*/
	task.evenSumX(x)
}

Output

 2 2 2 2
 2 2 4
 2 4 2
 2 6
 4 2 2
 4 4
 6 2
 8
<?php
// Php program for
// Print all subsequences of x whose
// Sum is combination of even numbers
class Combination
{
	public	function subSequences($result, 
                                   $start, 
                                   $index, 
                                   $x, 
                                   $sum)
	{
		if ($sum == $x)
		{
			// Display resultant sequence
			for ($i = 0; $i < $index; ++$i)
			{
				echo(" ".$result[$i]);
			}
			echo("\n");
		}
		else if ($index >= $x || $sum > $x)
		{
			return;
		}
		else
		{
			for ($i = $start; $i <= $x; $i += 2)
			{
				// Collect elements
				$result[$index] = $i;
				$this->subSequences($result, 
                                    $start, 
                                    $index + 1, 
                                    $x, 
                                    $sum + $i);
			}
		}
	}
	public	function evenSumX($x)
	{
		if ($x <= 0 || $x % 2 != 0)
		{
			return;
		}
		// Auxiliary array which is collect result
		$result = array_fill(0, $x, 0);
		$this->subSequences($result, 2, 0, $x, 0);
	}
}

function main()
{
	$task = new Combination();
	$x = 8;
	/*
	    x = 8
	  --------------------
	    2  2  2  2
	    2  2  4
	    2  4  2
	    2  6
	    4  2  2
	    4  4
	    6  2
	    8
	  ---------------
	   Possible subsequences
	*/
	$task->evenSumX($x);
}
main();

Output

 2 2 2 2
 2 2 4
 2 4 2
 2 6
 4 2 2
 4 4
 6 2
 8
// Node JS program for
// Print all subsequences of x whose
// Sum is combination of even numbers
class Combination
{
	subSequences(result, start, index, x, sum)
	{
		if (sum == x)
		{
			// Display resultant sequence
			for (var i = 0; i < index; ++i)
			{
				process.stdout.write(" " + result[i]);
			}
			process.stdout.write("\n");
		}
		else if (index >= x || sum > x)
		{
			return;
		}
		else
		{
			for (var i = start; i <= x; i += 2)
			{
				// Collect elements
				result[index] = i;
				this.subSequences(result, 
                                  start, 
                                  index + 1, 
                                  x, 
                                  sum + i);
			}
		}
	}
	evenSumX(x)
	{
		if (x <= 0 || x % 2 != 0)
		{
			return;
		}
		// Auxiliary array which is collect result
		var result = Array(x).fill(0);
		this.subSequences(result, 2, 0, x, 0);
	}
}

function main()
{
	var task = new Combination();
	var x = 8;
	/*
	    x = 8
	  --------------------
	    2  2  2  2
	    2  2  4
	    2  4  2
	    2  6
	    4  2  2
	    4  4
	    6  2
	    8
	  ---------------
	   Possible subsequences
	*/
	task.evenSumX(x);
}
main();

Output

 2 2 2 2
 2 2 4
 2 4 2
 2 6
 4 2 2
 4 4
 6 2
 8
#  Python 3 program for
#  Print all subsequences of x whose
#  Sum is combination of even numbers
class Combination :
	def subSequences(self, result, start, index, x, sum) :
		if (sum == x) :
			i = 0
			#  Display resultant sequence
			while (i < index) :
				print(" ", result[i], end = "")
				i += 1
			
			print(end = "\n")
		elif (index >= x or sum > x) :
			return
		else :
			i = start
			while (i <= x) :
				#  Collect elements
				result[index] = i
				self.subSequences(result, 
                                  start, 
                                  index + 1, 
                                  x, 
                                  sum + i)
				i += 2
			
		
	
	def evenSumX(self, x) :
		if (x <= 0 or x % 2 != 0) :
			return
		
		#  Auxiliary list which is collect result
		result = [0] * (x)
		self.subSequences(result, 2, 0, x, 0)
	

def main() :
	task = Combination()
	x = 8
	#    x = 8
	#  --------------------
	#    2  2  2  2
	#    2  2  4
	#    2  4  2
	#    2  6
	#    4  2  2
	#    4  4
	#    6  2
	#    8
	#  ---------------
	#   Possible subsequences
	task.evenSumX(x)

if __name__ == "__main__": main()

Output

  2  2  2  2
  2  2  4
  2  4  2
  2  6
  4  2  2
  4  4
  6  2
  8
#  Ruby program for
#  Print all subsequences of x whose
#  Sum is combination of even numbers
class Combination 
	def subSequences(result, start, index, x, sum) 
		if (sum == x) 
			i = 0
			#  Display resultant sequence
			while (i < index) 
				print(" ", result[i])
				i += 1
			end

			print("\n")
		elsif (index >= x || sum > x) 
			return
		else
 
			i = start
			while (i <= x) 
				#  Collect elements
				result[index] = i
				self.subSequences(result, 
                                  start, 
                                  index + 1, 
                                  x, 
                                  sum + i)
				i += 2
			end

		end

	end

	def evenSumX(x) 
		if (x <= 0 || x % 2 != 0) 
			return
		end

		#  Auxiliary array which is collect result
		result = Array.new(x) {0}
		self.subSequences(result, 2, 0, x, 0)
	end

end

def main() 
	task = Combination.new()
	x = 8
	#    x = 8
	#  --------------------
	#    2  2  2  2
	#    2  2  4
	#    2  4  2
	#    2  6
	#    4  2  2
	#    4  4
	#    6  2
	#    8
	#  ---------------
	#   Possible subsequences
	task.evenSumX(x)
end

main()

Output

 2 2 2 2
 2 2 4
 2 4 2
 2 6
 4 2 2
 4 4
 6 2
 8
// Scala program for
// Print all subsequences of x whose
// Sum is combination of even numbers
class Combination()
{
	def subSequences(result: Array[Int], 
      start: Int, 
        index: Int, 
          x: Int, 
            sum: Int): Unit = {
		if (sum == x)
		{
			var i: Int = 0;
			// Display resultant sequence
			while (i < index)
			{
				print(" " + result(i));
				i += 1;
			}
			print("\n");
		}
		else if (index >= x || sum > x)
		{
			return;
		}
		else
		{
			var i: Int = start;
			while (i <= x)
			{
				// Collect elements
				result(index) = i;
				subSequences(result, 
                             start, 
                             index + 1, 
                             x, 
                             sum + i);
				i += 2;
			}
		}
	}
	def evenSumX(x: Int): Unit = {
		if (x <= 0 || x % 2 != 0)
		{
			return;
		}
		// Auxiliary array which is collect result
		var result: Array[Int] = Array.fill[Int](x)(0);
		subSequences(result, 2, 0, x, 0);
	}
}
object Main
{
	def main(args: Array[String]): Unit = {
		var task: Combination = new Combination();
		var x: Int = 8;
		/*
		    x = 8
		  --------------------
		    2  2  2  2
		    2  2  4
		    2  4  2
		    2  6
		    4  2  2
		    4  4
		    6  2
		    8
		  ---------------
		   Possible subsequences
		*/
		task.evenSumX(x);
	}
}

Output

 2 2 2 2
 2 2 4
 2 4 2
 2 6
 4 2 2
 4 4
 6 2
 8
// Swift 4 program for
// Print all subsequences of x whose
// Sum is combination of even numbers
class Combination
{
	func subSequences(_ result: inout[Int], 
  	_ start: Int, 
    _ index: Int, 
      _ x: Int, 
        _ sum: Int)
	{
		if (sum == x)
		{
			var i: Int = 0;
			// Display resultant sequence
			while (i < index)
			{
				print(" ", result[i], terminator: "");
				i += 1;
			}
			print(terminator: "\n");
		}
		else if (index >= x || sum > x)
		{
			return;
		}
		else
		{
			var i: Int = start;
			while (i <= x)
			{
				// Collect elements
				result[index] = i;
				self.subSequences(&result, 
                                  start, 
                                  index + 1, 
                                  x, 
                                  sum + i);
				i += 2;
			}
		}
	}
	func evenSumX(_ x: Int)
	{
		if (x <= 0 || x % 2  != 0)
		{
			return;
		}
		// Auxiliary array which is collect result
		var result: [Int] = Array(repeating: 0, count: x);
		self.subSequences(&result, 2, 0, x, 0);
	}
}
func main()
{
	let task: Combination = Combination();
	let x: Int = 8;
	/*
	    x = 8
	  --------------------
	    2  2  2  2
	    2  2  4
	    2  4  2
	    2  6
	    4  2  2
	    4  4
	    6  2
	    8
	  ---------------
	   Possible subsequences
	*/
	task.evenSumX(x);
}
main();

Output

  2  2  2  2
  2  2  4
  2  4  2
  2  6
  4  2  2
  4  4
  6  2
  8
// Kotlin program for
// Print all subsequences of x whose
// Sum is combination of even numbers
class Combination
{
	fun subSequences(result: Array < Int > , 
                      start: Int, 
                      index: Int, 
                      x: Int, 
                      sum: Int): Unit
	{
		if (sum == x)
		{
			var i: Int = 0;
			// Display resultant sequence
			while (i < index)
			{
				print(" " + result[i]);
				i += 1;
			}
			print("\n");
		}
		else if (index >= x || sum > x)
		{
			return;
		}
		else
		{
			var i: Int = start;
			while (i <= x)
			{
				// Collect elements
				result[index] = i;
				this.subSequences(result, 
                                  start, 
                                  index + 1, 
                                  x, 
                                  sum + i);
				i += 2;
			}
		}
	}
	fun evenSumX(x: Int): Unit
	{
		if (x <= 0 || x % 2 != 0)
		{
			return;
		}
		// Auxiliary array which is collect result
		val result: Array < Int > = Array(x)
		{
			0
		};
		this.subSequences(result, 2, 0, x, 0);
	}
}
fun main(args: Array < String > ): Unit
{
	val task: Combination = Combination();
	val x: Int = 8;
	/*
	    x = 8
	  --------------------
	    2  2  2  2
	    2  2  4
	    2  4  2
	    2  6
	    4  2  2
	    4  4
	    6  2
	    8
	  ---------------
	   Possible subsequences
	*/
	task.evenSumX(x);
}

Output

 2 2 2 2
 2 2 4
 2 4 2
 2 6
 4 2 2
 4 4
 6 2
 8


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







© 2021, kalkicode.com, All rights reserved