Split array in three equal sum subarrays

Here given code implementation process.

/*
   C Program for
   Split array in three equal sum subarrays
*/
#include <stdio.h>

//Splitting an array into three sum subarrays
void splitEqualSum(int arr[], int n)
{
	int status = 1;
	if (n < 3)
	{
		status = 0;
	}
	else
	{
		int sum = 0;
		int auxiliary = 0;
		// Loop controlling variables
		int i = 0;
		int j = 0;
		// Calculate sum of all elements
		for (i = 0; i < n; ++i)
		{
			sum += arr[i];
		}
		int point[2];
		point[0] = -1;
		point[1] = -1;
		if (sum % 3 == 0)
		{
			// Find that three equal subarray exists in given array
			for (i = 0; i < n && j < 2; ++i)
			{
				// Add current element into auxiliary variable
				auxiliary += arr[i];
				if (auxiliary == sum / 3)
				{
					point[j] = i + 1;
					auxiliary = 0;
					j++;
				}
			}
			if (j == 2)
			{
				// When equal three subarray exist
				j = 0;
				// Print the elements of subarray 
				for (i = 0; i < n; ++i)
				{
					if (j < 2 && point[j] == i)
					{
						printf("\n");
						j++;
					}
					printf(" %d", arr[i]);
				}
			}
			else
			{
				// When not possible to split array into three equal sum
				status = 0;
			}
		}
		else
		{
			status = 0;
		}
	}
	if (status != 1)
	{
		printf("\n Three subarray with equal sum are not possible \n");
	}
}
int main()
{
	int arr[] = {
		5 , 2 , 7 , -5 , 7 , -1 , 3 , 4 , 5
	};
	// Get the size
	int n = sizeof(arr) / sizeof(arr[0]);
	splitEqualSum(arr, n);
	return 0;
}

Output

 5 2 7 -5
 7 -1 3
 4 5
/*
    Java Program for
    Split array in three equal sum subarrays
*/
public class Partition
{
	// Splitting an array into three sum subarrays
	public void splitEqualSum(int[] arr, int n)
	{
		boolean status = true;
		if (n < 3)
		{
			status = false;
		}
		else
		{
			int sum = 0;
			int auxiliary = 0;
			// Loop controlling variables
			int i = 0;
			int j = 0;
			// Calculate sum of all elements
			for (i = 0; i < n; ++i)
			{
				sum += arr[i];
			}
			int[] point = new int[2];
			point[0] = -1;
			point[1] = -1;
			if (sum % 3 == 0)
			{
				// Find that three equal subarray exists in given array
				for (i = 0; i < n && j < 2; ++i)
				{
					// Add current element into auxiliary variable
					auxiliary += arr[i];
					if (auxiliary == sum / 3)
					{
						point[j] = i + 1;
                      	// Set zero sum
						auxiliary = 0;
						j++;
					}
				}
				if (j == 2)
				{
					// When equal three subarray exist
					j = 0;
					// Print the elements of subarray 
					for (i = 0; i < n; ++i)
					{
						if (j < 2 && point[j] == i)
						{
							System.out.print("\n");
							j++;
						}
						System.out.print(" " + arr[i]);
					}
				}
				else
				{
					// When not possible to split array into three equal sum
					status = false;
				}
			}
			else
			{
				status = false;
			}
		}
		if (status != true)
		{
			System.out.print("\n Three subarray with equal sum are not possible \n");
		}
	}
	public static void main(String[] args)
	{
		Partition task = new Partition();
		int []arr = {
			5 , 2 , 7 , -5 , 7 , -1 , 3 , 4 , 5
		};
		// Get the size
		int n = arr.length;
		task.splitEqualSum(arr, n);
	}
}

Output

 5 2 7 -5
 7 -1 3
 4 5
// Include header file
#include <iostream>

