0% completed
Problem Statement
Given an integer array nums, determine the pivot index of this array.
The pivot index divides the array in such a way that the sum of the numbers on its left side is exactly equal to the sum of the numbers on its right side.
If the index is at the beginning, the sum to its left is considered zero, similarly for an index at the end.
Return the leftmost pivot index. If no such index exists, return -1.
Examples
-
Example 1:
- Input:
[1, 2, 3, 4, 3, 2, 1] - Expected Output:
3 - Justification: The sum of the numbers to the left of index
3(1 + 2 + 3) is6, and the sum to its right (3 + 2 + 1) is also6.
- Input:
-
Example 2:
- Input:
[2, 1, 3] - Expected Output:
-1 - Justification: There is no pivot index that exists in the given array.
- Input:
-
Example 3:
- Input:
[1, 100, 50, -51, 1, 1] - Expected Output:
1 - Justification: The sum to the left of index
1is1, and the sum to its right (50 + (-51) + 1 + 1) is1.
- Input:
Solution
To solve this problem, we'll employ a two-step approach to ensure efficiency and simplicity. Initially, we calculate the total sum of all array elements. Knowing the total, we can iterate through the array, maintaining a running sum of elements to the left of the current index. At any point, if the left sum is equal to the total minus the left sum minus the current element, we've found our pivot index. This method is effective because it only requires a single pass through the array after calculating the total sum, making it efficient in terms of both time and space.
The reason this approach is particularly appealing is its simplicity and the fact that it leverages the arithmetic property of sums. This allows for quick determination of the pivot index without needing complex data structures or multiple passes through the array, making it an ideal choice for this problem.
Step-by-step algorithm:
- Calculate the total sum of the array elements.
- Initialize a variable to keep track of the sum of elements to the left of the current index (
leftSum) and set it to0. - Iterate through the array:
- At each index, check if
leftSumis equal to the total sum minusleftSumminus the current element. If true, return the current index as the pivot index. - Otherwise, add the current element to
leftSumand continue.
- At each index, check if
- If no pivot index is found during the iteration, return
-1.
Algorithm Walkthrough
Using the input [1, 2, 3, 4, 3, 2, 1]:
Step 1: Calculate the Total Sum of the Array
- Initialize the
totalSumvariable to0. - Add each element in the array to
totalSum:- After adding
1,totalSumis1. - After adding
2,totalSumis3. - After adding
3,totalSumis6. - After adding
4,totalSumis10. - After adding the second
3,totalSumis13. - After adding the second
2,totalSumis15. - After adding the second
1,totalSumis16.
- After adding
Step 2: Iterate Through the Array to Find the Pivot Index
- Initialize the
leftSumvariable to0. - Iterate through the array, calculating the
leftSumand comparing it with the sum of elements to the right of the current index to find the pivot index.
Iteration 1: Index 0 ([1])
leftSumis0.- The sum to the right is
totalSum - leftSum - nums[i] = 16 - 0 - 1 = 15. - The sums are not equal, so continue.
Iteration 2: Index 1 ([1, 2])
- Update
leftSumto1(0 + 1). - The sum to the right is
totalSum - leftSum - nums[i] = 16 - 1 - 2 = 13. - The sums are not equal, so continue.
Iteration 3: Index 2 ([1, 2, 3])
- Update
leftSumto3(1 + 2). - The sum to the right is
totalSum - leftSum - nums[i] = 16 - 3 - 3 = 10. - The sums are not equal, so continue.
Iteration 4: Index 3 ([1, 2, 3, 4])
- Update
leftSumto6(3 + 3). - The sum to the right is
totalSum - leftSum - nums[i] = 16 - 6 - 4 = 6. - The sums are equal, which means we've found the pivot index at
3.
Final Result:
The pivot index for the array [1, 2, 3, 4, 3, 2, 1] is 3, as the sum of the numbers to the left of index 3 is equal to the sum of the numbers to its right, both summing to 6.
Code
Complexity Analysis
Time Complexity
The time complexity for the algorithm is O(n), where (n) is the number of elements in the input array. This efficiency is due to the algorithm requiring a single pass to calculate the total sum and another pass to find the pivot index, making the operations linear in time relative to the size of the input.
Space Complexity
The space complexity of the algorithm is O(1). This is because the amount of extra space required does not scale with the size of the input array.
On this page
Problem Statement
Examples
Solution
Step-by-step algorithm:
Algorithm Walkthrough
Code
Complexity Analysis
Time Complexity
Space Complexity