Maximum circular subarray sum solution

Here given code implementation process.

/*
    C program for
    Maximum circular subarray sum solution
*/
#include <stdio.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;
}
// Returns the manimum value of given two numbers
int minValue(int a, int b)
{
	if (a < b)
	{
		return a;
	}
	return b;
}
void maximumCircularSum(int arr[], int n)
{
	int result = 0;
	if (n > 1)
	{
		if (n == 1)
		{
			// When have single element in array
			result = arr[0];
		}
		else
		{
			int sum = 0;
			// Assign first element of array to all auxiliary variable
			int currentMax = arr[0];
			int max = currentMax;
			int currentMin = currentMax;
			int min = currentMax;
			// Sum of array elements
			for (int i = 0; i < n; ++i)
			{
				sum = sum + arr[i];
			}
			// Execute the loop from counter 1 to n 
			for (int i = 1; i < n; ++i)
			{
				// Minimum subarray sum
				currentMin = minValue(currentMin + arr[i], arr[i]);
				min = minValue(min, currentMin);
				// Maximum subarray sum
				currentMax = maxValue(currentMax + arr[i], arr[i]);
				max = maxValue(max, currentMax);
			}
			if (min == sum)
			{
				result = max;
			}
			else
			{
				result = maxValue(max, sum - min);
			}
		}
	}
	// Display given array
	printArray(arr, n);
	// Display calculated result
	printf("\n Result : %d \n", result);
}
int main()
{
	// Given array elements
	int arr1[] = {
		2 , 3 , -2 , 3 , 4 , -3 , -6 , 2 , -1
	};
	int arr2[] = {
		-5 , 1 , 2 , 3
	};
	int arr3[] = {
		1 , 2 , 4
	};
	int arr4[] = {
		5 , -3 , -1 , -4 , 4
	};
	// Test A
	// Get the number of elements
	int n = sizeof(arr1) / sizeof(arr1[0]);
	// [2, 3,-2, 3 , 4, 2, -1]
	// Result 11
	maximumCircularSum(arr1, n);
	// Test B
	// Get the number of elements
	n = sizeof(arr2) / sizeof(arr2[0]);
	// [1, 2, 3]
	// Result 6
	maximumCircularSum(arr2, n);
	// Test C
	// Get the number of elements
	n = sizeof(arr3) / sizeof(arr3[0]);
	// [1, 2, 4]
	// Result 7
	maximumCircularSum(arr3, n);
	// Test D
	// Get the number of elements
	n = sizeof(arr4) / sizeof(arr4[0]);
	// [5, 4]
	// Result 9
	maximumCircularSum(arr4, n);
	return 0;
}

Output

 2 3 -2 3 4 -3 -6 2 -1
 Result : 11
 -5 1 2 3
 Result : 6
 1 2 4
 Result : 7
 5 -3 -1 -4 4
 Result : 9
/*
    Java Program for
    Maximum circular subarray sum solution
*/
public class CircularSubarray
{
	// 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;
	}
	// Returns the manimum value of given two numbers
	public int minValue(int a, int b)
	{
		if (a < b)
		{
			return a;
		}
		return b;
	}
	public void maximumCircularSum(int[] arr, int n)
	{
		int result = 0;
		if (n > 1)
		{
			if (n == 1)
			{
				// When have single element in array
				result = arr[0];
			}
			else
			{
				int sum = 0;
				// Assign first element of array to all auxiliary variable
				int currentMax = arr[0];
				int max = currentMax;
				int currentMin = currentMax;
				int min = currentMax;
				// Sum of array elements
				for (int i = 0; i < n; ++i)
				{
					sum = sum + arr[i];
				}
				// Execute the loop from counter 1 to n 
				for (int i = 1; i < n; ++i)
				{
					// Minimum subarray sum
					currentMin = minValue(currentMin + arr[i], arr[i]);
					min = minValue(min, currentMin);
					// Maximum subarray sum
					currentMax = maxValue(currentMax + arr[i], arr[i]);
					max = maxValue(max, currentMax);
				}
				if (min == sum)
				{
					result = max;
				}
				else
				{
					result = maxValue(max, sum - min);
				}
			}
		}
		// Display given array
		printArray(arr, n);
		// Display calculated result
		System.out.print("\n Result : " + result + " \n");
	}
	public static void main(String[] args)
	{
		CircularSubarray task = new CircularSubarray();
		// Given array elements
		int[] arr1 = {
			2 , 3 , -2 , 3 , 4 , -3 , -6 , 2 , -1
		};
		int[] arr2 = {
			-5 , 1 , 2 , 3
		};
		int[] arr3 = {
			1 , 2 , 4
		};
		int[] arr4 = {
			5 , -3 , -1 , -4 , 4
		};
		// Test A
		// Get the number of elements
		int n = arr1.length;
		// [2, 3,-2, 3 , 4, 2, -1]
		// Result 11
		task.maximumCircularSum(arr1, n);
		// Test B
		// Get the number of elements
		n = arr2.length;
		// [1, 2, 3]
		// Result 6
		task.maximumCircularSum(arr2, n);
		// Test C
		// Get the number of elements
		n = arr3.length;
		// [1, 2, 4]
		// Result 7
		task.maximumCircularSum(arr3, n);
		// Test D
		// Get the number of elements
		n = arr4.length;
		// [5, 4]
		// Result 9
		task.maximumCircularSum(arr4, n);
	}
}

