Maximum equilibrium sum in an array

Here given code implementation process.

/*
    C program for
    Maximum equilibrium sum in an array
*/
#include <stdio.h>

#include <limits.h>
 // Display given array elements
void printArray(int arr[], int n)
{
	for (int i = 0; i < n; ++i)
	{
		printf(" %d", arr[i]);
	}
}
// Returns the maximum value of given two numbers
int maxValue(int a, int b)
{
	if (a > b)
	{
		return a;
	}
	return b;
}
void findMaximumSum(int arr[], int n)
{
	int sum = 0;
	int prefix = 0;
	int result = INT_MIN;
	// Sum of array elements
	for (int i = 0; i < n; ++i)
	{
		sum += arr[i];
	}
	// Find equilibrium sum
	for (int i = 0; i < n; i++)
	{
		// Prefix sum
		prefix = prefix + arr[i];
		if (prefix == sum)
		{
			result = maxValue(result, prefix);
		}
		// Reduce sum by current array element  
		sum = sum - arr[i];
	}
	printArray(arr, n);
	printf("\n Result : %d \n", result);
}
int main()
{
	// Given array elements
	int arr1[] = {
		6 , 5 , 2 , -5 , 8 , 1 , 4 , -4 , 7
	};
	int arr2[] = {
		3 , -2 , 1 , 4 , 5 , -3
	};
	// Test A
	// Get the number of elements
	int n = sizeof(arr1) / sizeof(arr1[0]);
	//  [6, 5, 2, -5, 8] == [8, 1, 4, -4, 7]
	// Result 16 [6 + 5 + 2 + -5 + 8]
	findMaximumSum(arr1, n);
	// Test B
	// Get the number of elements
	n = sizeof(arr2) / sizeof(arr2[0]);
	// [ 3, -2, 1, 4] == [4, 5,-3]
	// Result 6 [ 3 + -2 + 1 + 4]
	findMaximumSum(arr2, n);
	return 0;
}

Output

 6 5 2 -5 8 1 4 -4 7
 Result : 16
 3 -2 1 4 5 -3
 Result : 6
/*
    Java Program for
    Maximum equilibrium sum in an array
*/
public class EquilibriumSum
{
	// Display given array elements
	public void printArray(int[] arr, int n)
	{
		for (int i = 0; i < n; ++i)
		{
			System.out.print(" " + arr[i]);
		}
	}
	// Returns the maximum value of given two numbers
	public int maxValue(int a, int b)
	{
		if (a > b)
		{
			return a;
		}
		return b;
	}
	public void findMaximumSum(int[] arr, int n)
	{
		int sum = 0;
		int prefix = 0;
		int result = Integer.MIN_VALUE;
		// Sum of array elements
		for (int i = 0; i < n; ++i)
		{
			sum += arr[i];
		}
		// Find equilibrium sum
		for (int i = 0; i < n; i++)
		{
			// Prefix sum
			prefix = prefix + arr[i];
			if (prefix == sum)
			{
				result = maxValue(result, prefix);
			}
			// Reduce sum by current array element  
			sum = sum - arr[i];
		}
		printArray(arr, n);
		System.out.print("\n Result : " + result + " \n");
	}
	public static void main(String[] args)
	{
		EquilibriumSum task = new EquilibriumSum();
		// Given array elements
		int[] arr1 = {
			6 , 5 , 2 , -5 , 8 , 1 , 4 , -4 , 7
		};
		int[] arr2 = {
			3 , -2 , 1 , 4 , 5 , -3
		};
		// Test A
		// Get the number of elements
		int n = arr1.length;
		//  [6, 5, 2, -5, 8] == [8, 1, 4, -4, 7]
		// Result 16 [6 + 5 + 2 + -5 + 8]
		task.findMaximumSum(arr1, n);
		// Test B
		// Get the number of elements
		n = arr2.length;
		// [ 3, -2, 1, 4] == [4, 5,-3]
		// Result 6 [ 3 + -2 + 1 + 4]
		task.findMaximumSum(arr2, n);
	}
}

Output

 6 5 2 -5 8 1 4 -4 7
 Result : 16
 3 -2 1 4 5 -3
 Result : 6
