Split an array into two equal Sum subarrays
Split an array into two equal Sum subarrays
Given an array of integers arr, return true if it is possible to split it in two subarrays (without reordering the elements), such that the sum of the two subarrays are equal. If it is not possible then return false.
Examples:
Input: arr = [1, 2, 3, 4, 5, 5]
Output: true
Explanation: In the above example, we can divide the array into two subarrays with equal sum. The two subarrays are: [1, 2, 3, 4] and [5, 5]. The sum of both the subarrays are 10. Hence, the answer is true.
Input: arr = [4, 3, 2, 1]
Output: false
Explanation: In the above example, we cannot divide the array into two subarrays with equal sum. Hence, the answer is false.
Expected Time Complexity: O(n)
Expected Space Complexity: O(1)
class Solution {
public boolean canSplit(int arr[]) {
// code here
int totalsum=0;
int n=arr.length;
for(int i=0;i<arr.length;i++)
{
totalsum+=arr[i];
}
int prefixsum=0;
for(int i=0;i<n-1;i++)
{
prefixsum+=arr[i];
int rightsum=totalsum-prefixsum;
if(rightsum==prefixsum)
{
return true;
}
}
return false;
}
}
Comments
Post a Comment