-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfindMiddleIndex.py
More file actions
25 lines (18 loc) · 835 Bytes
/
findMiddleIndex.py
File metadata and controls
25 lines (18 loc) · 835 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
"""
Given a 0-indexed integer array nums, find the leftmost middleIndex (i.e., the smallest amongst all the possible ones).
A middleIndex is an index where nums[0] + nums[1] + ... + nums[middleIndex-1] == nums[middleIndex+1] +
nums[middleIndex+2] + ... + nums[nums.length-1].
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.
Return the leftmost middleIndex that satisfies the condition, or -1 if there is no such index.
"""
class Solution:
def findMiddleIndex(self, nums: list[int]) -> int:
left_sum = 0
right_sum = sum(nums)
for i in range(len(nums)):
right_sum -= nums[i]
if left_sum == right_sum:
return i
left_sum += nums[i]
return -1