Javatpoint Logo
Javatpoint Logo

Equilibrium index of an array

Equilibrium index of an array

Ever had a situation where you needed to locate the equilibrium point in an array? Finding the equilibrium point on a scale, where the sum of the elements on either side equals the sum of the elements on the other, is analogous to doing so. The "equilibrium index" is the name given to this important location in an array. We will examine the idea of equilibrium indices, how to recognise them, practical uses for them, and more in this article.

Understanding Array Equilibrium

The location within an array where the sum of elements to the left and right is equal is known as the equilibrium index. Mathematically, if an index 'i' exists for an array A with 'n' elements such that

A [0 to i] = A [i+1 to n]

'i' is the equilibrium index in that case.

How to Recognise Equilibrium Indices

Use a step-by-step procedure to find equilibrium indices. We determine the prefix sum of elements to its left and the suffix sum of elements to its right for each element in the array. The next step is to determine if there is an index where the prefix sum and suffix sum are equal. The index is an equilibrium index if it exists.

Take into account the array A = [1, 2, 3, 6, 3, 2, 1]. Calculating the prefix sum (zero) and suffix sum (17) begins with element 1. Element 1 is not an equilibrium index since there is no index where 0 = 17. The equilibrium indices, which in this case are 3 and 6, are obtained by repeating this procedure for each element.

Example

Assume that the array A = [-7, 1, 5, 2, -4, 3, 0] exists. Let's calculate the equilibrium indices for it:

Prefix sum = 0 and suffix sum = 0 for element -7 (equilibrium index found)

Prefix sum for element 1 is -7, and suffix sum is 8.

Prefix sum for element 5 is -6, and suffix sum is 6.

Prefix sum for element 2 is -1, and suffix sum is 1.

Prefix sum for element -4 is 1 and suffix sum is 3.

Prefix sum = -3, suffix sum = -1 for element 3.

Prefix sum = 0 and suffix sum = 0 for element 0 (equilibrium index found)

The indices of equilibrium are 0 and 6.

The Influences on Equilibrium Indices

The presence and quantity of equilibrium indices in an array are influenced by various factors, including:

  • Values of Elements in an Array: The existence of equilibrium indices is directly affected by the values of the array's elements. Equilibrium points are more probable in arrays with larger value variations.
  • Size of the Array: The number of potential equilibrium indices depends on the size of the array. Greater opportunities for balancing the total of the elements on both sides are provided by larger arrays.
  • Element Distribution: The array's element distribution is very important. In general, arrays with a lot of one-side-only elements have fewer or no equilibrium indices.

Effectiveness of Equilibrium Indices Discovery

Efficiency in finding equilibrium indices can be improved. One method entails calculating the array's element total in advance. Then, rather than recalculating the left and right sums each time, we can update them during the iterative process of identifying equilibrium indices.

This improved method has an O(n) time complexity for finding equilibrium indices, where n is the array's element count.

Practical Applications

The idea of equilibrium indices is applicable in a variety of real-world scenarios, including:

  • Equilibrium indices can be used to analyse financial data, including investment portfolios, where keeping assets in balance is essential.
  • Equilibrium indices are important in computer science and networking for distributing workload among servers fairly.
  • Climate Data: This idea can be used by climate researchers to examine temperature variations and look for data equilibrium points.

Python code:

Output:

Array: [-7, 1, 5, 2, -4, 3, 0]

Equilibrium indices: [0, 6]

As a result, equilibrium indices are intriguing locations within arrays where the total number of elements on both sides is exactly balanced. Even in complex arrays with bursty behaviour, we can successfully find these equilibrium indices by following the step-by-step procedure and refining our method. These indices are useful for load balancing in computer networks and financial analysis, among other things.

The provided code defines a Python function called find_equilibrium_indices that identifies the equilibrium indices within a given array. An equilibrium index is an index in the array where the sum of elements to the left of the index is equal to the sum of elements to the right of the index.The purpose of the code is to demonstrate how to find and identify equilibrium indices in an array where the sum of elements on the left side of the index is equal to the sum of elements on the right side.







Youtube For Videos Join Our Youtube Channel: Join Now

Feedback


Help Others, Please Share

facebook twitter pinterest

Learn Latest Tutorials


Preparation


Trending Technologies


B.Tech / MCA