Find sum of all subsets of a given array

Here given code implementation process.

/*
    C Program 
    Find sum of all subsets of a given array
*/
#include <stdio.h>

void printData(int arr[],int size)
{
    printf("\n Array elements \n");
    for (int i = 0; i < size; ++i)
    {
        printf(" %d",arr[i]);
    }
    printf("\n");
}
// Return sum of all subsets
int subsetSum(int arr[],int size, int i, int n, int sum)
{
   
    if(i==size)
    {
        // Here ( n -1) is indicates subset sum with different multiples
        return sum * (1 << (n-1));
    }

    // Get the subset sum
    int result = subsetSum(arr,size,i+1,n,sum);
    result    += subsetSum(arr,size,i+1,n+1, sum+arr[i]);

    // Return calculated result
    return result;
}
int main(int argc, char const *argv[])
{
    int arr1[] = { 1,2,3,4,5 }; 
    int arr2[] = { 1,4,5 }; 
    // Get the number of element in array
    int size = sizeof(arr1)/sizeof(arr1[0]); 

    printData(arr1,size);

    // Find sum of existing subset
    int sum = subsetSum(arr1,size,0,0,0); 
    printf(" Sumset Sum is : %d\n",sum); 


    size = sizeof(arr2)/sizeof(arr2[0]); 
    printData(arr2,size);
    // Find sum of existing subset
    sum = subsetSum(arr2,size,0,0,0); 
    printf(" Sumset Sum is : %d\n",sum); 

    return 0;
}

Output

 Array elements
 1 2 3 4 5
 Sumset Sum is : 1215

 Array elements
 1 4 5
 Sumset Sum is : 90
/*
  Java program
  Find sum of all subsets of a given array
*/
class Subsets
{
	// Display array elements
	public void printData(int[] arr, int size)
	{
		System.out.print("\n Array elements \n");
		for (int i = 0; i < size; ++i)
		{
			System.out.print(" " + arr[i]);
		}
		System.out.print("\n");
	}
	// Return sum of all subsets
	public int subsetSum(int[] arr, int size, int i, int n, int sum)
	{
		if (i == size)
		{
			// Here ( n -1) is indicates subset sum with different multiples
			return sum * (1 << (n - 1));
		}
		// Get the subset sum
		int result = subsetSum(arr, size, i + 1, n, sum);
		result += subsetSum(arr, size, i + 1, n + 1, sum + arr[i]);
		// Return calculated result
		return result;
	}
	public static void main(String[] args)
	{
		Subsets task = new Subsets();
		int[] arr1 = {
			1 , 2 , 3 , 4 , 5
		};
		int[] arr2 = {
			1 , 4 , 5
		};
		// Get the number of element in arr1
		int size = arr1.length;
		task.printData(arr1, size);
		// Find sum of existing subset
		int sum = task.subsetSum(arr1, size, 0, 0, 0);
		System.out.print(" Sumset Sum is : " + sum + "\n");
		// Get the size of arr2
		size = arr2.length;
		task.printData(arr2, size);
		// Find sum of existing subset
		sum = task.subsetSum(arr2, size, 0, 0, 0);
		System.out.print(" Sumset Sum is : " + sum + "\n");
	}
}

Output

 Array elements
 1 2 3 4 5
 Sumset Sum is : 1215

 Array elements
 1 4 5
 Sumset Sum is : 90
// Include header file
#include <iostream>

using namespace std;
/*
  C++ program
  Find sum of all subsets of a given array
*/
class Subsets
{
	public:
		// Display array elements
		void printData(int arr[], int size)
		{
			cout << "\n Array elements \n";
			for (int i = 0; i < size; ++i)
			{
				cout << " " << arr[i];
			}
			cout << "\n";
		}
	// Return sum of all subsets
	int subsetSum(int arr[], int size, int i, int n, int sum)
	{
		// Return calculated result
		if (i == size)
		{
			// Here ( n -1) is indicates subset sum with different multiples
			return sum *(1 << (n - 1));
		}
		// Get the subset sum
		int result = this->subsetSum(arr, size, i + 1, n, sum);
		result += this->subsetSum(arr, size, i + 1, n + 1, sum + arr[i]);
		return result;
	}
};
int main()
{
	Subsets task = Subsets();
	int arr1[] = {
		1 , 2 , 3 , 4 , 5
	};
	int arr2[] = {
		1 , 4 , 5
	};
	// Get the number of element in arr1
	int size = sizeof(arr1) / sizeof(arr1[0]);
	task.printData(arr1, size);
	// Find sum of existing subset
	int sum = task.subsetSum(arr1, size, 0, 0, 0);
	cout << " Sumset Sum is : " << sum << "\n";
	// Get the size of arr2
	size = sizeof(arr2) / sizeof(arr2[0]);
	task.printData(arr2, size);
	// Find sum of existing subset
	sum = task.subsetSum(arr2, size, 0, 0, 0);
	cout << " Sumset Sum is : " << sum << "\n";
	return 0;
}

