easyArraysPattern: Prefix Sum
Balanced Subsegment 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 the elements from index 0 to i is equal to the sum of the elements from index i + 1 to the end of the array. Return true if such an index exists, and false otherwise.
Examples
Example 1:
Input:nums = [1, 2, 3, 6]
Output:true
Explanation: The sum of the first three elements (1+2+3=6) equals the sum of the last element (6).
Example 2:
Input:nums = [1, 1, 1, 1]
Output:true
Explanation: The sum of the first two elements (1+1=2) equals the sum of the last two elements (1+1=2).
Example 3:
Input:nums = [1, 2, 4]
Output:false
Explanation: No possible split exists where the sum of the left partition equals the sum of the right partition.
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 Subsegment 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.
