Sum of all subarrays of an array efficiently

Here given code implementation process.

// C program for
// Sum of all subarrays of an array efficiently
#include <stdio.h>

void sumSubArray(int arr[], int n)
{
	if (n <= 0)
	{
		return;
	}
	// Sum of occurrence of first element
	int sum = arr[0] *n;
	// Calculate occurrence in remaining elements
	for (int i = 1; i < n; ++i)
	{
		// Here 
		// (n-i) Indicate number of element in current to last element
		// (i + 1) Indicate number of elements between start (0) to current 
		sum += (arr[i]) * ((n - i) * (i + 1));
	}
	// Display calculated result
	printf("\n Sum %d", sum);
}
int main(int argc, char
	const *argv[])
{
	int arr[] = {
		8 , 1 , 4 , 3 , 2 , 1
	};
	/*
	  arr = [8, 1, 4, 3, 2, 1]
	  ---------------------------
	  8
	  8 + 1
	  8 + 1 + 4
	  8 + 1 + 4 + 3
	  8 + 1 + 4 + 3 + 2
	  8 + 1 + 4 + 3 + 2 + 1
	  1
	  1 + 4
	  1 + 4 + 3
	  1 + 4 + 3 + 2
	  1 + 4 + 3 + 2 + 1
	  4
	  4 + 3
	  4 + 3 + 2
	  4 + 3 + 2 + 1
	  3
	  3 + 2
	  3 + 2 + 1
	  2
	  2 + 1
	  1
	  --------------------
	  Sum of all 168
	*/
	int n = sizeof(arr) / sizeof(arr[0]);
	sumSubArray(arr, n);
	return 0;
}

Output

 Sum 168
/*
    Java Program
    Sum of all subarrays of an array efficiently
*/
public class SubArray
{
	public void sumSubArray(int[] arr, int n)
	{
		if (n <= 0)
		{
			return;
		}
		// Sum of occurrence of first element
		int sum = arr[0] * n;
		// Calculate occurrence in remaining elements
		for (int i = 1; i < n; ++i)
		{
			// Here 
          	// -----------------------------------
			// (n-i) Indicate number of element in 
			// current to last element.
			// (i + 1) Indicate number of elements 
			// between start (0) to current.
			sum += (arr[i]) * ((n - i) * (i + 1));
		}
		// Display calculated result
		System.out.print("\n Sum " + sum);
	}
	public static void main(String[] args)
	{
		SubArray task = new SubArray();
		int[] arr = {
			8 , 1 , 4 , 3 , 2 , 1
		};
		/*
		  arr = [8, 1, 4, 3, 2, 1]
		  ---------------------------
		  8
		  8 + 1
		  8 + 1 + 4
		  8 + 1 + 4 + 3
		  8 + 1 + 4 + 3 + 2
		  8 + 1 + 4 + 3 + 2 + 1
		  1
		  1 + 4
		  1 + 4 + 3
		  1 + 4 + 3 + 2
		  1 + 4 + 3 + 2 + 1
		  4
		  4 + 3
		  4 + 3 + 2
		  4 + 3 + 2 + 1
		  3
		  3 + 2
		  3 + 2 + 1
		  2
		  2 + 1
		  1
		  --------------------
		  Sum of all 168
		*/
		int n = arr.length;
		task.sumSubArray(arr, n);
	}
}

Output

 Sum 168
// Include header file
#include <iostream>