Output

 2 3 -2 3 4 -3 -6 2 -1
 Result : 11
 -5 1 2 3
 Result : 6
 1 2 4
 Result : 7
 5 -3 -1 -4 4
 Result : 9
// Include header file
#include <iostream>
using namespace std;
/*
    C++ Program for
    Maximum circular subarray sum solution
*/
class CircularSubarray
{
	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;
	}
	// Returns the manimum value of given two numbers
	int minValue(int a, int b)
	{
		if (a < b)
		{
			return a;
		}
		return b;
	}
	void maximumCircularSum(int arr[], int n)
	{
		int result = 0;
		if (n > 1)
		{
			if (n == 1)
			{
				// When have single element in array
				result = arr[0];
			}
			else
			{
				int sum = 0;
				// Assign first element of array to all auxiliary variable
				int currentMax = arr[0];
				int max = currentMax;
				int currentMin = currentMax;
				int min = currentMax;
				// Sum of array elements
				for (int i = 0; i < n; ++i)
				{
					sum = sum + arr[i];
				}
				// Execute the loop from counter 1 to n 
				for (int i = 1; i < n; ++i)
				{
					// Minimum subarray sum
					currentMin = this->minValue(currentMin + arr[i], arr[i]);
					min = this->minValue(min, currentMin);
					// Maximum subarray sum
					currentMax = this->maxValue(currentMax + arr[i], arr[i]);
					max = this->maxValue(max, currentMax);
				}
				if (min == sum)
				{
					result = max;
				}
				else
				{
					result = this->maxValue(max, sum - min);
				}
			}
		}
		// Display given array
		this->printArray(arr, n);
		// Display calculated result
		cout << "\n Result : " << result << " \n";
	}
};
int main()
{
	CircularSubarray *task = new CircularSubarray();
	// Given array elements
	int arr1[] = {
		2 , 3 , -2 , 3 , 4 , -3 , -6 , 2 , -1
	};
	int arr2[] = {
		-5 , 1 , 2 , 3
	};
	int arr3[] = {
		1 , 2 , 4
	};
	int arr4[] = {
		5 , -3 , -1 , -4 , 4
	};
	// Test A
	// Get the number of elements
	int n = sizeof(arr1) / sizeof(arr1[0]);
	// [2, 3,-2, 3 , 4, 2, -1]
	// Result 11
	task->maximumCircularSum(arr1, n);
	// Test B
	// Get the number of elements
	n = sizeof(arr2) / sizeof(arr2[0]);
	// [1, 2, 3]
	// Result 6
	task->maximumCircularSum(arr2, n);
	// Test C
	// Get the number of elements
	n = sizeof(arr3) / sizeof(arr3[0]);
	// [1, 2, 4]
	// Result 7
	task->maximumCircularSum(arr3, n);
	// Test D
	// Get the number of elements
	n = sizeof(arr4) / sizeof(arr4[0]);
	// [5, 4]
	// Result 9
	task->maximumCircularSum(arr4, n);
	return 0;
}

