# Split an array into two equal sum subarrays

Here given code implementation process.

``````/*
C Program for
Split an array into two equal sum subarrays
*/
#include <stdio.h>

void splitEqualSum(int arr[], int n)
{
// Define some useful resultant auxiliary variables
int sum = 0;
int auxiliary = 0;
int breakPoint = -1;
// Loop controlling variables
int i = 0;
int j = 0;
// Calculate sum of all elements
for (i = 0; i < n; ++i)
{
sum += arr[i];
}
// Find that two equal subarray exists in given array
for (i = 0; i < n && breakPoint == -1; ++i)
{
// Add current element into auxiliary variable
auxiliary += arr[i];
// Reduce current element
sum = sum - arr[i];
if (auxiliary == sum)
{
breakPoint = i + 1;
}
}
if (breakPoint != -1)
{
for (i = 0; i < n; ++i)
{
if (breakPoint == i)
{
printf("\n");
}
printf(" %d", arr[i]);
}
}
else
{
printf("\n No equal subarrays sum \n");
}
}
int main()
{
int arr[] = {
6 , 2 , -4 , 3 , 2 , 1 , 1 , 6 , 1 , 2 , -1 , 3
};
// Get the size
int n = sizeof(arr) / sizeof(arr[0]);
splitEqualSum(arr, n);
return 0;
}``````

#### Output

`````` 6 2 -4 3 2 1 1
6 1 2 -1 3``````
``````/*
Java Program for
Split an array into two equal sum subarrays
*/
public class Partition
{
// Splitting an array into two subclasses that are equal in sum
public void splitEqualSum(int[] arr, int n)
{
// Define some useful resultant auxiliary variables
int sum = 0;
int auxiliary = 0;
int breakPoint = -1;
// Loop controlling variables
int i = 0;
int j = 0;
// Calculate sum of all elements
for (i = 0; i < n; ++i)
{
sum += arr[i];
}
// Find that two equal subarray exists in given array
for (i = 0; i < n && breakPoint == -1; ++i)
{
// Add current element into auxiliary variable
auxiliary += arr[i];
// Reduce current element
sum = sum - arr[i];
if (auxiliary == sum)
{
breakPoint = i + 1;
}
}
if (breakPoint != -1)
{
for (i = 0; i < n; ++i)
{
if (breakPoint == i)
{
System.out.print("\n");
}
System.out.print("  " + arr[i] );
}
}
else
{
System.out.print("\n No equal subarrays sum \n");
}
}
public static void main(String[] args)
{
Partition task = new Partition();
int arr[] =
{
6 , 2 , -4 , 3 , 2 , 1 , 1 , 6 , 1 , 2 , -1 , 3
};
// Get the size
int n = arr.length;
task.splitEqualSum(arr, n);
}
}``````

#### Output

``````  6  2  -4  3  2  1  1
6  1  2  -1  3``````
``````// Include header file
#include <iostream>

using namespace std;
/*
C++ Program for
Split an array into two equal sum subarrays
*/
class Partition
{
public:
// Splitting an array into two subclasses that are equal in sum
void splitEqualSum(int arr[], int n)
{
// Define some useful resultant auxiliary variables
int sum = 0;
int auxiliary = 0;
int breakPoint = -1;
// Loop controlling variables
int i = 0;
int j = 0;
// Calculate sum of all elements
for (i = 0; i < n; ++i)
{
sum += arr[i];
}
// Find that two equal subarray exists in given array
for (i = 0; i < n && breakPoint == -1; ++i)
{
// Add current element into auxiliary variable
auxiliary += arr[i];
// Reduce current element
sum = sum - arr[i];
if (auxiliary == sum)
{
breakPoint = i + 1;
}
}
if (breakPoint != -1)
{
for (i = 0; i < n; ++i)
{
if (breakPoint == i)
{
cout << "\n";
}
cout << "  " << arr[i];
}
}
else
{
cout << "\n No equal subarrays sum \n";
}
}
};
int main()
{
Partition task = Partition();
int arr[] =
{
6 , 2 , -4 , 3 , 2 , 1 , 1 , 6 , 1 , 2 , -1 , 3
};
// Get the size
int n = sizeof(arr) / sizeof(arr[0]);
task.splitEqualSum(arr, n);
return 0;
}``````

#### Output