using namespace std;
/*
    C++ Program
    Sum of all subarrays of an array efficiently
*/
class SubArray
{
	public: void sumSubArray(int arr[], int n)
	{
		if (n <= 0)
		{
			return;
		}
		// Sum of occurrence of first element
		int sum = arr[0] *n;
		// Calculate occurrence in remaining elements
		for (int i = 1; i < n; ++i)
		{
			// Here
			// -----------------------------------
			// (n-i) Indicate number of element in
			// current to last element.
			// (i + 1) Indicate number of elements
			// between start (0) to current.
			sum += (arr[i]) *((n - i) *(i + 1));
		}
		// Display calculated result
		cout << "\n Sum " << sum;
	}
};
int main()
{
	SubArray *task = new SubArray();
	int arr[] = {
		8 , 1 , 4 , 3 , 2 , 1
	};
	/*
	  arr = [8, 1, 4, 3, 2, 1]
	  ---------------------------
	  8
	  8 + 1
	  8 + 1 + 4
	  8 + 1 + 4 + 3
	  8 + 1 + 4 + 3 + 2
	  8 + 1 + 4 + 3 + 2 + 1
	  1
	  1 + 4
	  1 + 4 + 3
	  1 + 4 + 3 + 2
	  1 + 4 + 3 + 2 + 1
	  4
	  4 + 3
	  4 + 3 + 2
	  4 + 3 + 2 + 1
	  3
	  3 + 2
	  3 + 2 + 1
	  2
	  2 + 1
	  1
	  --------------------
	  Sum of all 168
	*/
	int n = sizeof(arr) / sizeof(arr[0]);
	task->sumSubArray(arr, n);
	return 0;
}

Output

 Sum 168
// Include namespace system
using System;
/*
    Csharp Program
    Sum of all subarrays of an array efficiently
*/
public class SubArray
{
	public void sumSubArray(int[] arr, int n)
	{
		if (n <= 0)
		{
			return;
		}
		// Sum of occurrence of first element
		int sum = arr[0] * n;
		// Calculate occurrence in remaining elements
		for (int i = 1; i < n; ++i)
		{
			// Here
			// -----------------------------------
			// (n-i) Indicate number of element in
			// current to last element.
			// (i + 1) Indicate number of elements
			// between start (0) to current.
			sum += (arr[i]) * ((n - i) * (i + 1));
		}
		// Display calculated result
		Console.Write("\n Sum " + sum);
	}
	public static void Main(String[] args)
	{
		SubArray task = new SubArray();
		int[] arr = {
			8 , 1 , 4 , 3 , 2 , 1
		};
		/*
		  arr = [8, 1, 4, 3, 2, 1]
		  ---------------------------
		  8
		  8 + 1
		  8 + 1 + 4
		  8 + 1 + 4 + 3
		  8 + 1 + 4 + 3 + 2
		  8 + 1 + 4 + 3 + 2 + 1
		  1
		  1 + 4
		  1 + 4 + 3
		  1 + 4 + 3 + 2
		  1 + 4 + 3 + 2 + 1
		  4
		  4 + 3
		  4 + 3 + 2
		  4 + 3 + 2 + 1
		  3
		  3 + 2
		  3 + 2 + 1
		  2
		  2 + 1
		  1
		  --------------------
		  Sum of all 168
		*/
		int n = arr.Length;
		task.sumSubArray(arr, n);
	}
}

Output

 Sum 168
package main
import "fmt"
/*
    Go Program
    Sum of all subarrays of an array efficiently
*/
type SubArray struct {}
func getSubArray() * SubArray {
	var me *SubArray = &SubArray {}
	return me
}
func(this SubArray) sumSubArray(arr[] int, n int) {
	if n <= 0 {
		return
	}
	// Sum of occurrence of first element
	var sum int = arr[0] * n
	// Calculate occurrence in remaining elements
	for i := 1 ; i < n ; i++ {
		// Here
		// -----------------------------------
		// (n-i) Indicate number of element in
		// current to last element.
		// (i + 1) Indicate number of elements
		// between start (0) to current.
		sum += (arr[i]) * ((n - i) * (i + 1))
	}
	// Display calculated result
	fmt.Print("\n Sum ", sum)
}
func main() {
	var task * SubArray = getSubArray()
	var arr = [] int {
		8,
		1,
		4,
		3,
		2,
		1,
	}
	/*
	  arr = [8, 1, 4, 3, 2, 1]
	  ---------------------------
	  8
	  8 + 1
	  8 + 1 + 4
	  8 + 1 + 4 + 3
	  8 + 1 + 4 + 3 + 2
	  8 + 1 + 4 + 3 + 2 + 1
	  1
	  1 + 4
	  1 + 4 + 3
	  1 + 4 + 3 + 2
	  1 + 4 + 3 + 2 + 1
	  4
	  4 + 3
	  4 + 3 + 2
	  4 + 3 + 2 + 1
	  3
	  3 + 2
	  3 + 2 + 1
	  2
	  2 + 1
	  1
	  --------------------
	  Sum of all 168
	*/
	var n int = len(arr)
	task.sumSubArray(arr, n)
}