// Include header file
#include <iostream>
#include <limits.h>

using namespace std;
/*
    C++ Program for
    Maximum equilibrium sum in an array
*/
class EquilibriumSum
{
	public:
		// Display given array elements
		void printArray(int arr[], int n)
		{
			for (int i = 0; i < n; ++i)
			{
				cout << " " << arr[i];
			}
		}
	// Returns the maximum value of given two numbers
	int maxValue(int a, int b)
	{
		if (a > b)
		{
			return a;
		}
		return b;
	}
	void findMaximumSum(int arr[], int n)
	{
		int sum = 0;
		int prefix = 0;
		int result = INT_MIN;
		// Sum of array elements
		for (int i = 0; i < n; ++i)
		{
			sum += arr[i];
		}
		// Find equilibrium sum
		for (int i = 0; i < n; i++)
		{
			// Prefix sum
			prefix = prefix + arr[i];
			if (prefix == sum)
			{
				result = this->maxValue(result, prefix);
			}
			// Reduce sum by current array element  
			sum = sum - arr[i];
		}
		this->printArray(arr, n);
		cout << "\n Result : " << result << " \n";
	}
};
int main()
{
	EquilibriumSum *task = new EquilibriumSum();
	// Given array elements
	int arr1[] = {
		6 , 5 , 2 , -5 , 8 , 1 , 4 , -4 , 7
	};
	int arr2[] = {
		3 , -2 , 1 , 4 , 5 , -3
	};
	// Test A
	// Get the number of elements
	int n = sizeof(arr1) / sizeof(arr1[0]);
	//  [6, 5, 2, -5, 8] == [8, 1, 4, -4, 7]
	// Result 16 [6 + 5 + 2 + -5 + 8]
	task->findMaximumSum(arr1, n);
	// Test B
	// Get the number of elements
	n = sizeof(arr2) / sizeof(arr2[0]);
	// [ 3, -2, 1, 4] == [4, 5,-3]
	// Result 6 [ 3 + -2 + 1 + 4]
	task->findMaximumSum(arr2, n);
	return 0;
}

Output

 6 5 2 -5 8 1 4 -4 7
 Result : 16
 3 -2 1 4 5 -3
 Result : 6
// Include namespace system
using System;
/*
    Csharp Program for
    Maximum equilibrium sum in an array
*/
public class EquilibriumSum
{
	// Display given array elements
	public void printArray(int[] arr, int n)
	{
		for (int i = 0; i < n; ++i)
		{
			Console.Write(" " + arr[i]);
		}
	}
	// Returns the maximum value of given two numbers
	public int maxValue(int a, int b)
	{
		if (a > b)
		{
			return a;
		}
		return b;
	}
	public void findMaximumSum(int[] arr, int n)
	{
		int sum = 0;
		int prefix = 0;
		int result = int.MinValue;
		// Sum of array elements
		for (int i = 0; i < n; ++i)
		{
			sum += arr[i];
		}
		// Find equilibrium sum
		for (int i = 0; i < n; i++)
		{
			// Prefix sum
			prefix = prefix + arr[i];
			if (prefix == sum)
			{
				result = this.maxValue(result, prefix);
			}
			// Reduce sum by current array element  
			sum = sum - arr[i];
		}
		this.printArray(arr, n);
		Console.Write("\n Result : " + result + " \n");
	}
	public static void Main(String[] args)
	{
		EquilibriumSum task = new EquilibriumSum();
		// Given array elements
		int[] arr1 = {
			6 , 5 , 2 , -5 , 8 , 1 , 4 , -4 , 7
		};
		int[] arr2 = {
			3 , -2 , 1 , 4 , 5 , -3
		};
		// Test A
		// Get the number of elements
		int n = arr1.Length;
		//  [6, 5, 2, -5, 8] == [8, 1, 4, -4, 7]
		// Result 16 [6 + 5 + 2 + -5 + 8]
		task.findMaximumSum(arr1, n);
		// Test B
		// Get the number of elements
		n = arr2.Length;
		// [ 3, -2, 1, 4] == [4, 5,-3]
		// Result 6 [ 3 + -2 + 1 + 4]
		task.findMaximumSum(arr2, n);
	}
}

