# Find all subsequences of N length whose sum is even

Here given code implementation process.

``````// C Program
// Find all subsequences of N length whose sum is even
#include <stdio.h>

void subSequences(int result[],
int start, int count,
int sum, int n)
{
if (count == n)
{
if (sum % 2 == 0)
{
// Display resultant sequence
printf("(");
for (int i = 0; i < count; ++i)
{
printf(" %d", result[i]);
}
printf(" ) = %d \n", sum);
}
}
else
{
for (int i = 1; i <= n; ++i)
{
// Collect elements
result[count] = i;
subSequences(result, i,
count + 1,
sum + i,
n);
}
}
}
void evenSumSequence(int n)
{
if (n <= 0)
{
return;
}
// Auxiliary array which is collect result
int result[n];
subSequences(result, 1, 0, 0, n);
}
int main()
{
// length and (1...n number)
int n = 3;
/*
( 1 1 2 ) = 4
( 1 2 1 ) = 4
( 1 2 3 ) = 6
( 1 3 2 ) = 6
( 2 1 1 ) = 4
( 2 1 3 ) = 6
( 2 2 2 ) = 6
( 2 3 1 ) = 6
( 2 3 3 ) = 8
( 3 1 2 ) = 6
( 3 2 1 ) = 6
( 3 2 3 ) = 8
( 3 3 2 ) = 8
*/
// Test
evenSumSequence(3);
return 0;
}``````

#### Output

``````( 1 1 2 ) = 4
( 1 2 1 ) = 4
( 1 2 3 ) = 6
( 1 3 2 ) = 6
( 2 1 1 ) = 4
( 2 1 3 ) = 6
( 2 2 2 ) = 6
( 2 3 1 ) = 6
( 2 3 3 ) = 8
( 3 1 2 ) = 6
( 3 2 1 ) = 6
( 3 2 3 ) = 8
( 3 3 2 ) = 8``````
``````// Java program for
// Find all subsequences of N length whose sum is even
public class Combination
{
public void subSequences(int[] result,
int start, int count,
int sum, int n)
{
if (count == n)
{
if (sum % 2 == 0)
{
// Display resultant sequence
System.out.print("(");
for (int i = 0; i < count; ++i)
{
System.out.print(" " + result[i]);
}
System.out.print(" ) = " + sum + " \n");
}
}
else
{
for (int i = 1; i <= n; ++i)
{
// Collect elements
result[count] = i;
subSequences(result, i,
count + 1,
sum + i,
n);
}
}
}
public void evenSumSequence(int n)
{
if (n <= 0)
{
return;
}
// Auxiliary array which is collect result
int[] result = new int[n];
subSequences(result, 1, 0, 0, n);
}
public static void main(String[] args)
{
Combination task = new Combination();
// length and (1...n number)
int n = 3;
/*
( 1 1 2 ) = 4
( 1 2 1 ) = 4
( 1 2 3 ) = 6
( 1 3 2 ) = 6
( 2 1 1 ) = 4
( 2 1 3 ) = 6
( 2 2 2 ) = 6
( 2 3 1 ) = 6
( 2 3 3 ) = 8
( 3 1 2 ) = 6
( 3 2 1 ) = 6
( 3 2 3 ) = 8
( 3 3 2 ) = 8
*/
// Test
task.evenSumSequence(3);
}
}``````

#### Output

``````( 1 1 2 ) = 4
( 1 2 1 ) = 4
( 1 2 3 ) = 6
( 1 3 2 ) = 6
( 2 1 1 ) = 4
( 2 1 3 ) = 6
( 2 2 2 ) = 6
( 2 3 1 ) = 6
( 2 3 3 ) = 8
( 3 1 2 ) = 6
( 3 2 1 ) = 6
( 3 2 3 ) = 8
( 3 3 2 ) = 8``````
``````// Include header file
#include <iostream>

