## Problem Description

Given an array of integers `nums`

, write a function to find the “pivot” index of this array.

The pivot index is the index where the sum of all the numbers to the left of the index is equal to the sum of all the numbers to the right of the index.

If no such index exists, return `-1`

.

## Example 724 Find Pivot Index

**vbnet**

```
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
```

## Approach and Solution 724 Find Pivot Index

To solve this problem, we can first calculate the sum of all elements in the array. We can then traverse the array from left to right and keep track of the sum of elements we’ve seen so far. At each index, we can check if the sum of elements to the left of the index is equal to the sum of elements to the right of the index. If so, we have found the pivot index.

Here’s the pseudocode for the algorithm:

**python**

```
total_sum = sum of all elements in nums
left_sum = 0
for i in range(len(nums)):
if left_sum == total_sum - left_sum - nums[i]:
return i
left_sum += nums[i]
return -1
```

## Complexity Analysis

The time complexity of this algorithm is O(n), where n is the length of the input array. We traverse the array twice – once to calculate the total sum and once to find the pivot index.

The space complexity is O(1), as we only need to store the total sum and the sum of elements seen so far.

## Conclusion

In this article, we’ve discussed how to solve the LeetCode problem 724 – Find Pivot Index. We’ve described the problem, provided an example, outlined an approach, and given a solution. The algorithm has a time complexity of O(n) and a space complexity of O(1).