Output

 2 3 -2 3 4 -3 -6 2 -1
 Result : 11
 -5 1 2 3
 Result : 6
 1 2 4
 Result : 7
 5 -3 -1 -4 4
 Result : 9
// Include namespace system
using System;
/*
    Csharp Program for
    Maximum circular subarray sum solution
*/
public class CircularSubarray
{
	// 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;
	}
	// Returns the manimum value of given two numbers
	public int minValue(int a, int b)
	{
		if (a < b)
		{
			return a;
		}
		return b;
	}
	public void maximumCircularSum(int[] arr, int n)
	{
		int result = 0;
		if (n > 1)
		{
			if (n == 1)
			{
				// When have single element in array
				result = arr[0];
			}
			else
			{
				int sum = 0;
				// Assign first element of array to all auxiliary variable
				int currentMax = arr[0];
				int max = currentMax;
				int currentMin = currentMax;
				int min = currentMax;
				// Sum of array elements
				for (int i = 0; i < n; ++i)
				{
					sum = sum + arr[i];
				}
				// Execute the loop from counter 1 to n 
				for (int i = 1; i < n; ++i)
				{
					// Minimum subarray sum
					currentMin = this.minValue(currentMin + arr[i], arr[i]);
					min = this.minValue(min, currentMin);
					// Maximum subarray sum
					currentMax = this.maxValue(currentMax + arr[i], arr[i]);
					max = this.maxValue(max, currentMax);
				}
				if (min == sum)
				{
					result = max;
				}
				else
				{
					result = this.maxValue(max, sum - min);
				}
			}
		}
		// Display given array
		this.printArray(arr, n);
		// Display calculated result
		Console.Write("\n Result : " + result + " \n");
	}
	public static void Main(String[] args)
	{
		CircularSubarray task = new CircularSubarray();
		// Given array elements
		int[] arr1 = {
			2 , 3 , -2 , 3 , 4 , -3 , -6 , 2 , -1
		};
		int[] arr2 = {
			-5 , 1 , 2 , 3
		};
		int[] arr3 = {
			1 , 2 , 4
		};
		int[] arr4 = {
			5 , -3 , -1 , -4 , 4
		};
		// Test A
		// Get the number of elements
		int n = arr1.Length;
		// [2, 3,-2, 3 , 4, 2, -1]
		// Result 11
		task.maximumCircularSum(arr1, n);
		// Test B
		// Get the number of elements
		n = arr2.Length;
		// [1, 2, 3]
		// Result 6
		task.maximumCircularSum(arr2, n);
		// Test C
		// Get the number of elements
		n = arr3.Length;
		// [1, 2, 4]
		// Result 7
		task.maximumCircularSum(arr3, n);
		// Test D
		// Get the number of elements
		n = arr4.Length;
		// [5, 4]
		// Result 9
		task.maximumCircularSum(arr4, n);
	}
}

Output

 2 3 -2 3 4 -3 -6 2 -1
 Result : 11
 -5 1 2 3
 Result : 6
 1 2 4
 Result : 7
 5 -3 -1 -4 4
 Result : 9
