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.
GeeksforGeeks has prepared a complete interview preparation course with premium videos, theory, practice problems, TA support and many more features. Please refer Placement 100 for details
- Find the sums for which an array can be divided into sub-arrays of equal sum
- Find an element which divides the array in two subarrays with equal product
- Check if an array of 1s and 2s can be divided into 2 parts with equal sum
- Split array in three equal sum subarrays
- Split an array into two equal Sum subarrays
- Split array to three subarrays such that sum of first and third subarray is equal and maximum
- Find remainder of array multiplication divided by n
- Number of ways to select equal sized subarrays from two arrays having atleast K equal pairs of elements
- Find an array of size N having exactly K subarrays with sum S
- Find an element in array such that sum of left array is equal to sum of right array
- Number of subarrays having sum exactly equal to k
- Number of subarrays with GCD equal to 1
- Number of subarrays such that XOR of one half is equal to the other
- Count subarrays with equal number of 1's and 0's
- Number of subarrays for which product and sum are equal