easyArraysPattern: Prefix Sum
Balanced Subarray Partitioning Solution
Problem Statement
Given an integer array nums, determine if there exists an index i (where 0 <= i < nums.length - 1) such that the sum of elements from index 0 to i is equal to the sum of elements from index i + 1 to the end of the array. Return true if such an index exists, otherwise return false.
Examples
Example 1:
Input:nums = [1, 2, 3, 6]
Output:true
Explanation: The sum of elements [1, 2, 3] is 6, which equals the remaining element [6].
Example 2:
Input:nums = [1, 2, 4]
Output:false
Explanation: No index i exists where the prefix sum equals the suffix sum.
Example 3:
Input:nums = [5, -2, 3]
Output:true
Explanation: The sum of [5, -2] is 3, which equals the remaining element [3].
Constraints
- 2 <= nums.length <= 10^5
- -10^4 <= nums[i] <= 10^4
Time: O(n) Space: O(1)
Master coding challenges related to Arrays and solve the Balanced Subarray Partitioning problem optimally.
Run, Test & Submit Code
Ready to practice this challenge? Launch our interactive compilation environment with compiler validation.
Solve on Interactive WorkspaceTested Solutions
No solution code is currently loaded.
Complete this code in the workspace editor.