Output

 Array elements
 1 2 3 4 5
 Sumset Sum is : 1215

 Array elements
 1 4 5
 Sumset Sum is : 90
// Include namespace system
using System;
/*
  C# program
  Find sum of all subsets of a given array
*/
public class Subsets
{
	// Display array elements
	public void printData(int[] arr, int size)
	{
		Console.Write("\n Array elements \n");
		for (int i = 0; i < size; ++i)
		{
			Console.Write(" " + arr[i]);
		}
		Console.Write("\n");
	}
	// Return sum of all subsets
	public int subsetSum(int[] arr, int size, int i, int n, int sum)
	{
		// Return calculated result
		if (i == size)
		{
			// Here ( n -1) is indicates subset sum with different multiples
			return sum * (1 << (n - 1));
		}
		// Get the subset sum
		int result = subsetSum(arr, size, i + 1, n, sum);
		result += subsetSum(arr, size, i + 1, n + 1, sum + arr[i]);
		return result;
	}
	public static void Main(String[] args)
	{
		Subsets task = new Subsets();
		int[] arr1 = {
			1 , 2 , 3 , 4 , 5
		};
		int[] arr2 = {
			1 , 4 , 5
		};
		// Get the number of element in arr1
		int size = arr1.Length;
		task.printData(arr1, size);
		// Find sum of existing subset
		int sum = task.subsetSum(arr1, size, 0, 0, 0);
		Console.Write(" Sumset Sum is : " + sum + "\n");
		// Get the size of arr2
		size = arr2.Length;
		task.printData(arr2, size);
		// Find sum of existing subset
		sum = task.subsetSum(arr2, size, 0, 0, 0);
		Console.Write(" Sumset Sum is : " + sum + "\n");
	}
}

Output

 Array elements
 1 2 3 4 5
 Sumset Sum is : 1215

 Array elements
 1 4 5
 Sumset Sum is : 90
<?php
/*
  Php program
  Find sum of all subsets of a given array
*/
class Subsets
{
	// Display array elements
	public	function printData( & $arr, $size)
	{
		echo "\n Array elements \n";
		for ($i = 0; $i < $size; ++$i)
		{
			echo " ". $arr[$i];
		}
		echo "\n";
	}
	// Return sum of all subsets
	public	function subsetSum( & $arr, $size, $i, $n, $sum)
	{
		// Return calculated result
		if ($i == $size)
		{
			// Here ( n -1) is indicates subset sum with different multiples
			return $sum * (1 << ($n - 1));
		}
		// Get the subset sum
		$result = $this->subsetSum($arr, $size, $i + 1, $n, $sum);
		$result += $this->subsetSum($arr, $size, $i + 1, $n + 1, $sum + $arr[$i]);
		return $result;
	}
}

function main()
{
	$task = new Subsets();
	$arr1 = array(1, 2, 3, 4, 5);
	$arr2 = array(1, 4, 5);
	// Get the number of element in arr1
	$size = count($arr1);
	$task->printData($arr1, $size);
	// Find sum of existing subset
	$sum = $task->subsetSum($arr1, $size, 0, 0, 0);
	echo " Sumset Sum is : ". $sum ."\n";
	// Get the size of arr2
	$size = count($arr2);
	$task->printData($arr2, $size);
	// Find sum of existing subset
	$sum = $task->subsetSum($arr2, $size, 0, 0, 0);
	echo " Sumset Sum is : ". $sum ."\n";
}
main();

Output

 Array elements
 1 2 3 4 5
 Sumset Sum is : 1215

 Array elements
 1 4 5
 Sumset Sum is : 90
/*
  Node Js program
  Find sum of all subsets of a given array
*/
class Subsets
{
	// Display array elements
	printData(arr, size)
	{
		process.stdout.write("\n Array elements \n");
		for (var i = 0; i < size; ++i)
		{
			process.stdout.write(" " + arr[i]);
		}
		process.stdout.write("\n");
	}
	// Return sum of all subsets
	subsetSum(arr, size, i, n, sum)
	{
		// Return calculated result
		if (i == size)
		{
			// Here ( n -1) is indicates subset sum with different multiples
			return sum * (1 << (n - 1));
		}
		// Get the subset sum
		var result = this.subsetSum(arr, size, i + 1, n, sum);
		result += this.subsetSum(arr, size, i + 1, n + 1, sum + arr[i]);
		return result;
	}
}