using namespace std;
// C++ program for
// Find all subsequences of N length whose sum is even
class Combination
{
public: void subSequences(int result[], int start,
int count, int sum, int n)
{
if (count == n)
{
if (sum % 2 == 0)
{
// Display resultant sequence
cout << "(";
for (int i = 0; i < count; ++i)
{
cout << " " << result[i];
}
cout << " ) = " << sum << " \n";
}
}
else
{
for (int i = 1; i <= n; ++i)
{
// Collect elements
result[count] = i;
this->subSequences(result, i,
count + 1,
sum + i,
n);
}
}
}
void evenSumSequence(int n)
{
if (n <= 0)
{
return;
}
// Auxiliary array which is collect result
int result[n];
this->subSequences(result, 1, 0, 0, n);
}
};
int main()
{
Combination *task = new Combination();
// length and (1...n number)
int n = 3;
/*
( 1 1 2 ) = 4
( 1 2 1 ) = 4
( 1 2 3 ) = 6
( 1 3 2 ) = 6
( 2 1 1 ) = 4
( 2 1 3 ) = 6
( 2 2 2 ) = 6
( 2 3 1 ) = 6
( 2 3 3 ) = 8
( 3 1 2 ) = 6
( 3 2 1 ) = 6
( 3 2 3 ) = 8
( 3 3 2 ) = 8
*/
// Test
task->evenSumSequence(n);
return 0;
}``````

#### Output

``````( 1 1 2 ) = 4
( 1 2 1 ) = 4
( 1 2 3 ) = 6
( 1 3 2 ) = 6
( 2 1 1 ) = 4
( 2 1 3 ) = 6
( 2 2 2 ) = 6
( 2 3 1 ) = 6
( 2 3 3 ) = 8
( 3 1 2 ) = 6
( 3 2 1 ) = 6
( 3 2 3 ) = 8
( 3 3 2 ) = 8``````
``````// Include namespace system
using System;
// Csharp program for
// Find all subsequences of N length whose sum is even
public class Combination
{
public void subSequences(int[] result,
int start, int count,
int sum, int n)
{
if (count == n)
{
if (sum % 2 == 0)
{
// Display resultant sequence
Console.Write("(");
for (int i = 0; i < count; ++i)
{
Console.Write(" " + result[i]);
}
Console.Write(" ) = " + sum + " \n");
}
}
else
{
for (int i = 1; i <= n; ++i)
{
// Collect elements
result[count] = i;
this.subSequences(result, i,
count + 1,
sum + i,
n);
}
}
}
public void evenSumSequence(int n)
{
if (n <= 0)
{
return;
}
// Auxiliary array which is collect result
int[] result = new int[n];
this.subSequences(result, 1, 0, 0, n);
}
public static void Main(String[] args)
{
Combination task = new Combination();
// length and (1...n number)
int n = 3;
/*
( 1 1 2 ) = 4
( 1 2 1 ) = 4
( 1 2 3 ) = 6
( 1 3 2 ) = 6
( 2 1 1 ) = 4
( 2 1 3 ) = 6
( 2 2 2 ) = 6
( 2 3 1 ) = 6
( 2 3 3 ) = 8
( 3 1 2 ) = 6
( 3 2 1 ) = 6
( 3 2 3 ) = 8
( 3 3 2 ) = 8
*/
// Test
task.evenSumSequence(n);
}
}``````

#### Output

