mediumArraysPattern: Iterative
Equilibrium Peak Finder Solution
Problem Statement
Given an integer array, identify the index of the first 'Equilibrium Peak'. An element is defined as an Equilibrium Peak if the sum of all elements strictly to its left is equal to the sum of all elements strictly to its right. If multiple indices satisfy this condition, return the smallest index. If no such index exists, return -1.
Examples
Example 1:
Input:nums = [1, 7, 3, 6, 5, 6]
Output:3
Explanation: At index 3, the sum of elements to the left (1+7+3 = 11) equals the sum of elements to the right (5+6 = 11).
Example 2:
Input:nums = [1, 2, 3]
Output:-1
Explanation: No index satisfies the condition where left sum equals right sum.
Constraints
- 1 <= nums.length <= 10^5
- -10^4 <= nums[i] <= 10^4
- Total sum of elements will not exceed the range of a 32-bit integer
Time: O(n) Space: O(1)
Calculate the total sum of the array once, then iterate through the array while maintaining a running left sum. At each index, determine the right sum by subtracting the left sum and the current element from the total sum, achieving O(n) time complexity.
Run, Test & Submit Code
Ready to practice this challenge? Launch our interactive compilation environment with compiler validation.
Solve on Interactive WorkspaceTested Solutions
No solution code is currently loaded.
Complete this code in the workspace editor.
