BackeasyArrays HCL

Array Pivot Index Solution

Problem Statement

Given an array of integers, find the index where the sum of the elements to the left is equal to the sum of the elements to the right.

Example 1
Input
[1, 2, 3, 4, 5]
Output
0

Explanation: Step-by-step: Given array [1, 2, 3, 4, 5], we calculate the left sum (1) and right sum (7) at index 0. Since the left sum is equal to the right sum, the pivot index is 0.

Example 2
Input
[1, 2, 3, 4, 5, 6]
Output
2

Explanation: Step-by-step: Given array [1, 2, 3, 4, 5, 6], we calculate the left sum (1+2+3 = 6) and right sum (4+5+6 = 15) at index 2. Since the left sum is not equal to the right sum, we move to the next index. At index 3, the left sum (1+2+3+4 = 10) is not equal to the right sum (5+6 = 11). At index 4, the left sum (1+2+3+4+5 = 15) is equal to the right sum (6 = 6). Therefore, the pivot index is 4.

Constraints

  • 1 <= n <= 10^4
  • -1000 <= arr[i] <= 1000
Live Compiler
Loading...
Test Cases & Output
🔒 Sign up to run your code

🚀 Practice this problem

Run code, get AI hints & track streak

Sign Up Free