package main
import "fmt"
/*
    Go Program for
    Maximum circular subarray sum solution
*/
// 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
}
// Returns the manimum value of given two numbers
func minValue(a, b int) int {
	if a < b {
		return a
	}
	return b
}
func maximumCircularSum(arr[] int, n int) {
	var result int = 0
	if n > 1 {
		if n == 1 {
			// When have single element in array
			result = arr[0]
		} else {
			var sum int = 0
			// Assign first element of array to all auxiliary variable
			var currentMax int = arr[0]
			var max int = currentMax
			var currentMin int = currentMax
			var min int = currentMax
			// Sum of array elements
			for i := 0 ; i < n ; i++ {
				sum = sum + arr[i]
			}
			// Execute the loop from counter 1 to n 
			for i := 1 ; i < n ; i++ {
				// Minimum subarray sum
				currentMin = minValue(currentMin + arr[i], arr[i])
				min = minValue(min, currentMin)
				// Maximum subarray sum
				currentMax = maxValue(currentMax + arr[i], arr[i])
				max = maxValue(max, currentMax)
			}
			if min == sum {
				result = max
			} else {
				result = maxValue(max, sum - min)
			}
		}
	}
	// Display given array
	printArray(arr, n)
	// Display calculated result
	fmt.Print("\n Result : ", result, " \n")
}
func main() {
	
	// Given array elements
	var arr1 = [] int { 2,3,-2,3,4,-3,-6,2,-1}
	var arr2 = [] int { -5, 1, 2, 3}
	var arr3 = [] int { 1,2,4 }
	var arr4 = [] int { 5,-3,-1,-4,4}
	// Test A
	// Get the number of elements
	var n int = len(arr1)
	// [2, 3,-2, 3 , 4, 2, -1]
	// Result 11
	maximumCircularSum(arr1, n)
	// Test B
	// Get the number of elements
	n = len(arr2)
	// [1, 2, 3]
	// Result 6
	maximumCircularSum(arr2, n)
	// Test C
	// Get the number of elements
	n = len(arr3)
	// [1, 2, 4]
	// Result 7
	maximumCircularSum(arr3, n)
	// Test D
	// Get the number of elements
	n = len(arr4)
	// [5, 4]
	// Result 9
	maximumCircularSum(arr4, n)
}

Output

 2 3 -2 3 4 -3 -6 2 -1
 Result : 11
 -5 1 2 3
 Result : 6
 1 2 4
 Result : 7
 5 -3 -1 -4 4
 Result : 9
<?php
/*
    Php Program for
    Maximum circular subarray sum solution
*/
class CircularSubarray
{
	// 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;
	}
	// Returns the manimum value of given two numbers
	public	function minValue($a, $b)
	{
		if ($a < $b)
		{
			return $a;
		}
		return $b;
	}
	public	function maximumCircularSum($arr, $n)
	{
		$result = 0;
		if ($n > 1)
		{
			if ($n == 1)
			{
				// When have single element in array
				$result = $arr[0];
			}
			else
			{
				$sum = 0;
				// Assign first element of array to all auxiliary variable
				$currentMax = $arr[0];
				$max = $currentMax;
				$currentMin = $currentMax;
				$min = $currentMax;
				// Sum of array elements
				for ($i = 0; $i < $n; ++$i)
				{
					$sum = $sum + $arr[$i];
				}
				// Execute the loop from counter 1 to n 
				for ($i = 1; $i < $n; ++$i)
				{
					// Minimum subarray sum
					$currentMin = $this->minValue(
                      $currentMin + $arr[$i], $arr[$i]);
					$min = $this->minValue($min, $currentMin);
					// Maximum subarray sum
					$currentMax = $this->maxValue(
                      $currentMax + $arr[$i], $arr[$i]);
					$max = $this->maxValue($max, $currentMax);
				}
				if ($min == $sum)
				{
					$result = $max;
				}
				else
				{
					$result = $this->maxValue($max, $sum - $min);
				}
			}
		}
		// Display given array
		$this->printArray($arr, $n);
		// Display calculated result
		echo("\n Result : ".$result.
			" \n");
	}
}

function main()
{
	$task = new CircularSubarray();
	// Given array elements
	$arr1 = array(2, 3, -2, 3, 4, -3, -6, 2, -1);
	$arr2 = array(-5, 1, 2, 3);
	$arr3 = array(1, 2, 4);
	$arr4 = array(5, -3, -1, -4, 4);
	// Test A
	// Get the number of elements
	$n = count($arr1);
	// [2, 3,-2, 3 , 4, 2, -1]
	// Result 11
	$task->maximumCircularSum($arr1, $n);
	// Test B
	// Get the number of elements
	$n = count($arr2);
	// [1, 2, 3]
	// Result 6
	$task->maximumCircularSum($arr2, $n);
	// Test C
	// Get the number of elements
	$n = count($arr3);
	// [1, 2, 4]
	// Result 7
	$task->maximumCircularSum($arr3, $n);
	// Test D
	// Get the number of elements
	$n = count($arr4);
	// [5, 4]
	// Result 9
	$task->maximumCircularSum($arr4, $n);
}
main();

Output

 2 3 -2 3 4 -3 -6 2 -1
 Result : 11
 -5 1 2 3
 Result : 6
 1 2 4
 Result : 7
 5 -3 -1 -4 4
 Result : 9
/*
    Node JS Program for
    Maximum circular subarray sum solution
*/
class CircularSubarray
{
	// 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;
	}
	// Returns the manimum value of given two numbers
	minValue(a, b)
	{
		if (a < b)
		{
			return a;
		}
		return b;
	}
	maximumCircularSum(arr, n)
	{
		var result = 0;
		if (n > 1)
		{
			if (n == 1)
			{
				// When have single element in array
				result = arr[0];
			}
			else
			{
				var sum = 0;
				// Assign first element of array to all auxiliary variable
				var currentMax = arr[0];
				var max = currentMax;
				var currentMin = currentMax;
				var min = currentMax;
				// Sum of array elements
				for (var i = 0; i < n; ++i)
				{
					sum = sum + arr[i];
				}
				// Execute the loop from counter 1 to n 
				for (var i = 1; i < n; ++i)
				{
					// Minimum subarray sum
					currentMin = this.minValue(
                      currentMin + arr[i], arr[i]);
					min = this.minValue(min, currentMin);
					// Maximum subarray sum
					currentMax = this.maxValue(
                      currentMax + arr[i], arr[i]);
					max = this.maxValue(max, currentMax);
				}
				if (min == sum)
				{
					result = max;
				}
				else
				{
					result = this.maxValue(max, sum - min);
				}
			}
		}
		// Display given array
		this.printArray(arr, n);
		// Display calculated result
		process.stdout.write("\n Result : " + result + " \n");
	}
}

function main()
{
	var task = new CircularSubarray();
	// Given array elements
	var arr1 = [2, 3, -2, 3, 4, -3, -6, 2, -1];
	var arr2 = [-5, 1, 2, 3];
	var arr3 = [1, 2, 4];
	var arr4 = [5, -3, -1, -4, 4];
	// Test A
	// Get the number of elements
	var n = arr1.length;
	// [2, 3,-2, 3 , 4, 2, -1]
	// Result 11
	task.maximumCircularSum(arr1, n);
	// Test B
	// Get the number of elements
	n = arr2.length;
	// [1, 2, 3]
	// Result 6
	task.maximumCircularSum(arr2, n);
	// Test C
	// Get the number of elements
	n = arr3.length;
	// [1, 2, 4]
	// Result 7
	task.maximumCircularSum(arr3, n);
	// Test D
	// Get the number of elements
	n = arr4.length;
	// [5, 4]
	// Result 9
	task.maximumCircularSum(arr4, n);
}
main();

Output

 2 3 -2 3 4 -3 -6 2 -1
 Result : 11
 -5 1 2 3
 Result : 6
 1 2 4
 Result : 7
 5 -3 -1 -4 4
 Result : 9
#    Python 3 Program for
#    Maximum circular subarray sum solution
class CircularSubarray :
	#  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
	
	#  Returns the manimum value of given two numbers
	def minValue(self, a, b) :
		if (a < b) :
			return a
		
		return b
	
	def maximumCircularSum(self, arr, n) :
		result = 0
		if (n > 1) :
			if (n == 1) :
				#  When have single element in list
				result = arr[0]
			else :
				sum = 0
				#  Assign first element of list to all auxiliary variable
				currentMax = arr[0]
				max = currentMax
				currentMin = currentMax
				min = currentMax
				i = 0
				#  Sum of list elements
				while (i < n) :
					sum = sum + arr[i]
					i += 1
				
				i = 1
				#  Execute the loop from counter 1 to n 
				while (i < n) :
					#  Minimum sublist sum
					currentMin = self.minValue(
                      currentMin + arr[i], arr[i])
					min = self.minValue(min, currentMin)
					#  Maximum sublist sum
					currentMax = self.maxValue(
                      currentMax + arr[i], arr[i])
					max = self.maxValue(max, currentMax)
					i += 1
				
				if (min == sum) :
					result = max
				else :
					result = self.maxValue(max, sum - min)
				
			
		
		#  Display given list
		self.printArray(arr, n)
		#  Display calculated result
		print("\n Result : ", result ," ")
	

