Description:
Given an array nums of n integers, find the minimal absolute difference between the sum of the first part and the second part when the array is split at any index P, where 1 ≤ P < n.
Example 1:
Input: nums = [3, 1, 2, 4, 3] Output: 1
Example 2:
Input: nums = [5, 6, 2, 4, 1] Output: 4
Example 3:
Input: nums = [1, 2] Output: 1
Algorithmic Steps This problem is solved by iterating through the array and maintaining running sums:
- Compute the total sum of the array.
- Initialize
leftSumto 0 andminDiffto infinity. - Iterate through the array (except the last element):
- Add the current element to
leftSum. - Compute
rightSumastotalSum - leftSum. - Calculate the absolute difference between
leftSumandrightSum. - Update
minDiffif the current difference is smaller.
- Add the current element to
- Return
minDiffafter the loop.
Time and Space complexity:
- Time complexity:
O(n), wherenis the number of elements in the array. - Space complexity:
O(1).