
Given an array of integers nums, calculate the pivot index of this array.
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.
If the index is on the left edge of the array, then the left sum is 0 because there are no elements to the left. This also applies to the right edge of the array.
Return the leftmost pivot index. If no such index exists, return -1.
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
Example 2:
Input: nums = [1,2,3]
Output: -1
Explanation:
There is no index that satisfies the conditions in the problem statement.
Example 3:
Input: nums = [2,1,-1]
Output: 0
Explanation:
The pivot index is 0.
Left sum = 0 (no elements to the left of index 0)
Right sum = nums[1] + nums[2] = 1 + -1 = 0
Explanation
Count all the numbers:
- First, we add up all the numbers on all the blocks and remember that total. We’ll need it later.
- Like saying: “Let’s see how many blocks we have in total.”
Look at each block, one by one:
- Now, we’re going to go down the line of blocks and look at each one.
- Like saying: “Now, let’s look at each block carefully.”
Check the balance:
- For each block, we’ll add up all the numbers on the blocks before it. We’ll call this the “left side” sum.
- Then, we’ll take the total sum we calculated earlier, subtract the number on the current block, and then subtract the “left side” sum. This gives us the “right side” sum.
- Like saying: “Let’s see if the blocks on the left side add up to the same number as the blocks on the right side.”
Is it the special block?
- If the “left side” sum is exactly the same as the “right side” sum, then this block is the special one! We’ll remember its position.
- Like saying: “If the left side and right side are equal, this block is the winner!”
Keep going or give up:
- If the sums aren’t equal, we move on to the next block and try again.
- If we go through all the blocks and don’t find a special one, we’ll say “no special block found.”
Function findSpecialBlock(blocks):
totalSum = 0
For each block in blocks:
totalSum = totalSum + number on block
leftSum = 0
For each block in blocks, and its position:
rightSum = totalSum - number on block - leftSum
If leftSum is equal to rightSum:
Return the position of this block (it's the special one!)
leftSum = leftSum + number on block
Return "no special block found"
Solutions
Python
class Solution:
def pivotIndex(self, nums: List[int]) -> int:
total_sum = sum(nums)
left_sum = 0
for i, num in enumerate(nums):
right_sum = total_sum - num - left_sum
if right_sum == left_sum:
return i
left_sum += num
return -1
Typescript
function pivotIndex(nums: number[]): number {
const totalSum = nums.reduce((acc, val) => acc + val, 0);
let leftSum = 0;
for (let i = 0; i < nums.length; i++) {
const rightSum = totalSum - nums[i] - leftSum;
if (rightSum === leftSum) {
return i;
}
leftSum += nums[i];
}
return -1;
}
Go
func pivotIndex(nums []int) int {
totalSum := 0
for _, num := range nums {
totalSum += num
}
leftSum := 0
for i, num := range nums {
rightSum := totalSum - num - leftSum
if leftSum == rightSum {
return i
}
leftSum += num
}
return -1
}
If you liked this content I’d appreciate an upvote or a comment. That helps me improve the quality of my posts as well as getting to know more about you, my dear reader.
Muchas gracias!
Follow me for more content like this.
X | PeakD | Rumble | YouTube | Linked In | GitHub | PayPal.me | Medium
Down below you can find other ways to tip my work.
BankTransfer: "710969000019398639", // CLABE
BAT: "0x33CD7770d3235F97e5A8a96D5F21766DbB08c875",
ETH: "0x33CD7770d3235F97e5A8a96D5F21766DbB08c875",
BTC: "33xxUWU5kjcPk1Kr9ucn9tQXd2DbQ1b9tE",
ADA: "addr1q9l3y73e82hhwfr49eu0fkjw34w9s406wnln7rk9m4ky5fag8akgnwf3y4r2uzqf00rw0pvsucql0pqkzag5n450facq8vwr5e",
DOT: "1rRDzfMLPi88RixTeVc2beA5h2Q3z1K1Uk3kqqyej7nWPNf",
DOGE: "DRph8GEwGccvBWCe4wEQsWsTvQvsEH4QKH",
DAI: "0x33CD7770d3235F97e5A8a96D5F21766DbB08c875"