function main()
{
	var task = new Subsets();
	var arr1 = [1, 2, 3, 4, 5];
	var arr2 = [1, 4, 5];
	// Get the number of element in arr1
	var size = arr1.length;
	task.printData(arr1, size);
	// Find sum of existing subset
	var sum = task.subsetSum(arr1, size, 0, 0, 0);
	process.stdout.write(" Sumset Sum is : " + sum + "\n");
	// Get the size of arr2
	size = arr2.length;
	task.printData(arr2, size);
	// Find sum of existing subset
	sum = task.subsetSum(arr2, size, 0, 0, 0);
	process.stdout.write(" Sumset Sum is : " + sum + "\n");
}
main();

Output

 Array elements
 1 2 3 4 5
 Sumset Sum is : 1215

 Array elements
 1 4 5
 Sumset Sum is : 90
#   Python 3 program
#   Find sum of all subsets of a given array

class Subsets :
    #  Display array elements
    def printData(self, arr, size) :
        print("\n Array elements ")
        i = 0
        while (i < size) :
            print(" ", arr[i], end = "")
            i += 1
        
        print(end = "\n")
    
    #  Return sum of all subsets
    def subsetSum(self, arr, size, i, n, sum) :
        #  Return calculated result
        if (i == size) :
            #  Here ( n -1) is indicates subset sum with different multiples
            if(n > 1) :
                return sum * (1 << (n - 1))
            return sum
        
        #  Get the subset sum
        result = self.subsetSum(arr, size, i + 1, n, sum)
        result += self.subsetSum(arr, size, i + 1, n + 1, sum + arr[i])
        return result
    

def main() :
    task = Subsets()
    arr1 = [1, 2, 3, 4, 5]
    arr2 = [1, 4, 5]
    #  Get the number of element in arr1
    size = len(arr1)
    task.printData(arr1, size)
    #  Find sum of existing subset
    sum = task.subsetSum(arr1, size, 0, 0, 0)
    print(" Sumset Sum is : ", sum )
    #  Get the size of arr2
    size = len(arr2)
    task.printData(arr2, size)
    #  Find sum of existing subset
    sum = task.subsetSum(arr2, size, 0, 0, 0)
    print(" Sumset Sum is : ", sum )

if __name__ == "__main__": main()

Output

 Array elements
  1  2  3  4  5
 Sumset Sum is :  1215

 Array elements
  1  4  5
 Sumset Sum is :  90
#   Ruby program
#   Find sum of all subsets of a given array

class Subsets 
	#  Display array elements
	def printData(arr, size) 
		print("\n Array elements \n")
		i = 0
		while (i < size) 
			print(" ", arr[i])
			i += 1
		end

		print("\n")
	end

	#  Return sum of all subsets
	def subsetSum(arr, size, i, n, sum) 
		#  Return calculated result
		if (i == size) 
			#  Here ( n -1) is indicates subset sum with different multiples
			return sum * (1 << (n - 1))
		end

		#  Get the subset sum
		result = self.subsetSum(arr, size, i + 1, n, sum)
		result += self.subsetSum(arr, size, i + 1, n + 1, sum + arr[i])
		return result
	end

end

def main() 
	task = Subsets.new()
	arr1 = [1, 2, 3, 4, 5]
	arr2 = [1, 4, 5]
	#  Get the number of element in arr1
	size = arr1.length
	task.printData(arr1, size)
	#  Find sum of existing subset
	sum = task.subsetSum(arr1, size, 0, 0, 0)
	print(" Sumset Sum is : ", sum ,"\n")
	#  Get the size of arr2
	size = arr2.length
	task.printData(arr2, size)
	#  Find sum of existing subset
	sum = task.subsetSum(arr2, size, 0, 0, 0)
	print(" Sumset Sum is : ", sum ,"\n")
end

main()

Output

 Array elements 
 1 2 3 4 5
 Sumset Sum is : 1215

 Array elements 
 1 4 5
 Sumset Sum is : 90
/*
  Scala program
  Find sum of all subsets of a given array
*/
class Subsets
{
	// Display array elements
	def printData(arr: Array[Int], size: Int): Unit = {
		print("\n Array elements \n");
		var i: Int = 0;
		while (i < size)
		{
			print(" " + arr(i));
			i += 1;
		}
		print("\n");
	}
	// Return sum of all subsets
	def subsetSum(arr: Array[Int], size: Int, i: Int, n: Int, sum: Int): Int = {
		// Return calculated result
		if (i == size)
		{
			// Here ( n -1) is indicates subset sum with different multiples
			return sum * (1 << (n - 1));
		}
		// Get the subset sum
		var result: Int = this.subsetSum(arr, size, i + 1, n, sum);
		result += this.subsetSum(arr, size, i + 1, n + 1, sum + arr(i));
		return result;
	}
}
object Main
{
	def main(args: Array[String]): Unit = {
		var task: Subsets = new Subsets();
		var arr1: Array[Int] = Array(1, 2, 3, 4, 5);
		var arr2: Array[Int] = Array(1, 4, 5);
		// Get the number of element in arr1
		var size: Int = arr1.length;
		task.printData(arr1, size);
		// Find sum of existing subset
		var sum: Int = task.subsetSum(arr1, size, 0, 0, 0);
		print(" Sumset Sum is : " + sum + "\n");
		// Get the size of arr2
		size = arr2.length;
		task.printData(arr2, size);
		// Find sum of existing subset
		sum = task.subsetSum(arr2, size, 0, 0, 0);
		print(" Sumset Sum is : " + sum + "\n");
	}
}

Output

 Array elements
 1 2 3 4 5
 Sumset Sum is : 1215

 Array elements
 1 4 5
 Sumset Sum is : 90
/*
  Swift 4 program
  Find sum of all subsets of a given array
*/
class Subsets
{
	// Display array elements
	func printData(_ arr: [Int], _ size: Int)
	{
		print("\n Array elements ");
		var i: Int = 0;
		while (i < size)
		{
			print(" ", arr[i], terminator: "");
			i += 1;
		}
		print(terminator: "\n");
	}
	// Return sum of all subsets
	func subsetSum(_ arr: [Int], _ size: Int, _ i: Int, _ n: Int, _ sum: Int)->Int
	{
		// Return calculated result
		if (i == size)
		{
			// Here ( n -1) is indicates subset sum with different multiples
			return sum * (1 << (n - 1));
		}
		// Get the subset sum
		var result: Int = self.subsetSum(arr, size, i + 1, n, sum);
		result += self.subsetSum(arr, size, i + 1, n + 1, sum + arr[i]);
		return result;
	}
}
func main()
{
	let task: Subsets = Subsets();
	let arr1: [Int] = [1, 2, 3, 4, 5];
	let arr2: [Int] = [1, 4, 5];
	// Get the number of element in arr1
	var size: Int = arr1.count;
	task.printData(arr1, size);
	// Find sum of existing subset
	var sum: Int = task.subsetSum(arr1, size, 0, 0, 0);
	print(" Sumset Sum is : ", sum );
	// Get the size of arr2
	size = arr2.count;
	task.printData(arr2, size);
	// Find sum of existing subset
	sum = task.subsetSum(arr2, size, 0, 0, 0);
	print(" Sumset Sum is : ", sum );
}
main();

Output

 Array elements
  1  2  3  4  5
 Sumset Sum is :  1215

 Array elements
  1  4  5
 Sumset Sum is :  90
/*
  Kotlin program
  Find sum of all subsets of a given array
*/
class Subsets
{
	// Display array elements
	fun printData(arr: Array <Int> , size: Int): Unit
	{
		print("\n Array elements \n");
		var i: Int = 0;
		while (i < size)
		{
			print(" " + arr[i]);
			i += 1;
		}
		print("\n");
	}
	// Return sum of all subsets
	fun subsetSum(arr: Array <Int> , size: Int, i: Int, n: Int, sum: Int): Int
	{
		// Return calculated result
		if (i == size)
		{
			// Here ( n -1) is indicates subset sum with different multiples
			return sum * (1 shl (n - 1));
		}
		// Get the subset sum
		var result: Int = this.subsetSum(arr, size, i + 1, n, sum);
		result += this.subsetSum(arr, size, i + 1, n + 1, sum + arr[i]);
		return result;
	}
}
fun main(args: Array < String > ): Unit
{
	var task: Subsets = Subsets();
	var arr1: Array < Int > = arrayOf(1, 2, 3, 4, 5);
	var arr2: Array < Int > = arrayOf(1, 4, 5);
	// Get the number of element in arr1
	var size: Int = arr1.count();
	task.printData(arr1, size);
	// Find sum of existing subset
	var sum: Int = task.subsetSum(arr1, size, 0, 0, 0);
	print(" Sumset Sum is : " + sum + "\n");
	// Get the size of arr2
	size = arr2.count();
	task.printData(arr2, size);
	// Find sum of existing subset
	sum = task.subsetSum(arr2, size, 0, 0, 0);
	print(" Sumset Sum is : " + sum + "\n");
}

Output

 Array elements
 1 2 3 4 5
 Sumset Sum is : 1215

 Array elements
 1 4 5
 Sumset Sum is : 90


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