How to optimally divide an array into two subarrays so that sum of elements in both are same ?
There are two methods used to split arrays,
- Given an array of integers greater than zero, find if it is possible to split it in two subarrays (without reordering the elements), such that the sum of the two subarrays is the same. For example,
Input : Arr[] = { 1 , 2 , 3 , 4 , 5 , 5 }
Output : { 1 2 3 4 }
{ 5 , 5 }
- Given an array of integers, find if it’s possible to remove exactly one integer from the array that divides the array into two subarrays with the same sum. For Example,
Input: arr = [6, 2, 3, 2, 1]
Output: true
- On removing element 2 at index 1, the array gets divided into two subarrays [6] and [3, 2, 1] having equal sum.
Sample Code in Java
Output
1 1
0 2