DEV Community

Cover image for Find pivot index
zuzexx
zuzexx

Posted on • Updated on

Find pivot index

Todays challenge comes from Leetcode. The task is to find a pivot index of a given array.

/*
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:
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
*/
Enter fullscreen mode Exit fullscreen mode

In this challenge, we are given an array of integers and asked to find the pivot index of the 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.

My Solution

One way to solve this challenge is to use two loops to calculate the sum of all the elements in the array and the sum of the elements on the left. Then, we can iterate through the array, comparing the left sum and the right sum at each index. If the left sum is equal to the right sum, we have found the pivot index and can return it.

Here is an example of a function that solves this challenge using this approach:


const findPivot = (array) => {
  let allSum = 0;
  let leftSum = 0;

  for (let i = 0; i < array.length; i++) {
    allSum += array[i];
  }

  for (let i = 0; i < array.length; i++) {
    if (allSum - leftSum - array[i] === leftSum) {
      return i;
    }
    leftSum += array[i];
  }
  return -1;
};

console.log(findPivot([1, 7, 3, 6, 5, 6]));

Enter fullscreen mode Exit fullscreen mode

In this function, we first initialize two variables: allSum and leftSum. We then use a loop to calculate the sum of all the elements in the array and store it in the allSum variable. Next, we use another loop to iterate through the array and calculate the sum of the elements on the left at each index, storing it in the leftSum variable.

As we are iterating through the array, we check if the left sum is equal to the right sum (which is calculated by subtracting the left sum and the current element from the allSum variable). If the left sum is equal to the right sum, we have found the pivot index and we return it. If the pivot index is not found, the function returns -1.

It is worth noting that this approach has a time complexity of O(n) as we are iterating through the array twice, once to calculate the allSum and once to find the pivot index.

Additionally, it is important to consider edge cases such as when the pivot index is located on the left or right edge of the array. In these cases, the left or right sum would be 0 as there are no elements to the left or right respectively. To account for these cases, we can add a condition to check if the current index is 0 or the last index of the array, and set the left or right sum accordingly.

In conclusion, this is one approach to solving the pivot index challenge. By using two loops and comparing the left and right sum at each index, we are able to find the pivot index in a time-efficient manner while also considering edge cases. With a little bit of practice and understanding of the problem, this challenge can be easily solved.

Top comments (1)

Collapse
 
kumarkalyan profile image
Kumar Kalyan

Gonna try it for sure :)