using namespace std;
/*
    C++ Program for
    Split array in three equal sum subarrays
*/
class Partition
{
	public:
		// Splitting an array into three sum subarrays
		void splitEqualSum(int arr[], int n)
		{
			bool status = true;
			if (n < 3)
			{
				status = false;
			}
			else
			{
				int sum = 0;
				int auxiliary = 0;
				// Loop controlling variables
				int i = 0;
				int j = 0;
				// Calculate sum of all elements
				for (i = 0; i < n; ++i)
				{
					sum += arr[i];
				}
				int point[2];
				point[0] = -1;
				point[1] = -1;
				if (sum % 3 == 0)
				{
					// Find that three equal subarray exists in given array
					for (i = 0; i < n && j < 2; ++i)
					{
						// Add current element into auxiliary variable
						auxiliary += arr[i];
						if (auxiliary == sum / 3)
						{
							point[j] = i + 1;
							// Set zero sum
							auxiliary = 0;
							j++;
						}
					}
					if (j == 2)
					{
						// When equal three subarray exist
						j = 0;
						// Print the elements of subarray
						for (i = 0; i < n; ++i)
						{
							if (j < 2 && point[j] == i)
							{
								cout << "\n";
								j++;
							}
							cout << " " << arr[i];
						}
					}
					else
					{
						// When not possible to split array into three equal sum
						status = false;
					}
				}
				else
				{
					status = false;
				}
			}
			if (status != true)
			{
				cout << "\n Three subarray with equal sum are not possible \n";
			}
		}
};
int main()
{
	Partition task = Partition();
	int arr[] = {
		5 , 2 , 7 , -5 , 7 , -1 , 3 , 4 , 5
	};
	// Get the size
	int n = sizeof(arr) / sizeof(arr[0]);
	task.splitEqualSum(arr, n);
	return 0;
}

Output

 5 2 7 -5
 7 -1 3
 4 5
// Include namespace system
using System;
/*
    C# Program for
    Split array in three equal sum subarrays
*/
public class Partition
{
	// Splitting an array into three sum subarrays
	public void splitEqualSum(int[] arr, int n)
	{
		Boolean status = true;
		if (n < 3)
		{
			status = false;
		}
		else
		{
			int sum = 0;
			int auxiliary = 0;
			// Loop controlling variables
			int i = 0;
			int j = 0;
			// Calculate sum of all elements
			for (i = 0; i < n; ++i)
			{
				sum += arr[i];
			}
			int[] point = new int[2];
			point[0] = -1;
			point[1] = -1;
			if (sum % 3 == 0)
			{
				// Find that three equal subarray exists in given array
				for (i = 0; i < n && j < 2; ++i)
				{
					// Add current element into auxiliary variable
					auxiliary += arr[i];
					if (auxiliary == sum / 3)
					{
						point[j] = i + 1;
						// Set zero sum
						auxiliary = 0;
						j++;
					}
				}
				if (j == 2)
				{
					// When equal three subarray exist
					j = 0;
					// Print the elements of subarray
					for (i = 0; i < n; ++i)
					{
						if (j < 2 && point[j] == i)
						{
							Console.Write("\n");
							j++;
						}
						Console.Write(" " + arr[i]);
					}
				}
				else
				{
					// When not possible to split array into three equal sum
					status = false;
				}
			}
			else
			{
				status = false;
			}
		}
		if (status != true)
		{
			Console.Write("\n Three subarray with equal sum are not possible \n");
		}
	}
	public static void Main(String[] args)
	{
		Partition task = new Partition();
		int[] arr = {
			5 , 2 , 7 , -5 , 7 , -1 , 3 , 4 , 5
		};
		// Get the size
		int n = arr.Length;
		task.splitEqualSum(arr, n);
	}
}

Output

 5 2 7 -5
 7 -1 3
 4 5
