Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
Skip to content

Commit 2d2fe48

Browse files
committed
Add solution #1991
1 parent bd73665 commit 2d2fe48

File tree

2 files changed

+37
-1
lines changed

2 files changed

+37
-1
lines changed

README.md

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# 1,672 LeetCode solutions in JavaScript
1+
# 1,673 LeetCode solutions in JavaScript
22

33
[https://leetcodejavascript.com](https://leetcodejavascript.com)
44

@@ -1526,6 +1526,7 @@
15261526
1985|[Find the Kth Largest Integer in the Array](./solutions/1985-find-the-kth-largest-integer-in-the-array.js)|Medium|
15271527
1986|[Minimum Number of Work Sessions to Finish the Tasks](./solutions/1986-minimum-number-of-work-sessions-to-finish-the-tasks.js)|Medium|
15281528
1987|[Number of Unique Good Subsequences](./solutions/1987-number-of-unique-good-subsequences.js)|Hard|
1529+
1991|[Find the Middle Index in Array](./solutions/1991-find-the-middle-index-in-array.js)|Easy|
15291530
1996|[The Number of Weak Characters in the Game](./solutions/1996-the-number-of-weak-characters-in-the-game.js)|Medium|
15301531
2000|[Reverse Prefix of Word](./solutions/2000-reverse-prefix-of-word.js)|Easy|
15311532
2011|[Final Value of Variable After Performing Operations](./solutions/2011-final-value-of-variable-after-performing-operations.js)|Easy|
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
/**
2+
* 1991. Find the Middle Index in Array
3+
* https://leetcode.com/problems/find-the-middle-index-in-array/
4+
* Difficulty: Easy
5+
*
6+
* Given a 0-indexed integer array nums, find the leftmost middleIndex (i.e., the smallest amongst
7+
* all the possible ones).
8+
*
9+
* A middleIndex is an index where nums[0] + nums[1] + ... +
10+
* nums[middleIndex-1] == nums[middleIndex+1] + nums[middleIndex+2] + ... + nums[nums.length-1].
11+
*
12+
* If middleIndex == 0, the left side sum is considered to be 0. Similarly, if
13+
* middleIndex == nums.length - 1, the right side sum is considered to be 0.
14+
*
15+
* Return the leftmost middleIndex that satisfies the condition, or -1 if there is no such index.
16+
*/
17+
18+
/**
19+
* @param {number[]} nums
20+
* @return {number}
21+
*/
22+
var findMiddleIndex = function(nums) {
23+
let left = 0;
24+
let right = nums.reduce((sum, n) => sum + n, 0);
25+
26+
for (let i = 0; i < nums.length; i++) {
27+
left += nums[i];
28+
right -= nums[i];
29+
if (left - nums[i] === right) {
30+
return i;
31+
}
32+
}
33+
34+
return -1;
35+
};

0 commit comments

Comments
 (0)