BackmediumArrays Paytm

Equilibrium Segment Balance Solution

Problem Statement

Given an integer array nums, determine if there exists an index i such that the sum of elements to the left of i is equal to the sum of elements to the right of i. If such an index exists, return the leftmost index. If no such index exists, return -1. Note that the element at index i itself is excluded from both the left and right summations.

Example 1
Input
nums = [1, 7, 3, 6, 5, 6]
Output
3

Explanation: At index 3, the left sum is 1 + 7 + 3 = 11 and the right sum is 5 + 6 = 11.

Example 2
Input
nums = [1, 2, 3]
Output
-1

Explanation: No index satisfies the condition as the left and right sums never balance.

Constraints

  • 1 <= nums.length <= 10^5
  • -1000 <= nums[i] <= 1000
  • The sum of all elements in the array will not exceed 2^31 - 1
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