``````  6  2  -4  3  2  1  1
6  1  2  -1  3``````
``````// Include namespace system
using System;
/*
C# Program for
Split an array into two equal sum subarrays
*/
public class Partition
{
// Splitting an array into two subclasses that are equal in sum
public void splitEqualSum(int[] arr, int n)
{
// Define some useful resultant auxiliary variables
int sum = 0;
int auxiliary = 0;
int breakPoint = -1;
// Loop controlling variable
int i = 0;
// Calculate sum of all elements
for (i = 0; i < n; ++i)
{
sum += arr[i];
}
// Find that two equal subarray exists in given array
for (i = 0; i < n && breakPoint == -1; ++i)
{
// Add current element into auxiliary variable
auxiliary += arr[i];
// Reduce current element
sum = sum - arr[i];
if (auxiliary == sum)
{
breakPoint = i + 1;
}
}
if (breakPoint != -1)
{
for (i = 0; i < n; ++i)
{
if (breakPoint == i)
{
Console.Write("\n");
}
Console.Write("  " + arr[i]);
}
}
else
{
Console.Write("\n No equal subarrays sum \n");
}
}
public static void Main(String[] args)
{
Partition task = new Partition();
int[] arr = {
6 , 2 , -4 , 3 , 2 , 1 , 1 , 6 , 1 , 2 , -1 , 3
};
// Get the size
int n = arr.Length;
task.splitEqualSum(arr, n);
}
}``````

#### Output

``````  6  2  -4  3  2  1  1
6  1  2  -1  3``````
``````<?php
/*
Php Program for
Split an array into two equal sum subarrays
*/
class Partition
{
// Splitting an array into two subclasses that are equal in sum
public	function splitEqualSum(\$arr, \$n)
{
// Define some useful resultant auxiliary variables
\$sum = 0;
\$auxiliary = 0;
\$breakPoint = -1;
// Loop controlling variable
\$i = 0;
// Calculate sum of all elements
for (\$i = 0; \$i < \$n; ++\$i)
{
\$sum += \$arr[\$i];
}
// Find that two equal subarray exists in given array
for (\$i = 0; \$i < \$n && \$breakPoint == -1; ++\$i)
{
// Add current element into auxiliary variable
\$auxiliary += \$arr[\$i];
// Reduce current element
\$sum = \$sum - \$arr[\$i];
if (\$auxiliary == \$sum)
{
\$breakPoint = \$i + 1;
}
}
if (\$breakPoint != -1)
{
for (\$i = 0; \$i < \$n; ++\$i)
{
if (\$breakPoint == \$i)
{
echo "\n";
}
echo "  ". \$arr[\$i];
}
}
else
{
echo "\n No equal subarrays sum \n";
}
}
}

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

#### Output

``````  6  2  -4  3  2  1  1
6  1  2  -1  3``````
``````/*
Node Js Program for
Split an array into two equal sum subarrays
*/
class Partition
{
// Splitting an array into two subclasses that are equal in sum
splitEqualSum(arr, n)
{
// Define some useful resultant auxiliary variables
var sum = 0;
var auxiliary = 0;
var breakPoint = -1;
// Loop controlling variable
var i = 0;
// Calculate sum of all elements
for (i = 0; i < n; ++i)
{
sum += arr[i];
}
// Find that two equal subarray exists in given array
for (i = 0; i < n && breakPoint == -1; ++i)
{
// Add current element into auxiliary variable
auxiliary += arr[i];
// Reduce current element
sum = sum - arr[i];
if (auxiliary == sum)
{
breakPoint = i + 1;
}
}
if (breakPoint != -1)
{
for (i = 0; i < n; ++i)
{
if (breakPoint == i)
{
process.stdout.write("\n");
}
process.stdout.write("  " + arr[i]);
}
}
else
{
process.stdout.write("\n No equal subarrays sum \n");
}
}
}

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

#### Output

``````  6  2  -4  3  2  1  1
6  1  2  -1  3``````
``````#  Python 3 Program for
#  Split an array into two equal sum subarrays

class Partition :
#  Splitting an array into two subclasses that are equal in sum
def splitEqualSum(self, arr, n) :
#  Define some useful resultant auxiliary variables
sum = 0
auxiliary = 0
breakPoint = -1
#  Loop controlling variable
i = 0
#  Calculate sum of all elements
while (i < n) :
sum += arr[i]
i += 1

i = 0
#  Find that two equal subarray exists in given array
while (i < n and breakPoint == -1) :
#  Add current element into auxiliary variable
auxiliary += arr[i]
#  Reduce current element
sum = sum - arr[i]
if (auxiliary == sum) :
breakPoint = i + 1

i += 1

if (breakPoint != -1) :
i = 0
while (i < n) :
if (breakPoint == i) :
print(end = "\n")

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

else :
print("\n No equal subarrays sum ")

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

if __name__ == "__main__": main()``````

#### Output

``````   6   2   -4   3   2   1   1
6   1   2   -1   3``````
``````#  Ruby Program for
#  Split an array into two equal sum subarrays

class Partition
#  Splitting an array into two subclasses that are equal in sum
def splitEqualSum(arr, n)
#  Define some useful resultant auxiliary variables
sum = 0
auxiliary = 0
breakPoint = -1
#  Loop controlling variable
i = 0
#  Calculate sum of all elements
while (i < n)
sum += arr[i]
i += 1
end

