Menu Close

Leetcode 724: Find Pivot Index Solution

Here, we will see how to solve Find Pivot Index Solution of leet code 724 problem.

You are given an array of integers nums. You have to 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.

You have to return the leftmost pivot index. If no such index exists, return -1.

Example 1:

Input: nums = [2,7,3,6,5,7]
Output: 3
Explanation:
The pivot index is 3.
Left sum = nums[0] + nums[1] + nums[2] = 2 + 7 + 3 = 12
Right sum = nums[4] + nums[5] = 5 + 7 = 12

Example 2:

Input: nums = [1,2,3,4]
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

Find Pivot Index Solution code in C++ and Go lang:

Here, we will be solving problem in multiple ways with code.

C++ code 1:

class Solution {
public:
    int pivotIndex(vector<int>& nums) {
        int total_sum = 0, sum = 0;
        for(auto it : nums) {
            total_sum += it;
        }
        for(int i = 0; i < nums.size(); i++) {
            if(total_sum - (2 * sum + nums[i]) == 0) {
                return i;
            }
            sum += nums[i];
        }
        return -1;
    }
};

C++ code 2:

class Solution {
 public:
  int pivotIndex(vector<int>& nums) {
    const int sum = accumulate(begin(nums), end(nums), 0);
    int prefix = 0;

    for (int i = 0; i < nums.size(); ++i) {
      if (prefix == sum - prefix - nums[i])
        return i;
      prefix += nums[i];
    }

    return -1;
  }
};

Go code 1:

func pivotIndex(nums []int) int {
    total_sum := 0
    right_sum := 0
    left_sum := 0
    
    for i := 0; i < len(nums); i++ {
        total_sum += nums[i];
    }
    for i := 0; i < len(nums); i++ {
        right_sum = total_sum - left_sum - nums[i];
        
        if left_sum == right_sum {
            return i;
        }
        left_sum += nums[i];
    }
    return -1;
}

Go code 2:

func pivotIndex(nums []int) int {
    temp := 0
	sumArray := make([]int, len(nums)+1)
	for key, value := range nums {
		temp += value
		sumArray[key+1] = temp
	}
	for key, value := range sumArray[1:] {
		if value+sumArray[key] == temp {
			return key
		}
	}
	return -1
}

Output:

Input: nums = [1,2,3,4,5]
Output: [1,3,6,10,15]

To check more leetcode problem’s solution. Pls click given below link:

https://www.techieindoor.com/category/leetcode/

https://www.techieindoor.com/category/leetcode/

Posted in C++, Easy, golang, golang program, Leetcode

Leave a Reply

Your email address will not be published. Required fields are marked *