def main() :
	task = CircularSubarray()
	#  Given list elements
	arr1 = [2, 3, -2, 3, 4, -3, -6, 2, -1]
	arr2 = [-5, 1, 2, 3]
	arr3 = [1, 2, 4]
	arr4 = [5, -3, -1, -4, 4]
	#  Test A
	#  Get the number of elements
	n = len(arr1)
	#  [2, 3,-2, 3 , 4, 2, -1]
	#  Result 11
	task.maximumCircularSum(arr1, n)
	#  Test B
	#  Get the number of elements
	n = len(arr2)
	#  [1, 2, 3]
	#  Result 6
	task.maximumCircularSum(arr2, n)
	#  Test C
	#  Get the number of elements
	n = len(arr3)
	#  [1, 2, 4]
	#  Result 7
	task.maximumCircularSum(arr3, n)
	#  Test D
	#  Get the number of elements
	n = len(arr4)
	#  [5, 4]
	#  Result 9
	task.maximumCircularSum(arr4, n)

if __name__ == "__main__": main()

Output

  2  3  -2  3  4  -3  -6  2  -1
 Result :  11
  -5  1  2  3
 Result :  6
  1  2  4
 Result :  7
  5  -3  -1  -4  4
 Result :  9
#    Ruby Program for
#    Maximum circular subarray sum solution
class CircularSubarray 
	#  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

	#  Returns the manimum value of given two numbers
	def minValue(a, b) 
		if (a < b) 
			return a
		end

		return b
	end

	def maximumCircularSum(arr, n) 
		result = 0
		if (n > 1) 
			if (n == 1) 
				#  When have single element in array
				result = arr[0]
			else
 
				sum = 0
				#  Assign first element of array to all auxiliary variable
				currentMax = arr[0]
				max = currentMax
				currentMin = currentMax
				min = currentMax
				i = 0
				#  Sum of array elements
				while (i < n) 
					sum = sum + arr[i]
					i += 1
				end

				i = 1
				#  Execute the loop from counter 1 to n 
				while (i < n) 
					#  Minimum subarray sum
					currentMin = self.minValue(
                      currentMin + arr[i], arr[i])
					min = self.minValue(min, currentMin)
					#  Maximum subarray sum
					currentMax = self.maxValue(
                      currentMax + arr[i], arr[i])
					max = self.maxValue(max, currentMax)
					i += 1
				end

				if (min == sum) 
					result = max
				else
 
					result = self.maxValue(max, sum - min)
				end

			end

		end

		#  Display given array
		self.printArray(arr, n)
		#  Display calculated result
		print("\n Result : ", result ," \n")
	end

end

def main() 
	task = CircularSubarray.new()
	#  Given array elements
	arr1 = [2, 3, -2, 3, 4, -3, -6, 2, -1]
	arr2 = [-5, 1, 2, 3]
	arr3 = [1, 2, 4]
	arr4 = [5, -3, -1, -4, 4]
	#  Test A
	#  Get the number of elements
	n = arr1.length
	#  [2, 3,-2, 3 , 4, 2, -1]
	#  Result 11
	task.maximumCircularSum(arr1, n)
	#  Test B
	#  Get the number of elements
	n = arr2.length
	#  [1, 2, 3]
	#  Result 6
	task.maximumCircularSum(arr2, n)
	#  Test C
	#  Get the number of elements
	n = arr3.length
	#  [1, 2, 4]
	#  Result 7
	task.maximumCircularSum(arr3, n)
	#  Test D
	#  Get the number of elements
	n = arr4.length
	#  [5, 4]
	#  Result 9
	task.maximumCircularSum(arr4, n)
end

main()

Output

 2 3 -2 3 4 -3 -6 2 -1
 Result : 11 
 -5 1 2 3
 Result : 6 
 1 2 4
 Result : 7 
 5 -3 -1 -4 4
 Result : 9 