Output

 6 5 2 -5 8 1 4 -4 7
 Result : 16
 3 -2 1 4 5 -3
 Result : 6
package main
import "math"
import "fmt"
/*
    Go Program for
    Maximum equilibrium sum in an array
*/

// Display given array elements
func printArray(arr[] int, n int) {
	for i := 0 ; i < n ; i++ {
		fmt.Print(" ", arr[i])
	}
}
// Returns the maximum value of given two numbers
func maxValue(a, b int) int {
	if a > b {
		return a
	}
	return b
}
func findMaximumSum(arr[] int, n int) {
	var sum int = 0
	var prefix int = 0
	var result int = math.MaxInt64
	// Sum of array elements
	for i := 0 ; i < n ; i++ {
		sum += arr[i]
	}
	// Find equilibrium sum
	for i := 0 ; i < n ; i++ {
		// Prefix sum
		prefix = prefix + arr[i]
		if prefix == sum {
			result = maxValue(result, prefix)
		}
		// Reduce sum by current array element  
		sum = sum - arr[i]
	}
	printArray(arr, n)
	fmt.Print("\n Result : ", result, " \n")
}
func main() {
	
	// Given array elements
	var arr1 = [] int { 6 , 5 , 2 , -5 , 8 , 1 , 4 , -4 , 7 }
	var arr2 = [] int { 3 , -2 , 1 , 4 , 5 , -3 }
	// Test A
	// Get the number of elements
	var n int = len(arr1)
	//  [6, 5, 2, -5, 8] == [8, 1, 4, -4, 7]
	// Result 16 [6 + 5 + 2 + -5 + 8]
	findMaximumSum(arr1, n)
	// Test B
	// Get the number of elements
	n = len(arr2)
	// [ 3, -2, 1, 4] == [4, 5,-3]
	// Result 6 [ 3 + -2 + 1 + 4]
	findMaximumSum(arr2, n)
}

Output

 6 5 2 -5 8 1 4 -4 7
 Result : 16
 3 -2 1 4 5 -3
 Result : 6
<?php
/*
    Php Program for
    Maximum equilibrium sum in an array
*/
class EquilibriumSum
{
	// Display given array elements
	public	function printArray($arr, $n)
	{
		for ($i = 0; $i < $n; ++$i)
		{
			echo(" ".$arr[$i]);
		}
	}
	// Returns the maximum value of given two numbers
	public	function maxValue($a, $b)
	{
		if ($a > $b)
		{
			return $a;
		}
		return $b;
	}
	public	function findMaximumSum($arr, $n)
	{
		$sum = 0;
		$prefix = 0;
		$result = -PHP_INT_MAX;
		// Sum of array elements
		for ($i = 0; $i < $n; ++$i)
		{
			$sum += $arr[$i];
		}
		// Find equilibrium sum
		for ($i = 0; $i < $n; $i++)
		{
			// Prefix sum
			$prefix = $prefix + $arr[$i];
			if ($prefix == $sum)
			{
				$result = $this->maxValue($result, $prefix);
			}
			// Reduce sum by current array element  
			$sum = $sum - $arr[$i];
		}
		$this->printArray($arr, $n);
		echo("\n Result : ".$result.
			" \n");
	}
}

function main()
{
	$task = new EquilibriumSum();
	// Given array elements
	$arr1 = array(6, 5, 2, -5, 8, 1, 4, -4, 7);
	$arr2 = array(3, -2, 1, 4, 5, -3);
	// Test A
	// Get the number of elements
	$n = count($arr1);
	//  [6, 5, 2, -5, 8] == [8, 1, 4, -4, 7]
	// Result 16 [6 + 5 + 2 + -5 + 8]
	$task->findMaximumSum($arr1, $n);
	// Test B
	// Get the number of elements
	$n = count($arr2);
	// [ 3, -2, 1, 4] == [4, 5,-3]
	// Result 6 [ 3 + -2 + 1 + 4]
	$task->findMaximumSum($arr2, $n);
}
main();

Output

 6 5 2 -5 8 1 4 -4 7
 Result : 16
 3 -2 1 4 5 -3
 Result : 6
