Imagine you have a line of people standing on a seesaw. Each person is holding a bag of weight in their hands. The seesaw is like an array of numbers, and each person’s weight is represented by a number in the array. (Equilibrium Index of an Array Problem)
The Equilibrium Index is like a special point on the seesaw where the total weight on one side is balanced with the total weight on the other side.
If you were to put a pivot point at the Equilibrium Index, the sum of the weights on the left side would be equal to the sum of the weights on the right side.
Let’s break it down a bit more:
- You have an array of numbers, let’s say [1, 3, 5, 2, 2]. Each number represents a person’s weight and their bag of weight.
- You start from the first person (the first number) and move to the last person (the last number) on the seesaw.
- For each person, you calculate the total weight on the left side of the person and the total weight on the right side of the person.
- When you find a person where the total weight on the left side is equal to the total weight on the right side, that person’s position is the Equilibrium Index.
- In our example array [1, 3, 5, 2, 2], the Equilibrium Index is 2 because if you put the pivot point after the person with weight 5, the total weight on both sides becomes equal (1 + 3 = 2 + 2).
So, the Equilibrium Index is like the point on the seesaw where if you balance it, the weights on both sides are same. It’s a point where the sum of weights to the left is equal to the sum of weights to the right in an array of numbers.(Equilibrium Index of an Array Problem)
Here’s how you could solve the Equilibrium Index problem in both Java and Python:
Java:
public class EquilibriumIndex {
public static int findEquilibriumIndex(int[] arr) {
int totalSum = 0;
int leftSum = 0;
// Calculate the total sum of the array
for (int num : arr) {
totalSum += num;
}
for (int i = 0; i < arr.length; i++) {
totalSum -= arr[i]; // Subtract the current element from the total sum
if (leftSum == totalSum) {
return i;
}
leftSum += arr[i]; // Add the current element to the left sum
}
return -1; // No equilibrium index found
}
public static void main(String[] args) {
int[] arr = {1, 3, 5, 2, 2};
int equilibriumIndex = findEquilibriumIndex(arr);
if (equilibriumIndex != -1) {
System.out.println("Equilibrium Index: " + equilibriumIndex);
} else {
System.out.println("No Equilibrium Index found");
}
}
}
Python:
def find_equilibrium_index(arr):
total_sum = sum(arr)
left_sum = 0
for i in range(len(arr)):
total_sum -= arr[i]
if left_sum == total_sum:
return i
left_sum += arr[i]
return -1
arr = [1, 3, 5, 2, 2]
equilibrium_index = find_equilibrium_index(arr)
if equilibrium_index != -1:
print(f"Equilibrium Index: {equilibrium_index}")
else:
print("No Equilibrium Index found")
Both versions of the code calculate the equilibrium index of an array. The logic is the same in both languages. They iterate through the array, calculating the total sum and keeping track of the left sum as they go. When left sum becomes equal to the remaining sum (total sum – left sum), the current index is the equilibrium index. If no equilibrium index is found, they return -1.
Thank You!