8000 feat: solved 1991. Find the Middle Index in Array Β· realtemirov/leetcode@ddfbddd Β· GitHub
[go: up one dir, main page]

Skip to content

Commit ddfbddd

Browse filesBrowse files
committed
feat: solved 1991. Find the Middle Index in Array
1 parent dc39aa1 commit ddfbddd

File tree

6 files changed

+91
-0
lines changed

6 files changed

+91
-0
lines changed
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
# 1991. Find the Middle Index in Array
2+
3+
🟩 Easy
4+
5+
## Solution
6+
7+
My Solution
8+
9+
```go
10+
func findMiddleIndex(nums []int) int {
11+
sum := 0
12+
for _, num := range nums {
13+
sum += num
14+
}
15+
< 8000 /div>
16+
left := 0
17+
for i, num := range nums {
18+
sum -= num
19+
if left == sum {
20+
return i
21+
}
22+
left += num
23+
}
24+
25+
return -1
26+
}
27+
```
28+
29+
![result](1991.png)
30+
31+
Leetcode: [link](https://leetcode.com/problems/find-the-middle-index-in-array/)

β€ŽProblems/1991/1991.png

31.9 KB
Loading

β€ŽProblems/1991/solution.go

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
package problem1991
2+
3+
func FindMiddleIndex(nums []int) int {
4+
sum := 0
5+
for _, num := range nums {
6+
sum += num
7+
}
8+
9+
left := 0
10+
for i, num := range nums {
11+
sum -= num
12+
if left == sum {
13+
return i
14+
}
15+
left += num
16+
}
17+
18+
return -1
19+
}

β€ŽProblems/1991/solution_test.go

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
package problem1991_test
2+
3+
import (
4+
"testing"
5+
6+
problem1991 "github.com/realtemirov/leetcode/Problems/1991"
7+
"github.com/stretchr/testify/require"
8+
)
9+
10+
func TestSolution(t *testing.T) {
11+
testCases := []struct {
12+
name string
13+
cases []int
14+
expected int
15+
}{
16+
{
17+
name: "Test 1",
18+
cases: []int{2, 3, -1, 8, 4},
19+
expected: 3,
20+
},
21+
{
22+
name: "Test 2",
23+
cases: []int{1, -1, 4},
24+
expected: 2,
25+
},
26+
{
27+
name: "Test 3",
28+
cases: []int{2, 5},
29+
expected: -1,
30+
},
31+
}
32+
33+
for _, tc := range testCases {
34+
t.Run(tc.name, func(t *testing.T) {
35+
result := problem1991.FindMiddleIndex(tc.cases)
36+
require.Equal(t, tc.expected, result, "expected: %v, result: %v", tc.expected, result)
37+
})
38+
}
39+
}

β€ŽProblems/README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,7 @@
8585
* 🟧 Medium - [1769. Minimum Number of Operations to Move All Balls to Each Box](<./1769/1769. Minimum Number of Operations to Move All Balls to Each Box.md>)
8686
* 🟩 Easy - [1822. Sign of the Product of an Array](<./1822/1822. Sign of the Product of an Array.md>)
8787
* 🟧 Medium - [1930. Unique Length-3 Palindromic Subsequences](<./1930/1930. Unique Length-3 Palindromic Subsequences.md>)
88+
* 🟩 Easy - [1991. Find the Middle Index in Array](<./1991/1991. Find the Middle Index in Array.md>)
8889
* 🟩 Easy - [2185. Counting Words With a Given Prefix](<./2185/2185. Counting Words With a Given Prefix.md>)
8990
* 🟩 Easy - [2235. Add Two Integers](<./2235/2235. Add Two Integers.md>)
9091
* 🟩 Easy - [2236. Root Equals Sum of Children](<./2236/2236. Root Equals Sum of Children.md>)

β€ŽSUMMARY.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -140,6 +140,7 @@
140140
* [1769. Minimum Number of Operations to Move All Balls to Each Box](<./Problems/1769/1769. Minimum Number of Operations to Move All Balls to Each Box.md>)
141141
* [1822. Sign of the Product of an Array](<./Problems/1822/1822. Sign of the Product of an Array.md>)
142142
* [1930. Unique Length-3 Palindromic Subsequences](<./Problems/1930/1930. Unique Length-3 Palindromic Subsequences.md>)
143+
* [1991. Find the Middle Index in Array](<./Problems/1991/1991. Find the Middle Index in Array.md>)
143144
* [2185. Counting Words With a Given Prefix](<./Problems/2185/2185. Counting Words With a Given Prefix.md>)
144145
* [2235. Add Two Integers](<./Problems/2235/2235. Add Two Integers.md>)
145146
* [2236. Root Equals Sum of Children](<./Problems/2236/2236. Root Equals Sum of Children.md>)

0 commit comments

Comments
Β (0)
0