Mid/SeniorArrayPrefix Sum

Find the Smallest Balanced Index

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
Inputnums = [2,1,2]
Output1
At index 1, the left sum is 2 and the right product is 2, and no smaller index satisfies the condition.
Example 2
Inputnums = [2,8,2,2,5]
Output2
At index 2, the left sum is 2 + 8 = 10 and the right product is 2 * 5 = 10, and no smaller index satisfies the condition.

Constraints

  • 1 <= nums.length <= 10^5
  • 1 <= nums[i] <= 10^9

Asked at 2 companies

</>

Your Solution

(Ctrl/Cmd + Enter)

Switching Language

Loading template...

Loading...

Sign in to save your progress

AI code evaluation

Get a correctness verdict, missed edge cases, and complexity analysis of your solution.

Sign in to evaluate