Find Pivot Index
2022-09-04T11:26:40 - Vicky Chhetri
Read Time:42 Second
Example 1:
Input: nums = [1,7,3,6,5,6] Output: 3 Explanation: The pivot index is 3. Left sum = nums[0] + nums[1] + nums[2] = 1 + 7 + 3 = 11 Right sum = nums[4] + nums[5] = 5 + 6 = 11 The pivot index is the index where the sum of all the numbers strictly to the left of the index is equal to the sum of all the numbers strictly to the index's right.
class Solution(object):
def pivotIndex(self, nums):
"""
:type nums: List[int]
:rtype: int
"""
PIndex=-1
for i in range(len(nums)):
pevotIndex=self.calculate(nums[0:i],nums[i+1:],i)
if pevotIndex==True:
return i
return PIndex
def calculate(self,a,b,k):
if(k==0):
if sum(b)==0:
return True
if sum(a)-sum(b)==0:
return True
return False
Wrong Answers 😂