``````( 1 1 2 ) = 4
( 1 2 1 ) = 4
( 1 2 3 ) = 6
( 1 3 2 ) = 6
( 2 1 1 ) = 4
( 2 1 3 ) = 6
( 2 2 2 ) = 6
( 2 3 1 ) = 6
( 2 3 3 ) = 8
( 3 1 2 ) = 6
( 3 2 1 ) = 6
( 3 2 3 ) = 8
( 3 3 2 ) = 8``````
``````package main
import "fmt"
// Go program for
// Find all subsequences of N length whose sum is even
type Combination struct {}
func getCombination() * Combination {
var me *Combination = &Combination {}
return me
}
func(this Combination) subSequences(result[] int,
start int,
count int,
sum int,
n int) {
if count == n {
if sum % 2 == 0 {
// Display resultant sequence
fmt.Print("(")
for i := 0 ; i < count ; i++ {
fmt.Print(" ", result[i])
}
fmt.Print(" ) = ", sum, " \n")
}
} else {
for i := 1 ; i <= n ; i++ {
// Collect elements
result[count] = i
this.subSequences(result, i,
count + 1, sum + i, n)
}
}
}
func(this Combination) evenSumSequence(n int) {
if n <= 0 {
return
}
// Auxiliary array which is collect result
var result = make([] int, n)
this.subSequences(result, 1, 0, 0, n)
}
func main() {
var task * Combination = getCombination()
// length and (1...n number)
var n int = 3
/*
( 1 1 2 ) = 4
( 1 2 1 ) = 4
( 1 2 3 ) = 6
( 1 3 2 ) = 6
( 2 1 1 ) = 4
( 2 1 3 ) = 6
( 2 2 2 ) = 6
( 2 3 1 ) = 6
( 2 3 3 ) = 8
( 3 1 2 ) = 6
( 3 2 1 ) = 6
( 3 2 3 ) = 8
( 3 3 2 ) = 8
*/
// Test
task.evenSumSequence(n)
}``````

#### Output

``````( 1 1 2 ) = 4
( 1 2 1 ) = 4
( 1 2 3 ) = 6
( 1 3 2 ) = 6
( 2 1 1 ) = 4
( 2 1 3 ) = 6
( 2 2 2 ) = 6
( 2 3 1 ) = 6
( 2 3 3 ) = 8
( 3 1 2 ) = 6
( 3 2 1 ) = 6
( 3 2 3 ) = 8
( 3 3 2 ) = 8``````
``````<?php
// Php program for
// Find all subsequences of N length whose sum is even
class Combination
{
public	function subSequences(\$result,
\$start,
\$count,
\$sum,
\$n)
{
if (\$count == \$n)
{
if (\$sum % 2 == 0)
{
// Display resultant sequence
echo("(");
for (\$i = 0; \$i < \$count; ++\$i)
{
echo(" ".\$result[\$i]);
}
echo(" ) = ".\$sum.
" \n");
}
}
else
{
for (\$i = 1; \$i <= \$n; ++\$i)
{
// Collect elements
\$result[\$count] = \$i;
\$this->subSequences(\$result, \$i,
\$count + 1,
\$sum + \$i,
\$n);
}
}
}
public	function evenSumSequence(\$n)
{
if (\$n <= 0)
{
return;
}
// Auxiliary array which is collect result
\$result = array_fill(0, \$n, 0);
\$this->subSequences(\$result, 1, 0, 0, \$n);
}
}

function main()
{
\$task = new Combination();
// length and (1...n number)
\$n = 3;
/*
( 1 1 2 ) = 4
( 1 2 1 ) = 4
( 1 2 3 ) = 6
( 1 3 2 ) = 6
( 2 1 1 ) = 4
( 2 1 3 ) = 6
( 2 2 2 ) = 6
( 2 3 1 ) = 6
( 2 3 3 ) = 8
( 3 1 2 ) = 6
( 3 2 1 ) = 6
( 3 2 3 ) = 8
( 3 3 2 ) = 8
*/
// Test
\$task->evenSumSequence(\$n);
}
main();``````

#### Output