<?php
/*
    Php Program for
    Split array in three equal sum subarrays
*/
class Partition
{
	// Splitting an array into three sum subarrays
	public	function splitEqualSum( & $arr, $n)
	{
		$status = true;
		if ($n < 3)
		{
			$status = false;
		}
		else
		{
			$sum = 0;
			$auxiliary = 0;
			// Loop controlling variables
			$i = 0;
			$j = 0;
			// Calculate sum of all elements
			for ($i = 0; $i < $n; ++$i)
			{
				$sum += $arr[$i];
			}
			$point = array_fill(0, 2, 0);
			if ($sum % 3 == 0)
			{
				// Find that three equal subarray exists in given array
				for ($i = 0; $i < $n && $j < 2; ++$i)
				{
					// Add current element into auxiliary variable
					$auxiliary += $arr[$i];
					if ($auxiliary == intval($sum / 3))
					{
						$point[$j] = $i + 1;
						// Set zero sum
						$auxiliary = 0;
						$j++;
					}
				}
				if ($j == 2)
				{
					// When equal three subarray exist
					$j = 0;
					// Print the elements of subarray
					for ($i = 0; $i < $n; ++$i)
					{
						if ($j < 2 && $point[$j] == $i)
						{
							echo "\n";
							$j++;
						}
						echo " ". $arr[$i];
					}
				}
				else
				{
					// When not possible to split array into three equal sum
					$status = false;
				}
			}
			else
			{
				$status = false;
			}
		}
		if ($status != true)
		{
			echo "\n Three subarray with equal sum are not possible \n";
		}
	}
}

function main()
{
	$task = new Partition();
	$arr = array(5, 2, 7, -5, 7, -1, 3, 4, 5);
	// Get the size
	$n = count($arr);
	$task->splitEqualSum($arr, $n);
}
main();

Output

 5 2 7 -5
 7 -1 3
 4 5
/*
    Node Js Program for
    Split array in three equal sum subarrays
*/
class Partition
{
	// Splitting an array into three sum subarrays
	splitEqualSum(arr, n)
	{
		var status = true;
		if (n < 3)
		{
			status = false;
		}
		else
		{
			var sum = 0;
			var auxiliary = 0;
			// Loop controlling variables
			var i = 0;
			var j = 0;
			// Calculate sum of all elements
			for (i = 0; i < n; ++i)
			{
				sum += arr[i];
			}
			var point = Array(2).fill(0);
			if (sum % 3 == 0)
			{
				// Find that three equal subarray exists in given array
				for (i = 0; i < n && j < 2; ++i)
				{
					// Add current element into auxiliary variable
					auxiliary += arr[i];
					if (auxiliary == parseInt(sum / 3))
					{
						point[j] = i + 1;
						// Set zero sum
						auxiliary = 0;
						j++;
					}
				}
				if (j == 2)
				{
					// When equal three subarray exist
					j = 0;
					// Print the elements of subarray
					for (i = 0; i < n; ++i)
					{
						if (j < 2 && point[j] == i)
						{
							process.stdout.write("\n");
							j++;
						}
						process.stdout.write(" " + arr[i]);
					}
				}
				else
				{
					// When not possible to split array into three equal sum
					status = false;
				}
			}
			else
			{
				status = false;
			}
		}
		if (status != true)
		{
			process.stdout.write("\n Three subarray with equal sum are not possible \n");
		}
	}
}

function main()
{
	var task = new Partition();
	var arr = [5, 2, 7, -5, 7, -1, 3, 4, 5];
	// Get the size
	var n = arr.length;
	task.splitEqualSum(arr, n);
}
main();

Output

 5 2 7 -5
 7 -1 3
 4 5
#  Python 3 Program for
#  Split array in three equal sum subarrays

class Partition :
	#  Splitting an array into three sum subarrays
	def splitEqualSum(self, arr, n) :
		status = True
		if (n < 3) :
			status = False
		else :
			sum = 0
			auxiliary = 0
			#  Loop controlling variables
			i = 0
			j = 0
			#  Calculate sum of all elements
			while (i < n) :
				sum += arr[i]
				i += 1
			
			point = [0] * (2)
			if (sum % 3 == 0) :
				#  Find that three equal subarray exists in given array
				i = 0
				while (i < n and j < 2) :
					#  Add current element into auxiliary variable
					auxiliary += arr[i]
					if (auxiliary == int(sum / 3)) :
						point[j] = i + 1
						#  Set zero sum
						auxiliary = 0
						j += 1
					
					i += 1
				
				if (j == 2) :
					#  When equal three subarray exist
					j = 0
					#  Print the elements of subarray
					i = 0
					while (i < n) :
						if (j < 2 and point[j] == i) :
							print(end = "\n")
							j += 1
						
						print(" ", arr[i], end = "")
						i += 1
					
				else :
					#  When not possible to split array into three equal sum
					status = False
				
			else :
				status = False
			
		
		if (status != True) :
			print("\n Three subarray with equal sum are not possible ")
		
	