i = 0
#  Find that two equal subarray exists in given array
while (i < n && breakPoint == -1)
#  Add current element into auxiliary variable
auxiliary += arr[i]
#  Reduce current element
sum = sum - arr[i]
if (auxiliary == sum)
breakPoint = i + 1
end

i += 1
end

if (breakPoint != -1)
i = 0
while (i < n)
if (breakPoint == i)
print("\n")
end

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

else
print("\n No equal subarrays sum \n")
end

end

end

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

main()``````

#### Output

``````  6  2  -4  3  2  1  1
6  1  2  -1  3``````
``````/*
Scala Program for
Split an array into two equal sum subarrays
*/
class Partition
{
// Splitting an array into two subclasses that are equal in sum
def splitEqualSum(arr: Array[Int], n: Int): Unit = {
// Define some useful resultant auxiliary variables
var sum: Int = 0;
var auxiliary: Int = 0;
var breakPoint: Int = -1;
// Loop controlling variable
var i: Int = 0;
// Calculate sum of all elements
while (i < n)
{
sum += arr(i);
i += 1;
}
i = 0;
// Find that two equal subarray exists in given array
while (i < n && breakPoint == -1)
{
// Add current element into auxiliary variable
auxiliary += arr(i);
// Reduce current element
sum = sum - arr(i);
if (auxiliary == sum)
{
breakPoint = i + 1;
}
i += 1;
}
if (breakPoint != -1)
{
i = 0;
while (i < n)
{
if (breakPoint == i)
{
print("\n");
}
print("  " + arr(i));
i += 1;
}
}
else
{
print("\n No equal subarrays sum \n");
}
}
}
object Main
{
def main(args: Array[String]): Unit = {
var task: Partition = new Partition();
var arr: Array[Int] = Array(6, 2, -4, 3, 2, 1, 1, 6, 1, 2, -1, 3);
// Get the size
var n: Int = arr.length;
task.splitEqualSum(arr, n);
}
}``````

#### Output

``````  6  2  -4  3  2  1  1
6  1  2  -1  3``````
``````/*
Swift 4 Program for
Split an array into two equal sum subarrays
*/
class Partition
{
// Splitting an array into two subclasses that are equal in sum
func splitEqualSum(_ arr: [Int], _ n: Int)
{
// Define some useful resultant auxiliary variables
var sum: Int = 0;
var auxiliary: Int = 0;
var breakPoint: Int = -1;
// Loop controlling variable
var i: Int = 0;
// Calculate sum of all elements
while (i < n)
{
sum += arr[i];
i += 1;
}
i = 0;
// Find that two equal subarray exists in given array
while (i < n && breakPoint == -1)
{
// Add current element into auxiliary variable
auxiliary += arr[i];
// Reduce current element
sum = sum - arr[i];
if (auxiliary == sum)
{
breakPoint = i + 1;
}
i += 1;
}
if (breakPoint  != -1)
{
i = 0;
while (i < n)
{
if (breakPoint == i)
{
print(terminator: "\n");
}
print("  ", arr[i], terminator: "");
i += 1;
}
}
else
{
print("\n No equal subarrays sum ");
}
}
}
func main()
{
let task: Partition = Partition();
let arr: [Int] = [6, 2, -4, 3, 2, 1, 1, 6, 1, 2, -1, 3];
// Get the size
let n: Int = arr.count;
task.splitEqualSum(arr, n);
}
main();``````

#### Output

``````   6   2   -4   3   2   1   1
6   1   2   -1   3``````
``````/*
Kotlin Program for
Split an array into two equal sum subarrays
*/
class Partition
{
// Splitting an array into two subclasses that are equal in sum
fun splitEqualSum(arr: Array <Int> , n: Int): Unit
{
// Define some useful resultant auxiliary variables
var sum: Int = 0;
var auxiliary: Int = 0;
var breakPoint: Int = -1;
// Loop controlling variable
var i: Int = 0;
// Calculate sum of all elements
while (i < n)
{
sum += arr[i];
i += 1;
}
i = 0;
// Find that two equal subarray exists in given array
while (i < n && breakPoint == -1)
{
// Add current element into auxiliary variable
auxiliary += arr[i];
// Reduce current element
sum = sum - arr[i];
if (auxiliary == sum)
{
breakPoint = i + 1;
}
i += 1;
}
if (breakPoint != -1)
{
i = 0;
while (i < n)
{
if (breakPoint == i)
{
print("\n");
}
print("  " + arr[i]);
i += 1;
}
}
else
{
print("\n No equal subarrays sum \n");
}
}
}
fun main(args: Array < String > ): Unit
{
var task: Partition = Partition();
var arr: Array <Int> = arrayOf(6, 2, -4, 3, 2, 1, 1, 6, 1, 2, -1, 3);
// Get the size
var n: Int = arr.count();
task.splitEqualSum(arr, n);
}``````

#### Output

``````  6  2  -4  3  2  1  1
6  1  2  -1  3``````

## Comment

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

© 2022, kalkicode.com, All rights reserved