VOOZH about

URL: https://www.geeksforgeeks.org/dsa/find-if-array-can-be-divided-into-two-subarrays-of-equal-sum/

⇱ Find if array can be divided into two subarrays of equal sum - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

Find if array can be divided into two subarrays of equal sum

Last Updated : 4 Jul, 2022

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.

Examples: 

Input:  arr = [6, 2, 3, 2, 1]
Output:  true
Explanation:  On removing element 2 at index 1,
the array gets divided into two subarrays [6]
 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:


Output
The array can be divided intotwo subarrays with equal sum
The two subarrays are - [ 6 ] [ 3 2 1 ] 
Comment
Article Tags:
Article Tags: