Skip to content

Commit ffade38

Browse files
committed
1991. Find the Middle Index in Array: AC
1 parent 8016529 commit ffade38

File tree

2 files changed

+96
-0
lines changed

2 files changed

+96
-0
lines changed

src/solution/mod.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1499,3 +1499,4 @@ mod s1984_minimum_difference_between_highest_and_lowest_of_k_scores;
14991499
mod s1985_find_the_kth_largest_integer_in_the_array;
15001500
mod s1986_minimum_number_of_work_sessions_to_finish_the_tasks;
15011501
mod s1987_number_of_unique_good_subsequences;
1502+
mod s1991_find_the_middle_index_in_array;
Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
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

Comments
 (0)