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 loopsIn 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:
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:
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) Next Topicflock() function in C++ |
We provides tutorials and interview questions of all technology like java tutorial, android, java frameworks
G-13, 2nd Floor, Sec-3, Noida, UP, 201301, India