Output

 Sum 168
<?php
/*
    Php Program
    Sum of all subarrays of an array efficiently
*/
class SubArray
{
	public	function sumSubArray($arr, $n)
	{
		if ($n <= 0)
		{
			return;
		}
		// Sum of occurrence of first element
		$sum = $arr[0] * $n;
		// Calculate occurrence in remaining elements
		for ($i = 1; $i < $n; ++$i)
		{
			// Here
			// -----------------------------------
			// (n-i) Indicate number of element in
			// current to last element.
			// (i + 1) Indicate number of elements
			// between start (0) to current.
			$sum += ($arr[$i]) * (($n - $i) * ($i + 1));
		}
		// Display calculated result
		echo("\n Sum ".$sum);
	}
}

function main()
{
	$task = new SubArray();
	$arr = array(8, 1, 4, 3, 2, 1);
	/*
	  arr = [8, 1, 4, 3, 2, 1]
	  ---------------------------
	  8
	  8 + 1
	  8 + 1 + 4
	  8 + 1 + 4 + 3
	  8 + 1 + 4 + 3 + 2
	  8 + 1 + 4 + 3 + 2 + 1
	  1
	  1 + 4
	  1 + 4 + 3
	  1 + 4 + 3 + 2
	  1 + 4 + 3 + 2 + 1
	  4
	  4 + 3
	  4 + 3 + 2
	  4 + 3 + 2 + 1
	  3
	  3 + 2
	  3 + 2 + 1
	  2
	  2 + 1
	  1
	  --------------------
	  Sum of all 168
	*/
	$n = count($arr);
	$task->sumSubArray($arr, $n);
}
main();

Output

 Sum 168
/*
    Node JS Program
    Sum of all subarrays of an array efficiently
*/
class SubArray
{
	sumSubArray(arr, n)
	{
		if (n <= 0)
		{
			return;
		}
		// Sum of occurrence of first element
		var sum = arr[0] * n;
		// Calculate occurrence in remaining elements
		for (var i = 1; i < n; ++i)
		{
			// Here
			// -----------------------------------
			// (n-i) Indicate number of element in
			// current to last element.
			// (i + 1) Indicate number of elements
			// between start (0) to current.
			sum += (arr[i]) * ((n - i) * (i + 1));
		}
		// Display calculated result
		process.stdout.write("\n Sum " + sum);
	}
}

function main()
{
	var task = new SubArray();
	var arr = [8, 1, 4, 3, 2, 1];
	/*
	  arr = [8, 1, 4, 3, 2, 1]
	  ---------------------------
	  8
	  8 + 1
	  8 + 1 + 4
	  8 + 1 + 4 + 3
	  8 + 1 + 4 + 3 + 2
	  8 + 1 + 4 + 3 + 2 + 1
	  1
	  1 + 4
	  1 + 4 + 3
	  1 + 4 + 3 + 2
	  1 + 4 + 3 + 2 + 1
	  4
	  4 + 3
	  4 + 3 + 2
	  4 + 3 + 2 + 1
	  3
	  3 + 2
	  3 + 2 + 1
	  2
	  2 + 1
	  1
	  --------------------
	  Sum of all 168
	*/
	var n = arr.length;
	task.sumSubArray(arr, n);
}
main();

Output

 Sum 168
#    Python 3 Program
#    Sum of all subarrays of an array efficiently
class SubArray :
	def sumSubArray(self, arr, n) :
		if (n <= 0) :
			return
		
		#  Sum of occurrence of first element
		sum = arr[0] * n
		i = 1
		#  Calculate occurrence in remaining elements
		while (i < n) :
			#  Here
			#  -----------------------------------
			#  (n-i) Indicate number of element in
			#  current to last element.
			#  (i + 1) Indicate number of elements
			#  between start (0) to current.
			sum += (arr[i]) * ((n - i) * (i + 1))
			i += 1
		
		#  Display calculated result
		print("\n Sum ", sum, end = "")
	

def main() :
	task = SubArray()
	arr = [8, 1, 4, 3, 2, 1]
	#  arr = [8, 1, 4, 3, 2, 1]
	#  ---------------------------
	#  8
	#  8 + 1
	#  8 + 1 + 4
	#  8 + 1 + 4 + 3
	#  8 + 1 + 4 + 3 + 2
	#  8 + 1 + 4 + 3 + 2 + 1
	#  1
	#  1 + 4
	#  1 + 4 + 3
	#  1 + 4 + 3 + 2
	#  1 + 4 + 3 + 2 + 1
	#  4
	#  4 + 3
	#  4 + 3 + 2
	#  4 + 3 + 2 + 1
	#  3
	#  3 + 2
	#  3 + 2 + 1
	#  2
	#  2 + 1
	#  1
	#  --------------------
	#  Sum of all 168
	n = len(arr)
	task.sumSubArray(arr, n)

if __name__ == "__main__": main()

Output

 Sum  168
#    Ruby Program
#    Sum of all subarrays of an array efficiently
class SubArray 
	def sumSubArray(arr, n) 
		if (n <= 0) 
			return
		end

		#  Sum of occurrence of first element
		sum = arr[0] * n
		i = 1
		#  Calculate occurrence in remaining elements
		while (i < n) 
			#  Here
			#  -----------------------------------
			#  (n-i) Indicate number of element in
			#  current to last element.
			#  (i + 1) Indicate number of elements
			#  between start (0) to current.
			sum += (arr[i]) * ((n - i) * (i + 1))
			i += 1
		end

		#  Display calculated result
		print("\n Sum ", sum)
	end

end

def main() 
	task = SubArray.new()
	arr = [8, 1, 4, 3, 2, 1]
	#  arr = [8, 1, 4, 3, 2, 1]
	#  ---------------------------
	#  8
	#  8 + 1
	#  8 + 1 + 4
	#  8 + 1 + 4 + 3
	#  8 + 1 + 4 + 3 + 2
	#  8 + 1 + 4 + 3 + 2 + 1
	#  1
	#  1 + 4
	#  1 + 4 + 3
	#  1 + 4 + 3 + 2
	#  1 + 4 + 3 + 2 + 1
	#  4
	#  4 + 3
	#  4 + 3 + 2
	#  4 + 3 + 2 + 1
	#  3
	#  3 + 2
	#  3 + 2 + 1
	#  2
	#  2 + 1
	#  1
	#  --------------------
	#  Sum of all 168
	n = arr.length
	task.sumSubArray(arr, n)
end

main()

Output

 Sum 168
/*
    Scala Program
    Sum of all subarrays of an array efficiently
*/
class SubArray()
{
	def sumSubArray(arr: Array[Int], n: Int): Unit = {
		if (n <= 0)
		{
			return;
		}
		// Sum of occurrence of first element
		var sum: Int = arr(0) * n;
		var i: Int = 1;
		// Calculate occurrence in remaining elements
		while (i < n)
		{
			// Here
			// -----------------------------------
			// (n-i) Indicate number of element in
			// current to last element.
			// (i + 1) Indicate number of elements
			// between start (0) to current.
			sum += (arr(i)) * ((n - i) * (i + 1));
			i += 1;
		}
		// Display calculated result
		print("\n Sum " + sum);
	}
}
object Main
{
	def main(args: Array[String]): Unit = {
		var task: SubArray = new SubArray();
		var arr: Array[Int] = Array(8, 1, 4, 3, 2, 1);
		/*
		  arr = [8, 1, 4, 3, 2, 1]
		  ---------------------------
		  8
		  8 + 1
		  8 + 1 + 4
		  8 + 1 + 4 + 3
		  8 + 1 + 4 + 3 + 2
		  8 + 1 + 4 + 3 + 2 + 1
		  1
		  1 + 4
		  1 + 4 + 3
		  1 + 4 + 3 + 2
		  1 + 4 + 3 + 2 + 1
		  4
		  4 + 3
		  4 + 3 + 2
		  4 + 3 + 2 + 1
		  3
		  3 + 2
		  3 + 2 + 1
		  2
		  2 + 1
		  1
		  --------------------
		  Sum of all 168
		*/
		var n: Int = arr.length;
		task.sumSubArray(arr, n);
	}
}

Output

 Sum 168
import Foundation;
/*
    Swift 4 Program
    Sum of all subarrays of an array efficiently
*/
class SubArray
{
	func sumSubArray(_ arr: [Int], _ n: Int)
	{
		if (n <= 0)
		{
			return;
		}
		// Sum of occurrence of first element
		var sum: Int = arr[0] * n;
		var i: Int = 1;
		// Calculate occurrence in remaining elements
		while (i < n)
		{
			// Here
			// -----------------------------------
			// (n-i) Indicate number of element in
			// current to last element.
			// (i + 1) Indicate number of elements
			// between start (0) to current.
			sum += (arr[i]) * ((n - i) * (i + 1));
			i += 1;
		}
		// Display calculated result
		print("\n Sum ", sum, terminator: "");
	}
}
func main()
{
	let task: SubArray = SubArray();
	let arr: [Int] = [8, 1, 4, 3, 2, 1];
	/*
	  arr = [8, 1, 4, 3, 2, 1]
	  ---------------------------
	  8
	  8 + 1
	  8 + 1 + 4
	  8 + 1 + 4 + 3
	  8 + 1 + 4 + 3 + 2
	  8 + 1 + 4 + 3 + 2 + 1
	  1
	  1 + 4
	  1 + 4 + 3
	  1 + 4 + 3 + 2
	  1 + 4 + 3 + 2 + 1
	  4
	  4 + 3
	  4 + 3 + 2
	  4 + 3 + 2 + 1
	  3
	  3 + 2
	  3 + 2 + 1
	  2
	  2 + 1
	  1
	  --------------------
	  Sum of all 168
	*/
	let n: Int = arr.count;
	task.sumSubArray(arr, n);
}
main();

Output

 Sum  168
/*
    Kotlin Program
    Sum of all subarrays of an array efficiently
*/
class SubArray
{
	fun sumSubArray(arr: Array < Int > , n: Int): Unit
	{
		if (n <= 0)
		{
			return;
		}
		// Sum of occurrence of first element
		var sum: Int = arr[0] * n;
		var i: Int = 1;
		// Calculate occurrence in remaining elements
		while (i < n)
		{
			// Here
			// -----------------------------------
			// (n-i) Indicate number of element in
			// current to last element.
			// (i + 1) Indicate number of elements
			// between start (0) to current.
			sum += (arr[i]) * ((n - i) * (i + 1));
			i += 1;
		}
		// Display calculated result
		print("\n Sum " + sum);
	}
}
fun main(args: Array < String > ): Unit
{
	val task: SubArray = SubArray();
	val arr: Array < Int > = arrayOf(8, 1, 4, 3, 2, 1);
	/*
	  arr = [8, 1, 4, 3, 2, 1]
	  ---------------------------
	  8
	  8 + 1
	  8 + 1 + 4
	  8 + 1 + 4 + 3
	  8 + 1 + 4 + 3 + 2
	  8 + 1 + 4 + 3 + 2 + 1
	  1
	  1 + 4
	  1 + 4 + 3
	  1 + 4 + 3 + 2
	  1 + 4 + 3 + 2 + 1
	  4
	  4 + 3
	  4 + 3 + 2
	  4 + 3 + 2 + 1
	  3
	  3 + 2
	  3 + 2 + 1
	  2
	  2 + 1
	  1
	  --------------------
	  Sum of all 168
	*/
	val n: Int = arr.count();
	task.sumSubArray(arr, n);
}

Output

 Sum 168


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