def main() :
	task = Partition()
	arr = [5, 2, 7, -5, 7, -1, 3, 4, 5]
	#  Get the size
	n = len(arr)
	task.splitEqualSum(arr, n)

if __name__ == "__main__": main()

Output

  5  2  7  -5
  7  -1  3
  4  5
#  Ruby Program for
#  Split array in three equal sum subarrays

class Partition 
	#  Splitting an array into three sum subarrays
	def splitEqualSum(arr, n) 
		status = true
		if (n < 3) 
			status = false
		else 
			sum = 0
			auxiliary = 0
			#  Loop controlling variables
			i = 0
			j = 0
			#  Calculate sum of all elements
			while (i < n) 
				sum += arr[i]
				i += 1
			end

			point = Array.new(2) {0}
			if (sum % 3 == 0) 
				#  Find that three equal subarray exists in given array
				i = 0
				while (i < n && j < 2) 
					#  Add current element into auxiliary variable
					auxiliary += arr[i]
					if (auxiliary == sum / 3) 
						point[j] = i + 1
						#  Set zero sum
						auxiliary = 0
						j += 1
					end

					i += 1
				end

				if (j == 2) 
					#  When equal three subarray exist
					j = 0
					#  Print the elements of subarray
					i = 0
					while (i < n) 
						if (j < 2 && point[j] == i) 
							print("\n")
							j += 1
						end

						print(" ", arr[i])
						i += 1
					end

				else 
					#  When not possible to split array into three equal sum
					status = false
				end

			else 
				status = false
			end

		end

		if (status != true) 
			print("\n Three subarray with equal sum are not possible \n")
		end

	end

end

def main() 
	task = Partition.new()
	arr = [5, 2, 7, -5, 7, -1, 3, 4, 5]
	#  Get the size
	n = arr.length
	task.splitEqualSum(arr, n)
end

main()

Output

 5 2 7 -5
 7 -1 3
 4 5
/*
    Scala Program for
    Split array in three equal sum subarrays
*/
class Partition
{
	// Splitting an array into three sum subarrays
	def splitEqualSum(arr: Array[Int], n: Int): Unit = {
		var status: Boolean = true;
		if (n < 3)
		{
			status = false;
		}
		else
		{
			var sum: Int = 0;
			var auxiliary: Int = 0;
			// Loop controlling variables
			var i: Int = 0;
			var j: Int = 0;
			// Calculate sum of all elements
			while (i < n)
			{
				sum += arr(i);
				i += 1;
			}
			var point: Array[Int] = Array.fill[Int](2)(0);
			if (sum % 3 == 0)
			{
				// Find that three equal subarray exists in given array
				i = 0;
				while (i < n && j < 2)
				{
					// Add current element into auxiliary variable
					auxiliary += arr(i);
					if (auxiliary == (sum / 3).toInt)
					{
						point(j) = i + 1;
						// Set zero sum
						auxiliary = 0;
						j += 1;
					}
					i += 1;
				}
				if (j == 2)
				{
					// When equal three subarray exist
					j = 0;
					// Print the elements of subarray
					i = 0;
					while (i < n)
					{
						if (j < 2 && point(j) == i)
						{
							print("\n");
							j += 1;
						}
						print(" " + arr(i));
						i += 1;
					}
				}
				else
				{
					// When not possible to split array into three equal sum
					status = false;
				}
			}
			else
			{
				status = false;
			}
		}
		if (status != true)
		{
			print("\n Three subarray with equal sum are not possible \n");
		}
	}
}
object Main
{
	def main(args: Array[String]): Unit = {
		var task: Partition = new Partition();
		var arr: Array[Int] = Array(5, 2, 7, -5, 7, -1, 3, 4, 5);
		// Get the size
		var n: Int = arr.length;
		task.splitEqualSum(arr, n);
	}
}

