3862. Find the Smallest Balanced Index Medium

@problem@discussion
#Array#Prefix Sum



1/**
2 * [3862] Find the Smallest Balanced Index
3 *
4 * You are given an integer array nums.
5 * 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.
6 * 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.
7 * Return an integer denoting the smallest balanced index. If no balanced index exists, return -1.
8 *  
9 * <strong class="example">Example 1:
10 * <div class="example-block">
11 * Input: <span class="example-io">nums = [2,1,2]</span>
12 * Output: <span class="example-io">1</span>
13 * Explanation:
14 * For index i = 1:
15 * 
16 * 	Left sum = nums[0] = 2
17 * 	Right product = nums[2] = 2
18 * 	Since the left sum equals the right product, index 1 is balanced.
19 * 
20 * No smaller index satisfies the condition, so the answer is 1.
21 * </div>
22 * <strong class="example">Example 2:
23 * <div class="example-block">
24 * Input: <span class="example-io">nums = [2,8,2,2,5]</span>
25 * Output: <span class="example-io">2</span>
26 * Explanation:
27 * For index i = 2:
28 * 
29 * 	Left sum = 2 + 8 = 10
30 * 	Right product = 2 * 5 = 10
31 * 	Since the left sum equals the right product, index 2 is balanced.
32 * 
33 * No smaller index satisfies the condition, so the answer is 2.
34 * </div>
35 * <strong class="example">Example 3:
36 * <div class="example-block">
37 * Input: <span class="example-io">nums = [1]</span>
38 * Output: <span class="example-io">-1</span>
39 * For index i = 0:
40 * 
41 * 	The left side is empty, so the left sum is 0.
42 * 	The right side is empty, so the right product is 1.
43 * 	Since the left sum does not equal the right product, index 0 is not balanced.
44 * Therefore, no balanced index exists and the answer is -1.</div>
45 *  
46 * Constraints:
47 * 
48 * 	1 <= nums.length <= 10^5
49 * 	1 <= nums[i] <= 10^9
50 * 
51 */
52pub struct Solution {}
53
54// problem: https://leetcode.com/problems/find-the-smallest-balanced-index/
55// discuss: https://leetcode.com/problems/find-the-smallest-balanced-index/discuss/?currentPage=1&orderBy=most_votes&query=
56
57// submission codes start here
58
59impl Solution {
60    pub fn smallest_balanced_index(nums: Vec<i32>) -> i32 {
61        0
62    }
63}
64
65// submission codes end
66
67#[cfg(test)]
68mod tests {
69    use super::*;
70
71    #[test]
72    fn test_3862() {
73    }
74}
75

Back
© 2026 bowen.ge All Rights Reserved.