/*
    Node JS Program for
    Maximum equilibrium sum in an array
*/
class EquilibriumSum
{
	// Display given array elements
	printArray(arr, n)
	{
		for (var i = 0; i < n; ++i)
		{
			process.stdout.write(" " + arr[i]);
		}
	}
	// Returns the maximum value of given two numbers
	maxValue(a, b)
	{
		if (a > b)
		{
			return a;
		}
		return b;
	}
	findMaximumSum(arr, n)
	{
		var sum = 0;
		var prefix = 0;
		var result = -Number.MAX_VALUE;
		// Sum of array elements
		for (var i = 0; i < n; ++i)
		{
			sum += arr[i];
		}
		// Find equilibrium sum
		for (var i = 0; i < n; i++)
		{
			// Prefix sum
			prefix = prefix + arr[i];
			if (prefix == sum)
			{
				result = this.maxValue(result, prefix);
			}
			// Reduce sum by current array element  
			sum = sum - arr[i];
		}
		this.printArray(arr, n);
		process.stdout.write("\n Result : " + result + " \n");
	}
}

function main()
{
	var task = new EquilibriumSum();
	// Given array elements
	var arr1 = [6, 5, 2, -5, 8, 1, 4, -4, 7];
	var arr2 = [3, -2, 1, 4, 5, -3];
	// Test A
	// Get the number of elements
	var n = arr1.length;
	//  [6, 5, 2, -5, 8] == [8, 1, 4, -4, 7]
	// Result 16 [6 + 5 + 2 + -5 + 8]
	task.findMaximumSum(arr1, n);
	// Test B
	// Get the number of elements
	n = arr2.length;
	// [ 3, -2, 1, 4] == [4, 5,-3]
	// Result 6 [ 3 + -2 + 1 + 4]
	task.findMaximumSum(arr2, n);
}
main();

Output

 6 5 2 -5 8 1 4 -4 7
 Result : 16
 3 -2 1 4 5 -3
 Result : 6
import sys
#    Python 3 Program for
#    Maximum equilibrium sum in an array
class EquilibriumSum :
	#  Display given list elements
	def printArray(self, arr, n) :
		i = 0
		while (i < n) :
			print(" ", arr[i], end = "")
			i += 1
		
	
	#  Returns the maximum value of given two numbers
	def maxValue(self, a, b) :
		if (a > b) :
			return a
		
		return b
	
	def findMaximumSum(self, arr, n) :
		sum = 0
		prefix = 0
		result = -sys.maxsize
		i = 0
		#  Sum of list elements
		while (i < n) :
			sum += arr[i]
			i += 1
		
		i = 0
		#  Find equilibrium sum
		while (i < n) :
			#  Prefix sum
			prefix = prefix + arr[i]
			if (prefix == sum) :
				result = self.maxValue(result, prefix)
			
			#  Reduce sum by current list element  
			sum = sum - arr[i]
			i += 1
		
		self.printArray(arr, n)
		print("\n Result : ", result ," ")
	

def main() :
	task = EquilibriumSum()
	#  Given list elements
	arr1 = [6, 5, 2, -5, 8, 1, 4, -4, 7]
	arr2 = [3, -2, 1, 4, 5, -3]
	#  Test A
	#  Get the number of elements
	n = len(arr1)
	#   [6, 5, 2, -5, 8] == [8, 1, 4, -4, 7]
	#  Result 16 [6 + 5 + 2 + -5 + 8]
	task.findMaximumSum(arr1, n)
	#  Test B
	#  Get the number of elements
	n = len(arr2)
	#  [ 3, -2, 1, 4] == [4, 5,-3]
	#  Result 6 [ 3 + -2 + 1 + 4]
	task.findMaximumSum(arr2, n)

if __name__ == "__main__": main()

Output

  6  5  2  -5  8  1  4  -4  7
 Result :  16
  3  -2  1  4  5  -3
 Result :  6
