- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Find the Equilibrium Index of an array in Java?
In Java, Array is an object. It is a non-primitive data type which stores values of similar data type.
As per the problem statement we have to find an index such that the sum of elements at lower indexes is equal to the sum of elements at higher indexes and hence that is called the equilibrium index of an array.
In this article, you will see how to get the equilibrium index of an array by using Java programming language. Let’s explore.
To Show You Some Instances
Instance-1
Suppose the original array is {-4, 6, 2, 1, -7, 5, 3 }
After finding equilibrium index of an array the result index will be − 2
Instance-2
Suppose the original array is {-7, 1, 5, 2, -4, 3, 0}.
After finding equilibrium index of an array the result index will be − 3
Instance-3
Suppose the original array is {11, 22, 33, 44, 55}
After finding equilibrium index of an array the result index will be − -1
Algorithm
Step 1 − Declare and initialize an integer array.
Step 2 − Convert array to string.
Step 3 − Find the total sum of the element.
Step 4 − Compare the current sum to the remaining sum to find equilibrium indices.
Step 5 − Print the desired result.
Syntax
To get the length of an array (number of elements in that array), there is an inbuilt property of array i.e length.
Below refers to the syntax of it −
array.length
Where, ‘array’ refers to the array reference.
To get the string representation of the content of the array or to convert the content of the array to string, Java Arrays class provides an inbuilt toString() method.
Below is the syntax of it −
arr.toString()
where, ‘arr’ refers to the array reference.
Multiple Approaches
We have provided the solution in different approaches.
By Using Static Initialization of Array Elements.
By Using User Defined Method.
Let’s see the program along with its output one by one.
Approach-1: By Using Static Initialization of Array Elements
Example
In this approach, array elements will be initialized in the program. Then as per the algorithm find the total sum and compare the current sum to the remaining sum to find equilibrium indices.
import java.util.Arrays; public class Main { //main method public static void main(String[] args) { //Declare and initialize the array elements int[] nums = {-4, 6, 2, 1, -7, 5, 3 }; //converting array to string System.out.println("Original array: "+Arrays.toString(nums)); //find total sum of element in an array int Sum = 0; for (int n : nums){ Sum += n; } //compare current sum to remaining sum to find equilibrium indices int currentSum = 0; for (int i = 0; i < nums.length; i++){ int n = nums[i]; if (Sum - currentSum - n == currentSum){ //printing the desired output System.out.println("Equilibrium indices is found at index number : "+i); } currentSum += n; } } }
Output
Original array: [-4, 6, 2, 1, -7, 5, 3] Equilibrium indices is found at index number : 2
Approach-2: By Using User Defined Method
Example
In this approach, array elements will be initialized in the program. Then call a user defined method by passing the array as parameter and inside method as per the algorithm find the total sum and compare the current sum to remaining sum to find equilibrium indices.
import java.util.Arrays; public class Main{ //main method public static void main(String[] args){ //Declare and initialize the array elements int[] nums = {-4, 6, 2, 1, -7, 5, 3 }; //converting array to string System.out.println("Original array: "+Arrays.toString(nums)); equal(nums); } //user defined method public static void equal(int[] nums){ //find total sum of element in an array int Sum = 0; for (int n : nums){ Sum += n; } //compare current sum to remaining sum to find equilibrium indices int currentSum = 0; for (int i = 0; i < nums.length; i++) { int n = nums[i]; if (Sum - currentSum - n == currentSum){ //printing the desired output System.out.println("Equilibrium indices is found at index number : "+i); } currentSum += n; } } }
Output
Original array: [-4, 6, 2, 1, -7, 5, 3] Equilibrium indices is found at index number : 2
In this article, we explored how to find the equilibrium index of an array by using Java programming language.