InferenceAI

Enter at least two characters to search.

Start free

Balance Point Index

easyArrays & Strings15 min10 XP

Return the smallest index ii such that the sum of the elements to the left of ii equals the sum of the elements to its right (the element at ii belongs to neither side). Return -1 if there is none.

Requirements

  • O(n)O(n) time with a running prefix sum.

Examples

EXAMPLE WALKTHROUGHmiddle

A guided view of the published example, not a trace of your code.

INPUT[[1,7,3,6,5,6]]
TASKbalance_point_index()
EXPECTED OUTPUT3

Start with the input values shown in this example.

Example 1 of 3

Input: nums = [1,7,3,6,5,6]

Output: 3

Explanation: middle

Input: nums = [1,2,3]

Output: -1

Explanation: none

Input: nums = [2,1,-1]

Output: 0

Explanation: first index

Hint 1

Right sum = total - left sum - nums[i].

Topics

DSAArrays & StringsPython