Equilibrium index of an array in C++28 Aug 2024 | 5 min read 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++ |
An ordered map in C++ is a container that stores key-value pairs in a sorted order, based on the keys. It is implemented as a balanced binary search tree, which allows for efficient access, insertion, and deletion of elements. To use an ordered map in C++, you...
4 min read
A menu-driven program in the C++ programming language is a kind of interactive software application that gives the user a menu of options and lets them select from a list of actions or functionalities. These apps are frequently used in a variety of fields, including software...
4 min read
In this article, you will learn about the flattening a linked list in C++ with different approaches and examples. Flattening a linked list in C++ means converting a linked list of linked lists into a single, sorted linked list. It is a common problem in data structures...
22 min read
The process of visiting the boundary nodes of a binary tree in a particular order is referred to as boundary traversal. The left boundary, which does not include the left leaf nodes, leaf nodes, and the right boundary, which does not include the right leaf nodes,...
6 min read
A number that divides into another number exactly and without producing a leftover is said to be a factor. For instance, the factors of 20 are 1, 2, 4, 5, 10, and 20. Lets 1. Header Inclusion The input and output stream functions from the C++ Standard Library...
3 min read
C++ is a powerful and versatile programming language. It supports a wide range of programming paradigms, including concurrency. Concurrency is the ability to execute multiple threads of execution simultaneously in a program. It results in improved performance and responsiveness, especially in applications that involve I/O-bound or...
6 min read
In this article, we will discuss the neural network in C++ with its example. What is a Neural Network? The neural network is a computational model that has a structure the same as the neuron that is present in the brain. Its functionality is also the same as...
11 min read
A is a decision taking diagram that follows the sequential order that starts from the root node and ends with the lead node. Here the leaf node represents the output we want to achieve through our decision. It is directly inspired by the binary tree....
3 min read
A thread pool is a collection of threads, each with a particular task. As a result, various threads do distinct types of jobs. As a result, each thread specializes in different tasks. A thread is responsible for executing a specific set of similar functions, while another...
4 min read
An Exception caught can be caught within a try block and handled using one or more Catch Blocks. A few cases exist where the exception is to be caught using a single Catch block and rethrow it, as the Catch block at the top of the...
4 min read
We request you to subscribe our newsletter for upcoming updates.
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