#    Ruby Program for
#    Maximum equilibrium sum in an array
class EquilibriumSum 
	#  Display given array elements
	def printArray(arr, n) 
		i = 0
		while (i < n) 
			print(" ", arr[i])
			i += 1
		end

	end

	#  Returns the maximum value of given two numbers
	def maxValue(a, b) 
		if (a > b) 
			return a
		end

		return b
	end

	def findMaximumSum(arr, n) 
		sum = 0
		prefix = 0
		result = -(2 ** (0. size * 8 - 2))
		i = 0
		#  Sum of array elements
		while (i < n) 
			sum += arr[i]
			i += 1
		end

		i = 0
		#  Find equilibrium sum
		while (i < n) 
			#  Prefix sum
			prefix = prefix + arr[i]
			if (prefix == sum) 
				result = self.maxValue(result, prefix)
			end

			#  Reduce sum by current array element  
			sum = sum - arr[i]
			i += 1
		end

		self.printArray(arr, n)
		print("\n Result : ", result ," \n")
	end

end

def main() 
	task = EquilibriumSum.new()
	#  Given array elements
	arr1 = [6, 5, 2, -5, 8, 1, 4, -4, 7]
	arr2 = [3, -2, 1, 4, 5, -3]
	#  Test A
	#  Get the number of elements
	n = arr1.length
	#   [6, 5, 2, -5, 8] == [8, 1, 4, -4, 7]
	#  Result 16 [6 + 5 + 2 + -5 + 8]
	task.findMaximumSum(arr1, n)
	#  Test B
	#  Get the number of elements
	n = arr2.length
	#  [ 3, -2, 1, 4] == [4, 5,-3]
	#  Result 6 [ 3 + -2 + 1 + 4]
	task.findMaximumSum(arr2, n)
end

main()

Output

 6 5 2 -5 8 1 4 -4 7
 Result : 16 
 3 -2 1 4 5 -3
 Result : 6 
/*
    Scala Program for
    Maximum equilibrium sum in an array
*/
class EquilibriumSum()
{
	// Display given array elements
	def printArray(arr: Array[Int], n: Int): Unit = {
		var i: Int = 0;
		while (i < n)
		{
			print(" " + arr(i));
			i += 1;
		}
	}
	// Returns the maximum value of given two numbers
	def maxValue(a: Int, b: Int): Int = {
		if (a > b)
		{
			return a;
		}
		return b;
	}
	def findMaximumSum(arr: Array[Int], n: Int): Unit = {
		var sum: Int = 0;
		var prefix: Int = 0;
		var result: Int = Int.MinValue;
		var i: Int = 0;
		// Sum of array elements
		while (i < n)
		{
			sum += arr(i);
			i += 1;
		}
		i = 0;
		// Find equilibrium sum
		while (i < n)
		{
			// Prefix sum
			prefix = prefix + arr(i);
			if (prefix == sum)
			{
				result = maxValue(result, prefix);
			}
			// Reduce sum by current array element  
			sum = sum - arr(i);
			i += 1;
		}
		printArray(arr, n);
		print("\n Result : " + result + " \n");
	}
}
object Main
{
	def main(args: Array[String]): Unit = {
		var task: EquilibriumSum = new EquilibriumSum();
		// Given array elements
		var arr1: Array[Int] = Array(6, 5, 2, -5, 8, 1, 4, -4, 7);
		var arr2: Array[Int] = Array(3, -2, 1, 4, 5, -3);
		// Test A
		// Get the number of elements
		var n: Int = arr1.length;
		//  [6, 5, 2, -5, 8] == [8, 1, 4, -4, 7]
		// Result 16 [6 + 5 + 2 + -5 + 8]
		task.findMaximumSum(arr1, n);
		// Test B
		// Get the number of elements
		n = arr2.length;
		// [ 3, -2, 1, 4] == [4, 5,-3]
		// Result 6 [ 3 + -2 + 1 + 4]
		task.findMaximumSum(arr2, n);
	}
}

Output

 6 5 2 -5 8 1 4 -4 7
 Result : 16
 3 -2 1 4 5 -3
 Result : 6
