# Java: Find the equilibrium indices from a given array of integers

## Java Array: Exercise-62 with Solution

Write a Java program to find equilibrium indices in a given array of integers.

An equilibrium index of a sequence is an index into the sequence such that the sum of elements at lower indices is equal to the sum of elements at higher indices.

For example, in a sequenceA:A_{0}= -7A_{1}= 1A_{2}= 5A_{3}= 2A_{4}= -4A_{5}= 3A3 is an equilibrium index, because:_{6}= 0A6 is also an equilibrium index, because:_{0}+ A_{1}+ A_{2}= A_{4}+ A_{5}+ A_{6}A(sum of zero elements is zero) 7 is not an equilibrium index, because it is not a valid index of sequence_{0}+ A_{1}+ A_{2}+ A_{3}+ A_{4}+ A_{5}= 0A.

Example:

Input :

nums = {-7, 1, 5, 2, -4, 3, 0}

Output:

Equilibrium indices found at : 3

Equilibrium indices found at : 6

Source: https://bit.ly/2ziUROQ

**Sample Solution**:

**Java Code:**

```
//Source: https://bit.ly/2ziUROQ
// Import the necessary Java class.
import java.util.Arrays;
// Define a class named 'solution'.
public class solution {
public static void main(String[] args) {
// Initialize an array of integers.
int[] nums = {-7, 1, 5, 2, -4, 3, 0};
System.out.println("Original array: " + Arrays.toString(nums));
// Call the 'equlibrium_indices' method to find equilibrium indices.
equlibrium_indices(nums);
}
public static void equlibrium_indices(int[] nums) {
// Find the total sum of elements in the array.
int totalSum = 0;
for (int n : nums) {
totalSum += n;
}
// Initialize a running sum to keep track of the sum as we iterate.
int runningSum = 0;
// Iterate through the array to find equilibrium indices.
for (int i = 0; i < nums.length; i++) {
int n = nums[i];
// Check if the current index is an equilibrium index.
if (totalSum - runningSum - n == runningSum) {
System.out.println("Equilibrium indices found at: " + i);
}
// Update the running sum as we move through the array.
runningSum += n;
}
}
}
```

Sample Output:

Original array: [-7, 1, 5, 2, -4, 3, 0] Equilibrium indices found at : 3 Equilibrium indices found at : 6

**Flowchart:**

**Java Code Editor:**

**Previous:** Write a Java program to rearrange a given array of unique elements such that every second element of the array is greater than its left and right elements.

**Next:** Write a Java program to replace each element of the array with product of every other element in a given array of integers.

**What is the difficulty level of this exercise?**

Test your Programming skills with w3resource's quiz.

**Weekly Trends**- Python Interview Questions and Answers: Comprehensive Guide
- Scala Exercises, Practice, Solution
- Kotlin Exercises practice with solution
- MongoDB Exercises, Practice, Solution
- SQL Exercises, Practice, Solution - JOINS
- Java Basic Programming Exercises
- SQL Subqueries
- Adventureworks Database Exercises
- C# Sharp Basic Exercises
- SQL COUNT() with distinct
- JavaScript String Exercises
- JavaScript HTML Form Validation
- Java Collection Exercises
- SQL COUNT() function
- SQL Inner Join