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

## Java Array: Exercise-62 with Solution

Write a Java program to find the equilibrium indices from 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:**

```
import java.util.Arrays;
public class solution {
public static void main(String[] args) {
int[] nums = {-7, 1, 5, 2, -4, 3, 0};
System.out.println("Original array: "+Arrays.toString(nums));
equlibrium_indices(nums);
}
public static void equlibrium_indices(int[] nums){
//find total sum
int totalSum = 0;
for (int n : nums) {
totalSum += n;
}
//compare running sum to remaining sum to find equlibrium indices
int runningSum = 0;
for (int i = 0; i < nums.length; i++) {
int n = nums[i];
if (totalSum - runningSum - n == runningSum) {
System.out.println("Equilibrium indices found at : "+i);
}
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:**

**Improve this sample solution and post your code through Disqus**

**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?**

## Java: Tips of the Day

**Array vs ArrayLists:**

The main difference between these two is that an Array is of fixed size so once you have created an Array you cannot change it but the ArrayList is not of fixed size. You can create instances of ArrayLists without specifying its size. So if you create such instances of an ArrayList without specifying its size Java will create an instance of an ArrayList of default size.

Once an ArrayList is full it re-sizes itself. In fact, an ArrayList is internally supported by an array. So when an ArrayList is resized it will slow down its performance a bit as the contents of the old Array must be copied to a new Array.

At the same time, it's compulsory to specify the size of an Array directly or indirectly while creating it. And also Arrays can store both primitives and objects while ArrayLists only can store objects.

Ref: https://bit.ly/3o8L2KH

**New Content published on w3resource:**- Scala Programming Exercises, Practice, Solution
- Python Itertools exercises
- Python Numpy exercises
- Python GeoPy Package exercises
- Python Pandas exercises
- Python nltk exercises
- Python BeautifulSoup exercises
- Form Template
- Composer - PHP Package Manager
- PHPUnit - PHP Testing
- Laravel - PHP Framework
- Angular - JavaScript Framework
- Vue - JavaScript Framework
- Jest - JavaScript Testing Framework