import Foundation;
/*
    Swift 4 Program for
    Maximum equilibrium sum in an array
*/
class EquilibriumSum
{
	// Display given array elements
	func printArray(_ arr: [Int], _ n: Int)
	{
		var i: Int = 0;
		while (i < n)
		{
			print(" ", arr[i], terminator: "");
			i += 1;
		}
	}
	// Returns the maximum value of given two numbers
	func maxValue(_ a: Int, _ b: Int) -> Int
	{
		if (a > b)
		{
			return a;
		}
		return b;
	}
	func findMaximumSum(_ arr: [Int], _ n: Int)
	{
		var sum: Int = 0;
		var prefix: Int = 0;
		var result: Int = Int.min;
		var i: Int = 0;
		// Sum of array elements
		while (i < n)
		{
			sum += arr[i];
			i += 1;
		}
		i = 0;
		// Find equilibrium sum
		while (i < n)
		{
			// Prefix sum
			prefix = prefix + arr[i];
			if (prefix == sum)
			{
				result = self.maxValue(result, prefix);
			}
			// Reduce sum by current array element  
			sum = sum - arr[i];
			i += 1;
		}
		self.printArray(arr, n);
		print("\n Result : ", result ," ");
	}
}
func main()
{
	let task: EquilibriumSum = EquilibriumSum();
	// Given array elements
	let arr1: [Int] = [6, 5, 2, -5, 8, 1, 4, -4, 7];
	let arr2: [Int] = [3, -2, 1, 4, 5, -3];
	// Test A
	// Get the number of elements
	var n: Int = arr1.count;
	//  [6, 5, 2, -5, 8] == [8, 1, 4, -4, 7]
	// Result 16 [6 + 5 + 2 + -5 + 8]
	task.findMaximumSum(arr1, n);
	// Test B
	// Get the number of elements
	n = arr2.count;
	// [ 3, -2, 1, 4] == [4, 5,-3]
	// Result 6 [ 3 + -2 + 1 + 4]
	task.findMaximumSum(arr2, n);
}
main();

Output

  6  5  2  -5  8  1  4  -4  7
 Result :  16
  3  -2  1  4  5  -3
 Result :  6
/*
    Kotlin Program for
    Maximum equilibrium sum in an array
*/
class EquilibriumSum
{
	// Display given array elements
	fun printArray(arr: Array < Int > , n: Int): Unit
	{
		var i: Int = 0;
		while (i < n)
		{
			print(" " + arr[i]);
			i += 1;
		}
	}
	// Returns the maximum value of given two numbers
	fun maxValue(a: Int, b: Int): Int
	{
		if (a > b)
		{
			return a;
		}
		return b;
	}
	fun findMaximumSum(arr: Array < Int > , n: Int): Unit
	{
		var sum: Int = 0;
		var prefix: Int = 0;
		var result: Int = Int.MIN_VALUE;
		var i: Int = 0;
		// Sum of array elements
		while (i < n)
		{
			sum += arr[i];
			i += 1;
		}
		i = 0;
		// Find equilibrium sum
		while (i < n)
		{
			// Prefix sum
			prefix = prefix + arr[i];
			if (prefix == sum)
			{
				result = this.maxValue(result, prefix);
			}
			// Reduce sum by current array element  
			sum = sum - arr[i];
			i += 1;
		}
		this.printArray(arr, n);
		print("\n Result : " + result + " \n");
	}
}
fun main(args: Array < String > ): Unit
{
	val task: EquilibriumSum = EquilibriumSum();
	// Given array elements
	val arr1: Array < Int > = arrayOf(6, 5, 2, -5, 8, 1, 4, -4, 7);
	val arr2: Array < Int > = arrayOf(3, -2, 1, 4, 5, -3);
	// Test A
	// Get the number of elements
	var n: Int = arr1.count();
	//  [6, 5, 2, -5, 8] == [8, 1, 4, -4, 7]
	// Result 16 [6 + 5 + 2 + -5 + 8]
	task.findMaximumSum(arr1, n);
	// Test B
	// Get the number of elements
	n = arr2.count();
	// [ 3, -2, 1, 4] == [4, 5,-3]
	// Result 6 [ 3 + -2 + 1 + 4]
	task.findMaximumSum(arr2, n);
}

Output

 6 5 2 -5 8 1 4 -4 7
 Result : 16
 3 -2 1 4 5 -3
 Result : 6


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