Find the Smallest Balanced Index Problem
Description
LeetCode Problem 3862.
You are given an integer array nums.
An index i is balanced if the sum of elements strictly to the left of i equals the product of elements strictly to the right of i.
If there are no elements to the left, the sum is considered as 0. Similarly, if there are no elements to the right, the product is considered as 1.
Return an integer denoting the smallest balanced index. If no balanced index exists, return -1.
Example 1:
Input: nums = [2,1,2]
Output: 1
Explanation:
For index i = 1:
- Left sum = nums[0] = 2
- Right product = nums[2] = 2
- Since the left sum equals the right product, index 1 is balanced.
No smaller index satisfies the condition, so the answer is 1.
Example 2:
Input: nums = [2,8,2,2,5]
Output: 2
Explanation:
For index i = 2:
- Left sum = 2 + 8 = 10
- Right product = 2 * 5 = 10
- Since the left sum equals the right product, index 2 is balanced.
No smaller index satisfies the condition, so the answer is 2.
Example 3:
Input: nums = [1]
Output: -1
For index i = 0:
- The left side is empty, so the left sum is 0.
- The right side is empty, so the right product is 1.
- Since the left sum does not equal the right product, index 0 is not balanced.
Therefore, no balanced index exists and the answer is -1.
Constraints:
1 <= nums.length <= 10^51 <= nums[i] <= 10^9
Solution
Approach: Scan from the Right with an Early Stop
Compute the total sum s, then go through the indices from right to left. At index i, remove nums[i] from s so that s is the sum to the left of i, and compare it with p, the product of everything to the right. If they are equal, i is balanced and we return it (the left sum strictly grows with i while the right product never grows, so at most one index can be balanced). Otherwise multiply p by nums[i]. All numbers are at least 1, so once p >= s the product can never come back down to the shrinking sum and we stop. This also keeps p from overflowing: it is only multiplied while it is below s, which is at most about 10^14.
Complexity: O(n) time and O(1) space.
class Solution:
def smallestBalancedIndex(self, nums: List[int]) -> int:
s = sum(nums)
p = 1
for i in range(len(nums) - 1, -1, -1):
s -= nums[i]
if s == p:
return i
p *= nums[i]
if p >= s:
break
return -1