January 18, 2025 |36.1K Views

Equilibrium Index

Explore Courseexplore course icon
Description
Discussion

An equilibrium index in an array is an index where the sum of elements on its left equals the sum of elements on its right. To find this, the naive approach uses nested loops to calculate the left and right sums for every index, resulting in O(n^2) time complexity. While simple, this method is inefficient for large arrays.

Optimized methods involve computing prefix and suffix sums in O(n) time. The prefix and suffix sum arrays store cumulative sums up to each index and from each index, respectively. A more space-efficient approach uses running prefix and suffix sums, comparing these at each pivot index to find the equilibrium index in O(1) space and O(n) time.

For more details, please go through - Equilibrium Index