1991. Find the Middle Index in Array Easy
1/**
2 * [1991] Find the Middle Index in Array
3 *
4 * Given a 0-indexed integer array nums, find the leftmost middleIndex (i.e., the smallest amongst all the possible ones).
5 * A middleIndex is an index where nums[0] + nums[1] + ... + nums[middleIndex-1] == nums[middleIndex+1] + nums[middleIndex+2] + ... + nums[nums.length-1].
6 * If middleIndex == 0, the left side sum is considered to be 0. Similarly, if middleIndex == nums.length - 1, the right side sum is considered to be 0.
7 * Return the leftmost middleIndex that satisfies the condition, or -1 if there is no such index.
8 *
9 * Example 1:
10 *
11 * Input: nums = [2,3,-1,<u>8</u>,4]
12 * Output: 3
13 * Explanation: The sum of the numbers before index 3 is: 2 + 3 + -1 = 4
14 * The sum of the numbers after index 3 is: 4 = 4
15 *
16 * Example 2:
17 *
18 * Input: nums = [1,-1,<u>4</u>]
19 * Output: 2
20 * Explanation: The sum of the numbers before index 2 is: 1 + -1 = 0
21 * The sum of the numbers after index 2 is: 0
22 *
23 * Example 3:
24 *
25 * Input: nums = [2,5]
26 * Output: -1
27 * Explanation: There is no valid middleIndex.
28 *
29 *
30 * Constraints:
31 *
32 * 1 <= nums.length <= 100
33 * -1000 <= nums[i] <= 1000
34 *
35 *
36 * Note: This question is the same as 724: <a href="https://leetcode.com/problems/find-pivot-index/" target="_blank">https://leetcode.com/problems/find-pivot-index/</a>
37 *
38 */
39pub struct Solution {}
40
41// problem: https://leetcode.com/problems/find-the-middle-index-in-array/
42// discuss: https://leetcode.com/problems/find-the-middle-index-in-array/discuss/?currentPage=1&orderBy=most_votes&query=
43
44// submission codes start here
45
46impl Solution {
47 pub fn find_middle_index(nums: Vec<i32>) -> i32 {
48 0
49 }
50}
51
52// submission codes end
53
54#[cfg(test)]
55mod tests {
56 use super::*;
57
58 #[test]
59 fn test_1991() {
60 }
61}
62
Back
© 2025 bowen.ge All Rights Reserved.