Equilibrium index java: In the previous article, we have seen Java Program to Print All the Unique Elements of an Array
In this article we are going to find the equilibrium indices in an integer array in Java.
Java Program to Find Equilibrium Indices from a Given Array of Integers
Array is a data structure which stores a fixed size sequential collection of values of single type. Where with every array elements/values memory location is associated. Each array elements have it’s own index where array index starts from 0.
In Array set of variables referenced by a single variable name and it’s array index position. It is also called as a container object which contains elements of similar type.
Declaration of an array:
dataType[] arrayName; (or) //Declaring an array dataType []arrayName; (or) dataType arr[];
Instantiation of an Array:
arrayName = new datatype[size]; //Allocating memory to array
Combining both Statements in One:
dataType[] arrayName = new dataType[size] //Declaring and Instantiating array
Initialization of an Array:
arrayName[index-0]= arrayElement1 //Initializing the array ... arrayName[index-s]= arrayElementS
Combining all Statements in One:
dataType arrayName[ ]={e1,e2,e3}; //declaration, instantiation and initialization
Equilibrium Index:
In an array, an equilibrium index refers to an index into the array such that the sum of elements at lower indices is equal to the sum of elements at higher indices.
For example: A0 = -8 A1= 2 A2 = 5 A3 = 2 A4 = -5 A5 = 3 A6 = 0 Here 3 is an equilibrium index. As A0+A1+A2 = A4+A5+A6 Also 6 is an equilibrium index. As A0+A1+A2+A3+A4+A5+A6 = 0(sum of zero element is 0)
Let’s see different ways to find duplicate string value in a string array.
Method-1: Java Program to Find the Duplicate Values of an Array of String Values By Static Initialization of Array Elements
Approach:
- Create an array
- Display the array
- Find the sum of all elements of the array and store them
- Traverse through the array and add elements in each iteration
- Check where the iterating sum is equal to the difference between the total sum and the iterating sum
- Print the indices.
Program:
import java.util.*; public class Main { public static void main(String args[]) { // Crating an array int arr[] = {-7, 1, 5, 2, -4, 3, 0}; // Displaying the array System.out.print("Array: "); printArray(arr); // Print equilibrium indices equIndice(arr); } // Function to print the array static void printArray(int arr[]) { for(int i = 0; i < arr.length ; i++) System.out.print(arr[i]+" "); System.out.println(); } static void equIndice(int[] arr) { //find sum of all elements int sum = 0; for (int i : arr) { sum += i; } //compare current sum with total sum to find equilibrium indices int currentSum = 0; for (int i = 0; i < arr.length; i++) { int n = arr[i]; if (sum - currentSum - n == currentSum) { System.out.println("Equilibrium indices found at : "+i); } currentSum += n; } } }
Output: Array: -7 1 5 2 -4 3 0 Equilibrium indices found at : 3 Equilibrium indices found at : 6
Have you mastered basic programming topics of java and looking forward to mastering advanced topics in a java programming language? Go with these ultimate Advanced java programs examples with output & achieve your goal in improving java coding skills.
Related Java Programs: