|
| 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 | + */ |
| 39 | +pub 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 | + |
| 46 | +impl Solution { |
| 47 | + pub fn find_middle_index(nums: Vec<i32>) -> i32 { |
| 48 | + let sum: i32 = nums.iter().sum(); |
| 49 | + |
| 50 | + let mut sum_cur: i32 = 0; |
| 51 | + |
| 52 | + for (idx, num) in nums.into_iter().enumerate() { |
| 53 | + if sum_cur == sum - sum_cur - num { |
| 54 | + return idx as i32; |
| 55 | + } |
| 56 | + sum_cur += num; |
| 57 | + } |
| 58 | + |
| 59 | + -1 |
| 60 | + } |
| 61 | +} |
| 62 | + |
| 63 | +// submission codes end |
| 64 | + |
| 65 | +#[cfg(test)] |
| 66 | +mod tests { |
| 67 | + use super::*; |
| 68 | + |
| 69 | + #[test] |
| 70 | + fn test_1991_example_1() { |
| 71 | + let nums = vec![2, 3, -1, 8, 4]; |
| 72 | + |
| 73 | + let result = 3; |
| 74 | + |
| 75 | + assert_eq!(Solution::find_middle_index(nums), result); |
| 76 | + } |
| 77 | + |
| 78 | + #[test] |
| 79 | + fn test_1991_example_2() { |
| 80 | + let nums = vec![1, -1, 4]; |
| 81 | + |
| 82 | + let result = 2; |
| 83 | + |
| 84 | + assert_eq!(Solution::find_middle_index(nums), result); |
| 85 | + } |
| 86 | + |
| 87 | + #[test] |
| 88 | + fn test_1991_example_3() { |
| 89 | + let nums = vec![2, 5]; |
| 90 | + |
| 91 | + let result = -1; |
| 92 | + |
| 93 | + assert_eq!(Solution::find_middle_index(nums), result); |
| 94 | + } |
| 95 | +} |
0 commit comments