Skip to content

Commit 9f439a3

Browse files
authored
1991. Find the Middle Index in Array
1 parent 94fb42b commit 9f439a3

File tree

1 file changed

+15
-0
lines changed

1 file changed

+15
-0
lines changed

1991.FindtheMiddleIndexInArray.kt

+15
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
//https://leetcode.com/problems/find-the-middle-index-in-array/description/
2+
class Solution {
3+
fun findMiddleIndex(nums: IntArray): Int {
4+
val sum = nums.sum()
5+
var leftSum = 0
6+
7+
for(i in nums.indices){
8+
if(leftSum == sum -leftSum - nums[i]){
9+
return i
10+
}
11+
leftSum += nums[i]
12+
}
13+
return -1
14+
}
15+
}

0 commit comments

Comments
 (0)