``````( 1 1 2 ) = 4
( 1 2 1 ) = 4
( 1 2 3 ) = 6
( 1 3 2 ) = 6
( 2 1 1 ) = 4
( 2 1 3 ) = 6
( 2 2 2 ) = 6
( 2 3 1 ) = 6
( 2 3 3 ) = 8
( 3 1 2 ) = 6
( 3 2 1 ) = 6
( 3 2 3 ) = 8
( 3 3 2 ) = 8``````
``````// Node JS program for
// Find all subsequences of N length whose sum is even
class Combination
{
subSequences(result, start, count, sum, n)
{
if (count == n)
{
if (sum % 2 == 0)
{
// Display resultant sequence
process.stdout.write("(");
for (var i = 0; i < count; ++i)
{
process.stdout.write(" " + result[i]);
}
process.stdout.write(" ) = " + sum + " \n");
}
}
else
{
for (var i = 1; i <= n; ++i)
{
// Collect elements
result[count] = i;
this.subSequences(result, i,
count + 1,
sum + i, n);
}
}
}
evenSumSequence(n)
{
if (n <= 0)
{
return;
}
// Auxiliary array which is collect result
var result = Array(n).fill(0);
this.subSequences(result, 1, 0, 0, n);
}
}

function main()
{
var task = new Combination();
// length and (1...n number)
var n = 3;
/*
( 1 1 2 ) = 4
( 1 2 1 ) = 4
( 1 2 3 ) = 6
( 1 3 2 ) = 6
( 2 1 1 ) = 4
( 2 1 3 ) = 6
( 2 2 2 ) = 6
( 2 3 1 ) = 6
( 2 3 3 ) = 8
( 3 1 2 ) = 6
( 3 2 1 ) = 6
( 3 2 3 ) = 8
( 3 3 2 ) = 8
*/
// Test
task.evenSumSequence(n);
}
main();``````

#### Output

``````( 1 1 2 ) = 4
( 1 2 1 ) = 4
( 1 2 3 ) = 6
( 1 3 2 ) = 6
( 2 1 1 ) = 4
( 2 1 3 ) = 6
( 2 2 2 ) = 6
( 2 3 1 ) = 6
( 2 3 3 ) = 8
( 3 1 2 ) = 6
( 3 2 1 ) = 6
( 3 2 3 ) = 8
( 3 3 2 ) = 8``````
``````#  Python 3 program for
#  Find all subsequences of N length whose sum is even
class Combination :
def subSequences(self, result, start, count, sum, n) :
if (count == n) :
if (sum % 2 == 0) :
#  Display resultant sequence
print("(", end = "")
i = 0
while (i < count) :
print("", result[i], end = "")
i += 1

print(" ) = ", sum ," ")

else :
i = 1
while (i <= n) :
#  Collect elements
result[count] = i
self.subSequences(result, i,
count + 1,
sum + i,
n)
i += 1

def evenSumSequence(self, n) :
if (n <= 0) :
return

#  Auxiliary list which is collect result
result = [0] * (n)
self.subSequences(result, 1, 0, 0, n)

def main() :
task = Combination()
#  length and (1...n number)
n = 3
#    ( 1 1 2 ) = 4
#    ( 1 2 1 ) = 4
#    ( 1 2 3 ) = 6
#    ( 1 3 2 ) = 6
#    ( 2 1 1 ) = 4
#    ( 2 1 3 ) = 6
#    ( 2 2 2 ) = 6
#    ( 2 3 1 ) = 6
#    ( 2 3 3 ) = 8
#    ( 3 1 2 ) = 6
#    ( 3 2 1 ) = 6
#    ( 3 2 3 ) = 8
#    ( 3 3 2 ) = 8
#  Test
task.evenSumSequence(n)

if __name__ == "__main__": main()``````

#### Output

``````( 1 1 2 ) =  4
( 1 2 1 ) =  4
( 1 2 3 ) =  6
( 1 3 2 ) =  6
( 2 1 1 ) =  4
( 2 1 3 ) =  6
( 2 2 2 ) =  6
( 2 3 1 ) =  6
( 2 3 3 ) =  8
( 3 1 2 ) =  6
( 3 2 1 ) =  6
( 3 2 3 ) =  8
( 3 3 2 ) =  8``````
``````#  Ruby program for
#  Find all subsequences of N length whose sum is even
class Combination
def subSequences(result, start, count, sum, n)
if (count == n)
if (sum % 2 == 0)
#  Display resultant sequence
print("(")
i = 0
while (i < count)
print(" ", result[i])
i += 1
end

