Given an integer array, partition it into two subarrays having the same sum of elements.

For example,

Input:  {6, -4, -3, 2, 3}
 
Output: The two subarrays are {6, -4} and {-3, 2, 3} having equal sum of 2
 
 
Input:  {6, -5, 2, -4, 1}
 
Output: The two subarrays are {} and {6, -5, 2, -4, 1} having equal sum of 0

Practice this problem

Please note that the problem specifically targets subarrays that are contiguous (i.e., occupy consecutive positions) and inherently maintains the order of elements.

 
A simple solution is to iterate the array and calculate the sum of the left and right subarray for each array element. The time complexity of this solution is O(n2), where n is the size of the input. Following is the C++, Java, and Python program that demonstrates it:

C++


Download  Run Code

Output:

6 -4
-3 2 3

Java


Download  Run Code

Output:

[6, -4]
[-3, 2, 3]

Python


Download  Run Code

Output:

(6, -4)
(-3, 2, 3)

We can also solve this problem in O(n) time and O(1) space. The idea is to preprocess the array and calculate the sum of all array elements. Then for each array element, we can calculate its right sum in O(1) time by using the following formula:

sum of right subarray = total sum – sum of elements so far

Following is the implementation of the above approach in C++, Java, and Python:

C++


Download  Run Code

Output:

6 -4
-3 2 3

Java


Download  Run Code

Output:

[6, -4]
[-3, 2, 3]

Python


Download  Run Code

Output:

(6, -4)
(-3, 2, 3)