Javatpoint Logo
Javatpoint Logo

Equilibrium index of an array in C++

An equilibrium index of a sequence is an index within a sequence such that the total elements at lower indices equals the total number of elements at higher indices.

For example, in a sequence A:

A{0}=-8

A{1}=2

A{2}=5

A{3}=2

A{4}=-6

A{5}=3

A{6}=0

3 is the equilibrium index. A{0}+A{1}+A{2}=A{4}+A{5}+A{6}

7 is not an equilibrium because it is not in range.

In other words, an array's equilibrium index is an index i at which the total of elements with indices less than i equals the sum of elements with indices greater than i. We know that the component at index i is not included in either portion, and it is specified that if more than one equilibrium index exists, you must return the first one. If no equilibrium index is found, it Return -1.

Method 1: Using two loops

In this method, we use two loops in which the outer loop continues through all the elements, while the inner loop determines if the current index selected by the outer loop is an equilibrium index. This solution has a time complexity of O(N^2). The objective of this approach is to find the sum of all events for each index. The outer loop traverses the array of values, and the inner loop determines whether or not it contains an equilibrium index.

The sequence of steps:

  • Making use of two loops.
  • The outer loop of the code iterates through all of the elements, while the inner loop determines whether the current index selected by the outer loop is an equilibrium index.
  • Make a loop through the array.
  • Find the sum of the components to both sides of the current index for every index.
  • The current index is the state of the equilibrium index if the left_sum and right_sum become equal.
  • If not, return -1.
  • This solution has an O(n^2) time complexity.

Filename: Equilibrium_index.cpp

Output:

6

Method 2: (Tricky and efficient)

Calculate the total sum of the array and initialize the left sum of the array to 0. While traversing the array, subtract the current element from the left sum to obtain the correct sum. At each step, double-check the left and right sums. Return the current index if both of them are equal.

The first goal is to obtain the array's total sum. After that, iterate through the array and update the left sum value. We can acquire the correct sum in the loop by subtracting the items individually. Store the array's prefixed sum. The prefix sum could help maintain track of the sum of every element in the array up to any index. Now, we need to figure out how to keep track of the sum of the values that are to the right of the current index's value.

We can utilize a temporary sum variable that initially maintains the sum of the array's elements. We get the total values to the right if we remove the value at the moment from the index. Now, evaluate the left_sum and right_sum values to determine whether the current index corresponds to the equilibrium index.

Algorithm:

  • Set left_sum to zero.
  • Calculate the whole array sum as the sum
  • Iterate through the array, doing the following for each index: i.
    1. Update the total to obtain the correct sum.
  1. sum = sum - array[i]
  2. // total is now an accurate sum
    1. If left_sum equals sum, return the current index.
  3. // Recalculate the left_sum for the next iteration.
    1. left_sum = array[i] + left_sum
  • return -1 // If we exit the loop without returning, there will be no equilibrium index.

Filename: EqIndex.cpp

Output:

The First equilibrium index of the array is 6

Method 3:

It is quite a simple and easy way. The objective is to multiply the array's prefix sum by two. Once using the array's front end and other from its rear end.

After collecting both prefix sums, execute a loop and see if both prefix sums from one array are identical to the corresponding prefix sums from the other array for at least one index 'i'. If this criterion is satisfied, this point might be considered the equilibrium point.

Filename: Equindex. cpp

Output:

The First Point of equilibrium of the array is at index 6 

Complexity:

Time complexity: O(N)

Space Complexity: O(N)







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