print(" ) = ", sum ," \n")
end

else

i = 1
while (i <= n)
#  Collect elements
result[count] = i
self.subSequences(result, i,
count + 1,
sum + i, n)
i += 1
end

end

end

def evenSumSequence(n)
if (n <= 0)
return
end

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

end

def main()
task = Combination.new()
#  length and (1...n number)
n = 3
#    ( 1 1 2 ) = 4
#    ( 1 2 1 ) = 4
#    ( 1 2 3 ) = 6
#    ( 1 3 2 ) = 6
#    ( 2 1 1 ) = 4
#    ( 2 1 3 ) = 6
#    ( 2 2 2 ) = 6
#    ( 2 3 1 ) = 6
#    ( 2 3 3 ) = 8
#    ( 3 1 2 ) = 6
#    ( 3 2 1 ) = 6
#    ( 3 2 3 ) = 8
#    ( 3 3 2 ) = 8
#  Test
task.evenSumSequence(n)
end

main()``````

#### Output

``````( 1 1 2 ) = 4
( 1 2 1 ) = 4
( 1 2 3 ) = 6
( 1 3 2 ) = 6
( 2 1 1 ) = 4
( 2 1 3 ) = 6
( 2 2 2 ) = 6
( 2 3 1 ) = 6
( 2 3 3 ) = 8
( 3 1 2 ) = 6
( 3 2 1 ) = 6
( 3 2 3 ) = 8
( 3 3 2 ) = 8
``````
``````// Scala program for
// Find all subsequences of N length whose sum is even
class Combination()
{
def subSequences(result: Array[Int],
start: Int, count: Int,
sum: Int, n: Int): Unit = {
if (count == n)
{
if (sum % 2 == 0)
{
// Display resultant sequence
print("(");
var i: Int = 0;
while (i < count)
{
print(" " + result(i));
i += 1;
}
print(" ) = " + sum + " \n");
}
}
else
{
var i: Int = 1;
while (i <= n)
{
// Collect elements
result(count) = i;
subSequences(result, i,
count + 1,
sum + i,
n);
i += 1;
}
}
}
def evenSumSequence(n: Int): Unit = {
if (n <= 0)
{
return;
}
// Auxiliary array which is collect result
var result: Array[Int] = Array.fill[Int](n)(0);
subSequences(result, 1, 0, 0, n);
}
}
object Main
{
def main(args: Array[String]): Unit = {
var task: Combination = new Combination();
// length and (1...n number)
var n: Int = 3;
/*
( 1 1 2 ) = 4
( 1 2 1 ) = 4
( 1 2 3 ) = 6
( 1 3 2 ) = 6
( 2 1 1 ) = 4
( 2 1 3 ) = 6
( 2 2 2 ) = 6
( 2 3 1 ) = 6
( 2 3 3 ) = 8
( 3 1 2 ) = 6
( 3 2 1 ) = 6
( 3 2 3 ) = 8
( 3 3 2 ) = 8
*/
// Test
task.evenSumSequence(n);
}
}``````

#### Output