/*
    Scala Program for
    Maximum circular subarray sum solution
*/
class CircularSubarray()
{
	// 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;
	}
	// Returns the manimum value of given two numbers
	def minValue(a: Int, b: Int): Int = {
		if (a < b)
		{
			return a;
		}
		return b;
	}
	def maximumCircularSum(arr: Array[Int], n: Int): Unit = {
		var result: Int = 0;
		if (n > 1)
		{
			if (n == 1)
			{
				// When have single element in array
				result = arr(0);
			}
			else
			{
				var sum: Int = 0;
				// Assign first element of array to all auxiliary variable
				var currentMax: Int = arr(0);
				var max: Int = currentMax;
				var currentMin: Int = currentMax;
				var min: Int = currentMax;
				var i: Int = 0;
				// Sum of array elements
				while (i < n)
				{
					sum = sum + arr(i);
					i += 1;
				}
				i = 1;
				// Execute the loop from counter 1 to n 
				while (i < n)
				{
					// Minimum subarray sum
					currentMin = minValue(currentMin + arr(i), arr(i));
					min = minValue(min, currentMin);
					// Maximum subarray sum
					currentMax = maxValue(currentMax + arr(i), arr(i));
					max = maxValue(max, currentMax);
					i += 1;
				}
				if (min == sum)
				{
					result = max;
				}
				else
				{
					result = maxValue(max, sum - min);
				}
			}
		}
		// Display given array
		printArray(arr, n);
		// Display calculated result
		print("\n Result : " + result + " \n");
	}
}
object Main
{
	def main(args: Array[String]): Unit = {
		var task: CircularSubarray = new CircularSubarray();
		// Given array elements
		var arr1: Array[Int] = Array(2, 3, -2, 3, 4, -3, -6, 2, -1);
		var arr2: Array[Int] = Array(-5, 1, 2, 3);
		var arr3: Array[Int] = Array(1, 2, 4);
		var arr4: Array[Int] = Array(5, -3, -1, -4, 4);
		// Test A
		// Get the number of elements
		var n: Int = arr1.length;
		// [2, 3,-2, 3 , 4, 2, -1]
		// Result 11
		task.maximumCircularSum(arr1, n);
		// Test B
		// Get the number of elements
		n = arr2.length;
		// [1, 2, 3]
		// Result 6
		task.maximumCircularSum(arr2, n);
		// Test C
		// Get the number of elements
		n = arr3.length;
		// [1, 2, 4]
		// Result 7
		task.maximumCircularSum(arr3, n);
		// Test D
		// Get the number of elements
		n = arr4.length;
		// [5, 4]
		// Result 9
		task.maximumCircularSum(arr4, n);
	}
}

Output

 2 3 -2 3 4 -3 -6 2 -1
 Result : 11
 -5 1 2 3
 Result : 6
 1 2 4
 Result : 7
 5 -3 -1 -4 4
 Result : 9
import Foundation;
/*
    Swift 4 Program for
    Maximum circular subarray sum solution
*/
class CircularSubarray
{
	// 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;
	}
	// Returns the manimum value of given two numbers
	func minValue(_ a: Int, _ b: Int) -> Int
	{
		if (a < b)
		{
			return a;
		}
		return b;
	}
	func maximumCircularSum(_ arr: [Int], _ n: Int)
	{
		var result: Int = 0;
		if (n > 1)
		{
			if (n == 1)
			{
				// When have single element in array
				result = arr[0];
			}
			else
			{
				var sum: Int = 0;
				// Assign first element of array to all auxiliary variable
				var currentMax: Int = arr[0];
				var max: Int = currentMax;
				var currentMin: Int = currentMax;
				var min: Int = currentMax;
				var i: Int = 0;
				// Sum of array elements
				while (i < n)
				{
					sum = sum + arr[i];
					i += 1;
				}
				i = 1;
				// Execute the loop from counter 1 to n 
				while (i < n)
				{
					// Minimum subarray sum
					currentMin = self.minValue(currentMin + arr[i], arr[i]);
					min = self.minValue(min, currentMin);
					// Maximum subarray sum
					currentMax = self.maxValue(currentMax + arr[i], arr[i]);
					max = self.maxValue(max, currentMax);
					i += 1;
				}
				if (min == sum)
				{
					result = max;
				}
				else
				{
					result = self.maxValue(max, sum - min);
				}
			}
		}
		// Display given array
		self.printArray(arr, n);
		// Display calculated result
		print("\n Result : ", result ," ");
	}
}
func main()
{
	let task: CircularSubarray = CircularSubarray();
	// Given array elements
	let arr1: [Int] = [2, 3, -2, 3, 4, -3, -6, 2, -1];
	let arr2: [Int] = [-5, 1, 2, 3];
	let arr3: [Int] = [1, 2, 4];
	let arr4: [Int] = [5, -3, -1, -4, 4];
	// Test A
	// Get the number of elements
	var n: Int = arr1.count;
	// [2, 3,-2, 3 , 4, 2, -1]
	// Result 11
	task.maximumCircularSum(arr1, n);
	// Test B
	// Get the number of elements
	n = arr2.count;
	// [1, 2, 3]
	// Result 6
	task.maximumCircularSum(arr2, n);
	// Test C
	// Get the number of elements
	n = arr3.count;
	// [1, 2, 4]
	// Result 7
	task.maximumCircularSum(arr3, n);
	// Test D
	// Get the number of elements
	n = arr4.count;
	// [5, 4]
	// Result 9
	task.maximumCircularSum(arr4, n);
}
main();

