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.
Input: arr = [6, 2, 3, 2, 1] Output: true Explanation: On removing element 2 at index 1, the array gets divided into two subarrays  and [3, 2, 1] having equal sum Input: arr = [6, 1, 3, 2, 5] Output: true Explanation: On removing element 3 at index 2, the array gets divided into two subarrays [6, 1] and [2, 5] having equal sum. Input: arr = [6, -2, -3, 2, 3] Output: true Explanation: On removing element 6 at index 0, the array gets divided into two sets  and [-2, -3, 2, 3] having equal sum Input: arr = [6, -2, 3, 2, 3] Output: false
A naive solution would be to consider all elements of the array and calculate their left and right sum and return true if left and right sum are found to be equal. The time complexity of this solution would be O(n2).
The efficient solution involves calculating sum of all elements of the array in advance. Then for each element of the array we can calculate its right sum in O(1) time by using total sum of the array elements minus sum of elements found so far. The time complexity of this solution would be O(n) and auxiliary space used by it will be O(1).
Below is the implementation of above approach:
The array can be divided into two subarrays with equal sum The two sets are -  [3 2 1]
This article is contributed by Aditya Goel. If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to email@example.com. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.
- Find the sums for which an array can be divided into sub-arrays of equal sum
- Check if an array of 1s and 2s can be divided into 2 parts with equal sum
- Number of times an array can be partitioned repetitively into two subarrays with equal sum
- Check if array can be divided into two sub-arrays such that their absolute difference is K
- Number of K's such that the given array can be divided into two sets satisfying the given conditions
- Check if a given array can be divided into pairs with even sum
- Check if an array can be divided into pairs whose sum is divisible by k
- Split an array into two equal Sum subarrays
- Minimize the sum of the squares of the sum of elements of each group the array is divided into
- Generate Array whose sum of all K-size subarrays divided by N leaves remainder X
- Check if a sorted array can be divided in pairs whose sum is k
- Partitioning into two contiguous element subarrays with equal sums
- Split an Array A into Subsets having equal Sum and sizes equal to elements of Array B
- Split array into two subarrays such that difference of their sum is minimum
- Check if two arrays can be made equal by reversing subarrays multiple times
- Check if an array can be split into subarrays with GCD exceeding K
- Number of ways to select equal sized subarrays from two arrays having atleast K equal pairs of elements
- Check if an array can be split into 3 subsequences of equal sum or not
- Count of Array elements to be divided by 2 to make at least K elements equal
- Find an element which divides the array in two subarrays with equal product