Output

 5 2 7 -5
 7 -1 3
 4 5
/*
    Swift 4 Program for
    Split array in three equal sum subarrays
*/
class Partition
{
	// Splitting an array into three sum subarrays
	func splitEqualSum(_ arr: [Int], _ n: Int)
	{
		var status: Bool = true;
		if (n < 3)
		{
			status = false;
		}
		else
		{
			var sum: Int = 0;
			var auxiliary: Int = 0;
			// Loop controlling variables
			var i: Int = 0;
			var j: Int = 0;
			// Calculate sum of all elements
			while (i < n)
			{
				sum += arr[i];
				i += 1;
			}
			var point: [Int] = Array(repeating: 0, count: 2);
			if (sum % 3 == 0)
			{
				// Find that three equal subarray exists in given array
				i = 0;
				while (i < n && j < 2)
				{
					// Add current element into auxiliary variable
					auxiliary += arr[i];
					if (auxiliary == sum / 3)
					{
						point[j] = i + 1;
						// Set zero sum
						auxiliary = 0;
						j += 1;
					}
					i += 1;
				}
				if (j == 2)
				{
					// When equal three subarray exist
					j = 0;
					// Print the elements of subarray
					i = 0;
					while (i < n)
					{
						if (j < 2 && point[j] == i)
						{
							print(terminator: "\n");
							j += 1;
						}
						print(" ", arr[i], terminator: "");
						i += 1;
					}
				}
				else
				{
					// When not possible to split array into three equal sum
					status = false;
				}
			}
			else
			{
				status = false;
			}
		}
		if (status  != true)
		{
			print("\n Three subarray with equal sum are not possible ");
		}
	}
}
func main()
{
	let task: Partition = Partition();
	let arr: [Int] = [5, 2, 7, -5, 7, -1, 3, 4, 5];
	// Get the size
	let n: Int = arr.count;
	task.splitEqualSum(arr, n);
}
main();

Output

  5  2  7  -5
  7  -1  3
  4  5
/*
    Kotlin Program for
    Split array in three equal sum subarrays
*/
class Partition
{
	// Splitting an array into three sum subarrays
	fun splitEqualSum(arr: Array < Int > , n: Int): Unit
	{
		var status: Boolean = true;
		if (n < 3)
		{
			status = false;
		}
		else
		{
			var sum: Int = 0;
			var auxiliary: Int = 0;
			// Loop controlling variables
			var i: Int = 0;
			var j: Int = 0;
			// Calculate sum of all elements
			while (i < n)
			{
				sum += arr[i];
				i += 1;
			}
			var point: Array < Int > = Array(2)
			{
				0
			};
			if (sum % 3 == 0)
			{
				// Find that three equal subarray exists in given array
				i = 0;
				while (i < n && j < 2)
				{
					// Add current element into auxiliary variable
					auxiliary += arr[i];
					if (auxiliary == sum / 3)
					{
						point[j] = i + 1;
						// Set zero sum
						auxiliary = 0;
						j += 1;
					}
					i += 1;
				}
				if (j == 2)
				{
					// When equal three subarray exist
					j = 0;
					// Print the elements of subarray
					i = 0;
					while (i < n)
					{
						if (j < 2 && point[j] == i)
						{
							print("\n");
							j += 1;
						}
						print(" " + arr[i]);
						i += 1;
					}
				}
				else
				{
					// When not possible to split array into three equal sum
					status = false;
				}
			}
			else
			{
				status = false;
			}
		}
		if (status != true)
		{
			print("\n Three subarray with equal sum are not possible \n");
		}
	}
}
fun main(args: Array < String > ): Unit
{
	var task: Partition = Partition();
	var arr: Array < Int > = arrayOf(5, 2, 7, -5, 7, -1, 3, 4, 5);
	// Get the size
	var n: Int = arr.count();
	task.splitEqualSum(arr, n);
}

Output

 5 2 7 -5
 7 -1 3
 4 5


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