Output

  2  3  -2  3  4  -3  -6  2  -1
 Result :  11
  -5  1  2  3
 Result :  6
  1  2  4
 Result :  7
  5  -3  -1  -4  4
 Result :  9
/*
    Kotlin Program for
    Maximum circular subarray sum solution
*/
class CircularSubarray
{
	// 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;
	}
	// Returns the manimum value of given two numbers
	fun minValue(a: Int, b: Int): Int
	{
		if (a < b)
		{
			return a;
		}
		return b;
	}
	fun maximumCircularSum(arr: Array < Int > , n: Int): Unit
	{
		var result: Int = 0;
		if (n > 1)
		{
			if (n == 1)
			{
				// When have single element in array
				result = arr[0];
			}
			else
			{
				var sum: Int = 0;
				// Assign first element of array to all auxiliary variable
				var currentMax: Int = arr[0];
				var max: Int = currentMax;
				var currentMin: Int = currentMax;
				var min: Int = currentMax;
				var i: Int = 0;
				// Sum of array elements
				while (i < n)
				{
					sum = sum + arr[i];
					i += 1;
				}
				i = 1;
				// Execute the loop from counter 1 to n 
				while (i < n)
				{
					// Minimum subarray sum
					currentMin = this.minValue(currentMin + arr[i], arr[i]);
					min = this.minValue(min, currentMin);
					// Maximum subarray sum
					currentMax = this.maxValue(currentMax + arr[i], arr[i]);
					max = this.maxValue(max, currentMax);
					i += 1;
				}
				if (min == sum)
				{
					result = max;
				}
				else
				{
					result = this.maxValue(max, sum - min);
				}
			}
		}
		// Display given array
		this.printArray(arr, n);
		// Display calculated result
		print("\n Result : " + result + " \n");
	}
}
fun main(args: Array < String > ): Unit
{
	val task: CircularSubarray = CircularSubarray();
	// Given array elements
	val arr1: Array < Int > = arrayOf(2, 3, -2, 3, 4, -3, -6, 2, -1);
	val arr2: Array < Int > = arrayOf(-5, 1, 2, 3);
	val arr3: Array < Int > = arrayOf(1, 2, 4);
	val arr4: Array < Int > = arrayOf(5, -3, -1, -4, 4);
	// Test A
	// Get the number of elements
	var n: Int = arr1.count();
	// [2, 3,-2, 3 , 4, 2, -1]
	// Result 11
	task.maximumCircularSum(arr1, n);
	// Test B
	// Get the number of elements
	n = arr2.count();
	// [1, 2, 3]
	// Result 6
	task.maximumCircularSum(arr2, n);
	// Test C
	// Get the number of elements
	n = arr3.count();
	// [1, 2, 4]
	// Result 7
	task.maximumCircularSum(arr3, n);
	// Test D
	// Get the number of elements
	n = arr4.count();
	// [5, 4]
	// Result 9
	task.maximumCircularSum(arr4, n);
}

Output

 2 3 -2 3 4 -3 -6 2 -1
 Result : 11
 -5 1 2 3
 Result : 6
 1 2 4
 Result : 7
 5 -3 -1 -4 4
 Result : 9


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