LeetCode //C - 724. Find Pivot Index

724. Find Pivot Index

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

Constraints:
  • 1 < = n u m s . l e n g t h < = 1 0 4 1 <= nums.length <= 10^4 1<=nums.length<=104
  • -1000 <= nums[i] <= 1000

From: LeetCode
Link: 724. Find Pivot Index


Solution:

Ideas:
  1. Calculate the Total Sum: First, calculate the total sum of all elements in the array.

  2. Iterate and Compare Sums: Iterate through each element of the array. For each index, calculate the left sum (which starts from 0 and accumulates as we iterate). The right sum at any index can be obtained by subtracting the current left sum and the current element from the total sum. If at any point, the left sum equals the right sum, return the current index.

  3. No Pivot Found Case: If no such index is found, return -1.

Code:
int pivotIndex(int* nums, int numsSize) {
   
    int totalSum = 0, leftSum = 0;

    // Calculate the total sum of the array
    for (int i = 0; i < numsSize; i++) {
   
        totalSum += nums[i];
    }

    // Iterate through the array to find the pivot index
    for (int i = 0; i < numsSize; i++) {
   
        // Right sum is total sum minus left sum minus current element
        if (leftSum == (totalSum - leftSum - nums[i])) {
   
            return i;
        }
        leftSum += nums[i];
    }

    // Return -1 if no pivot index is found
    return -1;
}

相关推荐

  1. LeetCode //C - 724. Find Pivot Index

    2024-01-02 05:08:01       35 阅读
  2. leetcode704. 二分查找

    2024-01-02 05:08:01       38 阅读
  3. 704. 二分查找

    2024-01-02 05:08:01       35 阅读
  4. Leetcode:704. 二分查找

    2024-01-02 05:08:01       48 阅读
  5. 704.二分查找

    2024-01-02 05:08:01       39 阅读
  6. ERC721解读

    2024-01-02 05:08:01       31 阅读

最近更新

  1. TCP协议是安全的吗?

    2024-01-02 05:08:01       18 阅读
  2. 阿里云服务器执行yum,一直下载docker-ce-stable失败

    2024-01-02 05:08:01       19 阅读
  3. 【Python教程】压缩PDF文件大小

    2024-01-02 05:08:01       19 阅读
  4. 通过文章id递归查询所有评论(xml)

    2024-01-02 05:08:01       20 阅读

热门阅读

  1. 使用 Docker 安装 Redis、MySQL、MinIO 和 RocketMQ

    2024-01-02 05:08:01       42 阅读
  2. 项目搭建中的mysql_rpt层,项目中遇到的code2问题

    2024-01-02 05:08:01       33 阅读
  3. 零工经济:雇佣到合作的转变

    2024-01-02 05:08:01       33 阅读
  4. python的bytearray对象的使用

    2024-01-02 05:08:01       36 阅读
  5. jsp介绍

    jsp介绍

    2024-01-02 05:08:01      36 阅读
  6. 4种常见的跨域问题

    2024-01-02 05:08:01       30 阅读
  7. 疯狂程序员之重头暴学英语语法宝典!!!

    2024-01-02 05:08:01       28 阅读
  8. 使用TypeReference解析泛型数据类型

    2024-01-02 05:08:01       57 阅读
  9. I2C通信协议:设备互联的黄金标准

    2024-01-02 05:08:01       30 阅读
  10. 训练生成手写体数字 对抗神经网络

    2024-01-02 05:08:01       39 阅读