Number of Ways to Split Array

Given a 0-indexed integer array nums of length n, a split at index i (0 <= i < n - 1) is valid when the sum of the first i + 1 elements is greater than or equal to the sum of the remaining elements. Return the number of valid splits.

30:00

Number of Ways to Split Array
medium
Free written solution
Topics
Companies

Given a 0-indexed integer array nums of length n, a split at index i (0 <= i < n - 1) is valid when the sum of the first i + 1 elements is greater than or equal to the sum of the remaining elements. Return the number of valid splits.

Example 1:
Input: {"nums":[10,4,-8,7]}
Output: 2
Constraints:
  • 2≤nums.length≤1052 \leq \text{nums.length} \leq 10^5

  • −105≤nums[i]≤105-10^5 \leq \text{nums}[i] \leq 10^5

Test Cases
Input (Stdin)
Expected Output (Stdout)
Execution Result

Run code to see results.

Input
arr ={"nums":[10,4,-8,7]}

Put every element on the right pan first: total = 13. Moving the split one step right shifts one element from the right pan to the left pan.

nums · split after index - · total = 13

10

0

4

1

-8

2

7

3

split positions checked

0
1
2

valid splits

0

Variables
VariableValue
total13
left0
ways0
DepthFunction Call
Stack empty
0/7