You are on page 1of 3

#include <stdio.

h>

int findEquilibriumIndex(int A[], int n) {

int leftSum = 0;

int totalSum = 0;

for (int i = 0; i < n; i++) {

totalSum += A[i];

for (int i = 0; i < n; i++) {

totalSum -= A[i];

if (leftSum == totalSum) {

return i;

leftSum += A[i];

return -1; // No equilibrium index found

int main(void) {

int n;

printf("Enter the size of the array: ");

scanf("%d", &n);

int A[n];

printf("Enter the elements in the array (space-separated): ");

for (int i = 0; i < n; i++) {

scanf("%d", &A[i]);

int equilibriumIndex = findEquilibriumIndex(A, n);

if (equilibriumIndex != -1) {

printf("Equilibrium Index found at %d\n", equilibriumIndex);

} else {

printf("No Equilibrium Index found in the array\n");

return 0;

}
1. Equilibrium in the context of this program and problem refers to an index (position) within an array where
the sum of elements on the left side of that index is equal to the sum of elements on the right side of that
index. In other words, it's the point where the array can be "balanced."

The program provided does the following:

 User Input: It prompts the user to input the size of an array and the elements of the array.
 Calculation of Equilibrium Index: It then calculates and searches for the equilibrium index within the array by
examining each element in the array.
 Find Equilibrium Index: To find the equilibrium index, the program iterates through the array, calculating the
sum of elements to the left and the sum of elements to the right of the current element.
 Display Result: If an equilibrium index is found (where the left sum equals the right sum), it displays the
index. If no equilibrium index is found, it displays a message indicating that no equilibrium index exists.

Now, let's break down the program step by step:

 User is prompted to enter the size of the array and its elements.
 The program calculates the total sum of all elements in the array.
 It then iterates through the array. For each element at index i, it calculates the left sum (sum of elements
from index 0 to i-1) and the right sum (sum of elements from i+1 to the end of the array).
 If it finds an index where the left sum is equal to the right sum, it means an equilibrium index is found.
 The program displays the equilibrium index if one is found, or it displays a message indicating that no
equilibrium index exists.

This program essentially checks for points in the array where the sum of elements to the left is equal to the sum of
elements to the right, indicating a point of balance or equilibrium.

2. The algorithm used in this program to find the equilibrium index in an array is a linear algorithm that iterates
through the array only once. This is often referred to as a linear or one-pass algorithm.

Here are the key steps of the algorithm:

 Calculate Total Sum: Calculate the total sum of all elements in the array. This step involves iterating through
the array once.
 Iterate through the Array: For each element in the array, calculate the left sum (sum of elements to the left
of the current element) and the right sum (sum of elements to the right of the current element).
 Check for Equilibrium: Check if the left sum is equal to the right sum at the current index. If yes, the current
index is an equilibrium index.
 Move to the Next Element: Move to the next element in the array and repeat the process.

The algorithm only requires a single pass through the array, making it a linear time complexity algorithm, O(n),
where n is the size of the array. It's an efficient approach for finding equilibrium indices in an array without the need
for nested loops or excessive computations.

3. The program doesn't explicitly use a complex data structure. It relies on basic arrays and variables to store
and manipulate the input data. The primary data structures used are:
 Array (int A[]): The array is used to store the elements provided by the user. It is the main data structure
being manipulated to find the equilibrium index.
 Variables (leftSum, totalSum, right, etc.): Simple variables are used to store intermediate and final
results during the computation. For example, leftSum and totalSum are used to calculate the sum of
elements on the left side and the total sum of all elements, respectively.

 Scalars (integers): Basic scalar variables are used to store indices, sizes, and other integer values.
The algorithm in the program doesn't require more sophisticated data structures such as linked lists, trees, or
queues. It efficiently operates on the given array, performing calculations and comparisons to determine the
equilibrium index. The simplicity of the algorithm allows it to be implemented with straightforward data
structures.

You might also like