``````( 1 1 2 ) = 4
( 1 2 1 ) = 4
( 1 2 3 ) = 6
( 1 3 2 ) = 6
( 2 1 1 ) = 4
( 2 1 3 ) = 6
( 2 2 2 ) = 6
( 2 3 1 ) = 6
( 2 3 3 ) = 8
( 3 1 2 ) = 6
( 3 2 1 ) = 6
( 3 2 3 ) = 8
( 3 3 2 ) = 8``````
``````// Swift 4 program for
// Find all subsequences of N length whose sum is even
class Combination
{
func subSequences(
_ result: inout[Int],
_ start: Int,
_ count: Int,
_ sum: Int,
_ n: Int)
{
if (count == n)
{
if (sum % 2 == 0)
{
// Display resultant sequence
print("(", terminator: "");
var i: Int = 0;
while (i < count)
{
print(" ", result[i], terminator: "");
i += 1;
}
print(" ) = ", sum ," ");
}
}
else
{
var i: Int = 1;
while (i <= n)
{
// Collect elements
result[count] = i;
self.subSequences(&result, i,
count + 1,
sum + i, n);
i += 1;
}
}
}
func evenSumSequence(_ n: Int)
{
if (n <= 0)
{
return;
}
// Auxiliary array which is collect result
var result: [Int] = Array(repeating: 0, count: n);
self.subSequences(&result, 1, 0, 0, n);
}
}
func main()
{
let task: Combination = Combination();
// length and (1...n number)
let n: Int = 3;
/*
( 1 1 2 ) = 4
( 1 2 1 ) = 4
( 1 2 3 ) = 6
( 1 3 2 ) = 6
( 2 1 1 ) = 4
( 2 1 3 ) = 6
( 2 2 2 ) = 6
( 2 3 1 ) = 6
( 2 3 3 ) = 8
( 3 1 2 ) = 6
( 3 2 1 ) = 6
( 3 2 3 ) = 8
( 3 3 2 ) = 8
*/
// Test
task.evenSumSequence(n);
}
main();``````

#### Output

``````(  1  1  2 ) =  4
(  1  2  1 ) =  4
(  1  2  3 ) =  6
(  1  3  2 ) =  6
(  2  1  1 ) =  4
(  2  1  3 ) =  6
(  2  2  2 ) =  6
(  2  3  1 ) =  6
(  2  3  3 ) =  8
(  3  1  2 ) =  6
(  3  2  1 ) =  6
(  3  2  3 ) =  8
(  3  3  2 ) =  8``````
``````// Kotlin program for
// Find all subsequences of N length whose sum is even
class Combination
{
fun subSequences(result: Array < Int > ,
count: Int,
sum: Int, n: Int): Unit
{
if (count == n)
{
if (sum % 2 == 0)
{
// Display resultant sequence
print("(");
var i: Int = 0;
while (i < count)
{
print(" " + result[i]);
i += 1;
}
print(" ) = " + sum + " \n");
}
}
else
{
var i: Int = 1;
while (i <= n)
{
// Collect elements
result[count] = i;
this.subSequences(result,
count + 1, sum + i, n);
i += 1;
}
}
}
fun evenSumSequence(n: Int): Unit
{
if (n <= 0)
{
return;
}
// Auxiliary array which is collect result
var result: Array < Int > = Array(n)
{
0
};
this.subSequences(result, 0, 0, n);
}
}
fun main(args: Array < String > ): Unit
{
val task: Combination = Combination();
// length and (1...n number)
val n: Int = 3;
/*
( 1 1 2 ) = 4
( 1 2 1 ) = 4
( 1 2 3 ) = 6
( 1 3 2 ) = 6
( 2 1 1 ) = 4
( 2 1 3 ) = 6
( 2 2 2 ) = 6
( 2 3 1 ) = 6
( 2 3 3 ) = 8
( 3 1 2 ) = 6
( 3 2 1 ) = 6
( 3 2 3 ) = 8
( 3 3 2 ) = 8
*/
// Test
task.evenSumSequence(n);
}``````

#### Output

``````( 1 1 2 ) = 4
( 1 2 1 ) = 4
( 1 2 3 ) = 6
( 1 3 2 ) = 6
( 2 1 1 ) = 4
( 2 1 3 ) = 6
( 2 2 2 ) = 6
( 2 3 1 ) = 6
( 2 3 3 ) = 8
( 3 1 2 ) = 6
( 3 2 1 ) = 6
( 3 2 3 ) = 8
( 3 3 2 ) = 8``````

## 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

© 2022, kalkicode.com, All rights reserved