# Equilibrium index of an array

• Difficulty Level : Easy
• Last Updated : 13 Jul, 2022

Equilibrium index of an array is an index such that the sum of elements at lower indexes is equal to the sum of elements at higher indexes. For example, in an array A:

Example : Input: A[] = {-7, 1, 5, 2, -4, 3, 0}
Output: 3
3 is an equilibrium index, because:
A + A + A = A + A + A

Input: A[] = {1, 2, 3}
Output: -1

Write a function int equilibrium(int[] arr, int n); that given a sequence arr[] of size n, returns an equilibrium index (if any) or -1 if no equilibrium indexes exist.

Recommended Practice

Method 1 (Simple but inefficient)
Use two loops. Outer loop iterates through all the element and inner loop finds out whether the current index picked by the outer loop is equilibrium index or not. Time complexity of this solution is O(n^2).

## C++

 `// C++ program to find equilibrium ` `// index of an array ` `#include ` `using` `namespace` `std; ` ` `  `int` `equilibrium(``int` `arr[], ``int` `n) ` `{ ` `    ``int` `i, j; ` `    ``int` `leftsum, rightsum; ` ` `  `    ``/* Check for indexes one by one until  ` `    ``an equilibrium index is found */` `    ``for` `(i = 0; i < n; ++i)  ` `    ``{      ` ` `  `        ``/* get left sum */` `        ``leftsum = 0;  ` `        ``for` `(j = 0; j < i; j++) ` `            ``leftsum += arr[j]; ` ` `  `        ``/* get right sum */` `        ``rightsum = 0;  ` `        ``for` `(j = i + 1; j < n; j++) ` `            ``rightsum += arr[j]; ` ` `  `        ``/* if leftsum and rightsum   ` `        ``are same, then we are done */` `        ``if` `(leftsum == rightsum) ` `            ``return` `i; ` `    ``} ` ` `  `    ``/* return -1 if no equilibrium  ` `    ``index is found */` `    ``return` `-1; ` `} ` ` `  `// Driver code ` `int` `main() ` `{ ` `    ``int` `arr[] = { -7, 1, 5, 2, -4, 3, 0 }; ` `    ``int` `arr_size = ``sizeof``(arr) / ``sizeof``(arr); ` `    ``cout << equilibrium(arr, arr_size); ` `    ``return` `0; ` `} ` ` `  `// This code is contributed  ` `// by Akanksha Rai(Abby_akku) `

## C

 `// C program to find equilibrium ` `// index of an array ` ` `  `#include ` ` `  `int` `equilibrium(``int` `arr[], ``int` `n) ` `{ ` `    ``int` `i, j; ` `    ``int` `leftsum, rightsum; ` ` `  `    ``/* Check for indexes one by one until  ` `      ``an equilibrium index is found */` `    ``for` `(i = 0; i < n; ++i) {        ` ` `  `        ``/* get left sum */` `        ``leftsum = 0;  ` `        ``for` `(j = 0; j < i; j++) ` `            ``leftsum += arr[j]; ` ` `  `        ``/* get right sum */` `        ``rightsum = 0;  ` `        ``for` `(j = i + 1; j < n; j++) ` `            ``rightsum += arr[j]; ` ` `  `        ``/* if leftsum and rightsum are same,  ` `           ``then we are done */` `        ``if` `(leftsum == rightsum) ` `            ``return` `i; ` `    ``} ` ` `  `    ``/* return -1 if no equilibrium index is found */` `    ``return` `-1; ` `} ` ` `  `// Driver code ` `int` `main() ` `{ ` `    ``int` `arr[] = { -7, 1, 5, 2, -4, 3, 0 }; ` `    ``int` `arr_size = ``sizeof``(arr) / ``sizeof``(arr); ` `    ``printf``(``"%d"``, equilibrium(arr, arr_size)); ` ` `  `    ``getchar``(); ` `    ``return` `0; ` `} `

## Java

 `// Java program to find equilibrium ` `// index of an array ` ` `  `class` `EquilibriumIndex { ` `    ``int` `equilibrium(``int` `arr[], ``int` `n) ` `    ``{ ` `        ``int` `i, j; ` `        ``int` `leftsum, rightsum; ` ` `  `        ``/* Check for indexes one by one until  ` `           ``an equilibrium index is found */` `        ``for` `(i = ``0``; i < n; ++i) { ` ` `  `            ``/* get left sum */` `            ``leftsum = ``0``;   ` `            ``for` `(j = ``0``; j < i; j++) ` `                ``leftsum += arr[j]; ` ` `  `            ``/* get right sum */` `            ``rightsum = ``0``; ` `            ``for` `(j = i + ``1``; j < n; j++) ` `                ``rightsum += arr[j]; ` ` `  `            ``/* if leftsum and rightsum are same,  ` `               ``then we are done */` `            ``if` `(leftsum == rightsum) ` `                ``return` `i; ` `        ``} ` ` `  `        ``/* return -1 if no equilibrium index is found */` `        ``return` `-``1``; ` `    ``} ` `    ``// Driver code ` `    ``public` `static` `void` `main(String[] args) ` `    ``{ ` `        ``EquilibriumIndex equi = ``new` `EquilibriumIndex(); ` `        ``int` `arr[] = { -``7``, ``1``, ``5``, ``2``, -``4``, ``3``, ``0` `}; ` `        ``int` `arr_size = arr.length; ` `        ``System.out.println(equi.equilibrium(arr, arr_size)); ` `    ``} ` `} ` ` `  `// This code has been contributed by Mayank Jaiswal `

## Python3

 `# Python program to find equilibrium  ` `# index of an array ` ` `  `# function to find the equilibrium index ` `def` `equilibrium(arr): ` `    ``leftsum ``=` `0` `    ``rightsum ``=` `0` `    ``n ``=` `len``(arr) ` ` `  `    ``# Check for indexes one by one  ` `    ``# until an equilibrium index is found ` `    ``for` `i ``in` `range``(n): ` `        ``leftsum ``=` `0` `        ``rightsum ``=` `0` `     `  `        ``# get left sum ` `        ``for` `j ``in` `range``(i): ` `            ``leftsum ``+``=` `arr[j] ` `         `  `        ``# get right sum ` `        ``for` `j ``in` `range``(i ``+` `1``, n): ` `            ``rightsum ``+``=` `arr[j] ` `         `  `        ``# if leftsum and rightsum are same, ` `        ``# then we are done ` `        ``if` `leftsum ``=``=` `rightsum: ` `            ``return` `i ` `     `  `    ``# return -1 if no equilibrium index is found ` `    ``return` `-``1` `             `  `# driver code ` `arr ``=` `[``-``7``, ``1``, ``5``, ``2``, ``-``4``, ``3``, ``0``] ` `print` `(equilibrium(arr)) ` ` `  `# This code is contributed by Abhishek Sharama `

## C#

 `// C# program to find equilibrium ` `// index of an array ` ` `  `using` `System; ` ` `  `class` `GFG { ` `    ``static` `int` `equilibrium(``int``[] arr, ``int` `n) ` `    ``{ ` `        ``int` `i, j; ` `        ``int` `leftsum, rightsum; ` ` `  `        ``/* Check for indexes one by  ` `         ``one until an equilibrium ` `        ``index is found */` `        ``for` `(i = 0; i < n; ++i) { ` ` `  `            ``// initialize left sum ` `            ``// for current index i ` `            ``leftsum = 0; ` ` `  `            ``// initialize right sum ` `            ``// for current index i ` `            ``rightsum = 0; ` ` `  `            ``/* get left sum */` `            ``for` `(j = 0; j < i; j++) ` `                ``leftsum += arr[j]; ` ` `  `            ``/* get right sum */` `            ``for` `(j = i + 1; j < n; j++) ` `                ``rightsum += arr[j]; ` ` `  `            ``/* if leftsum and rightsum are ` `             ``same, then we are done */` `            ``if` `(leftsum == rightsum) ` `                ``return` `i; ` `        ``} ` ` `  `        ``/* return -1 if no equilibrium  ` `         ``index is found */` `        ``return` `-1; ` `    ``} ` ` `  `    ``// driver code ` `    ``public` `static` `void` `Main() ` `    ``{ ` `        ``int``[] arr = { -7, 1, 5, 2, -4, 3, 0 }; ` `        ``int` `arr_size = arr.Length; ` ` `  `        ``Console.Write(equilibrium(arr, arr_size)); ` `    ``} ` `} ` ` `  `// This code is contributed by Sam007 `

## PHP

 ` `

## Javascript

 ``

Output

`3`

Time Complexity: O(n^2)

Auxiliary Space: O(1)

Method 2 (Tricky and Efficient)
The idea is to get the total sum of the array first. Then Iterate through the array and keep updating the left sum which is initialized as zero. In the loop, we can get the right sum by subtracting the elements one by one. Thanks to Sambasiva for suggesting this solution and providing code for this.

```1) Initialize leftsum  as 0
2) Get the total sum of the array as sum
3) Iterate through the array and for each index i, do following.
a)  Update sum to get the right sum.
sum = sum - arr[i]
// sum is now right sum
b) If leftsum is equal to sum, then return current index.
// update leftsum for next iteration.
c) leftsum = leftsum + arr[i]
4) return -1
// If we come out of loop without returning then
// there is no equilibrium index```

The image below shows the dry run of the above approach: Below is the implementation of the above approach:

## C++

 `// C++ program to find equilibrium  ` `// index of an array  ` `#include ` `using` `namespace` `std; ` ` `  `int` `equilibrium(``int` `arr[], ``int` `n)  ` `{  ` `    ``int` `sum = 0; ``// initialize sum of whole array  ` `    ``int` `leftsum = 0; ``// initialize leftsum  ` ` `  `    ``/* Find sum of the whole array */` `    ``for` `(``int` `i = 0; i < n; ++i)  ` `        ``sum += arr[i];  ` ` `  `    ``for` `(``int` `i = 0; i < n; ++i)  ` `    ``{  ` `        ``sum -= arr[i]; ``// sum is now right sum for index i  ` ` `  `        ``if` `(leftsum == sum)  ` `            ``return` `i;  ` ` `  `        ``leftsum += arr[i];  ` `    ``}  ` ` `  `    ``/* If no equilibrium index found, then return 0 */` `    ``return` `-1;  ` `}  ` ` `  `// Driver code  ` `int` `main()  ` `{  ` `    ``int` `arr[] = { -7, 1, 5, 2, -4, 3, 0 };  ` `    ``int` `arr_size = ``sizeof``(arr) / ``sizeof``(arr);  ` `    ``cout << ``"First equilibrium index is "` `<< equilibrium(arr, arr_size);  ` `    ``return` `0;  ` `}  ` ` `  `// This is code is contributed by rathbhupendra `

## C

 `// C program to find equilibrium ` `// index of an array ` ` `  `#include ` ` `  `int` `equilibrium(``int` `arr[], ``int` `n) ` `{ ` `    ``int` `sum = 0; ``// initialize sum of whole array ` `    ``int` `leftsum = 0; ``// initialize leftsum ` ` `  `    ``/* Find sum of the whole array */` `    ``for` `(``int` `i = 0; i < n; ++i) ` `        ``sum += arr[i]; ` ` `  `    ``for` `(``int` `i = 0; i < n; ++i) { ` `        ``sum -= arr[i]; ``// sum is now right sum for index i ` ` `  `        ``if` `(leftsum == sum) ` `            ``return` `i; ` ` `  `        ``leftsum += arr[i]; ` `    ``} ` ` `  `    ``/* If no equilibrium index found, then return 0 */` `    ``return` `-1; ` `} ` ` `  `// Driver code ` `int` `main() ` `{ ` `    ``int` `arr[] = { -7, 1, 5, 2, -4, 3, 0 }; ` `    ``int` `arr_size = ``sizeof``(arr) / ``sizeof``(arr); ` `    ``printf``(``"First equilibrium index is %d"``,  ` `                 ``equilibrium(arr, arr_size)); ` ` `  `    ``getchar``(); ` `    ``return` `0; ` `} `

## Java

 `// Java program to find equilibrium ` `// index of an array ` ` `  `class` `EquilibriumIndex { ` `    ``int` `equilibrium(``int` `arr[], ``int` `n) ` `    ``{ ` `        ``int` `sum = ``0``; ``// initialize sum of whole array ` `        ``int` `leftsum = ``0``; ``// initialize leftsum ` ` `  `        ``/* Find sum of the whole array */` `        ``for` `(``int` `i = ``0``; i < n; ++i) ` `            ``sum += arr[i]; ` ` `  `        ``for` `(``int` `i = ``0``; i < n; ++i) { ` `            ``sum -= arr[i]; ``// sum is now right sum for index i ` ` `  `            ``if` `(leftsum == sum) ` `                ``return` `i; ` ` `  `            ``leftsum += arr[i]; ` `        ``} ` ` `  `        ``/* If no equilibrium index found, then return 0 */` `        ``return` `-``1``; ` `    ``} ` ` `  `   ``// Driver code ` `    ``public` `static` `void` `main(String[] args) ` `    ``{ ` `        ``EquilibriumIndex equi = ``new` `EquilibriumIndex(); ` `        ``int` `arr[] = { -``7``, ``1``, ``5``, ``2``, -``4``, ``3``, ``0` `}; ` `        ``int` `arr_size = arr.length; ` `        ``System.out.println(``"First equilibrium index is "` `+  ` `                          ``equi.equilibrium(arr, arr_size)); ` `    ``} ` `} ` ` `  `// This code has been contributed by Mayank Jaiswal `

## Python3

 `# Python program to find the equilibrium ` `# index of an array ` ` `  `# function to find the equilibrium index ` `def` `equilibrium(arr): ` ` `  `    ``# finding the sum of whole array ` `    ``total_sum ``=` `sum``(arr) ` `    ``leftsum ``=` `0` `    ``for` `i, num ``in` `enumerate``(arr): ` `         `  `        ``# total_sum is now right sum ` `        ``# for index i ` `        ``total_sum ``-``=` `num ` `         `  `        ``if` `leftsum ``=``=` `total_sum: ` `            ``return` `i ` `        ``leftsum ``+``=` `num ` `      `  `      ``# If no equilibrium index found,  ` `      ``# then return -1 ` `    ``return` `-``1` `     `  `# Driver code ` `arr ``=` `[``-``7``, ``1``, ``5``, ``2``, ``-``4``, ``3``, ``0``] ` `print` `(``'First equilibrium index is '``, ` `       ``equilibrium(arr)) ` ` `  `# This code is contributed by Abhishek Sharma `

## C#

 `// C# program to find the equilibrium ` `// index of an array ` ` `  `using` `System; ` ` `  `class` `GFG { ` `    ``static` `int` `equilibrium(``int``[] arr, ``int` `n) ` `    ``{ ` `        ``// initialize sum of whole array ` `        ``int` `sum = 0; ` ` `  `        ``// initialize leftsum ` `        ``int` `leftsum = 0; ` ` `  `        ``/* Find sum of the whole array */` `        ``for` `(``int` `i = 0; i < n; ++i) ` `            ``sum += arr[i]; ` ` `  `        ``for` `(``int` `i = 0; i < n; ++i) { ` ` `  `            ``// sum is now right sum ` `            ``// for index i ` `            ``sum -= arr[i]; ` ` `  `            ``if` `(leftsum == sum) ` `                ``return` `i; ` ` `  `            ``leftsum += arr[i]; ` `        ``} ` ` `  `        ``/* If no equilibrium index found,  ` `        ``then return 0 */` `        ``return` `-1; ` `    ``} ` ` `  `    ``// Driver code ` `    ``public` `static` `void` `Main() ` `    ``{ ` `        ``int``[] arr = { -7, 1, 5, 2, -4, 3, 0 }; ` `        ``int` `arr_size = arr.Length; ` ` `  `        ``Console.Write(``"First equilibrium index is "` `+ ` `                        ``equilibrium(arr, arr_size)); ` `    ``} ` `} ` `// This code is contributed by Sam007`

## PHP

 ` `

## Javascript

 ``

Output

`First equilibrium index is 3`

Time Complexity: O(n)

Auxiliary Space: O(1)

Method 3 :

This is a quite simple and straightforward method. The idea is to take the prefix sum of the array twice. Once from the front end of array and another from the back end of array.

After taking both prefix sums run a loop and check for some i if both the prefix sum from one array is equal to prefix sum from the second array then that point can be considered as the Equilibrium point.

## C++

 `// C++ program to find equilibrium index of an array ` `#include ` `using` `namespace` `std; ` ` `  `int` `equilibrium(``int` `a[], ``int` `n) ` `{ ` `    ``if` `(n == 1) ` `        ``return` `(0); ` `    ``int` `forward[n] = { 0 }; ` `    ``int` `rev[n] = { 0 }; ` ` `  `    ``// Taking the prefixsum from front end array ` `    ``for` `(``int` `i = 0; i < n; i++) { ` `        ``if` `(i) { ` `            ``forward[i] = forward[i - 1] + a[i]; ` `        ``} ` `        ``else` `{ ` `            ``forward[i] = a[i]; ` `        ``} ` `    ``} ` ` `  `    ``// Taking the prefixsum from back end of array ` `    ``for` `(``int` `i = n - 1; i > 0; i--) { ` `        ``if` `(i <= n - 2) { ` `            ``rev[i] = rev[i + 1] + a[i]; ` `        ``} ` `        ``else` `{ ` `            ``rev[i] = a[i]; ` `        ``} ` `    ``} ` ` `  `    ``// Checking if forward prefix sum ` `    ``// is equal to rev prefix ` `    ``// sum ` `    ``for` `(``int` `i = 0; i < n; i++) { ` `        ``if` `(forward[i] == rev[i]) { ` `            ``return` `i; ` `        ``} ` `    ``} ` `    ``return` `-1; ` ` `  `    ``// If You want all the points ` `    ``// of equilibrium create ` `    ``// vector and push all equilibrium ` `    ``// points in it and ` `    ``// return the vector ` `} ` ` `  `// Driver code ` `int` `main() ` `{ ` `    ``int` `arr[] = { -7, 1, 5, 2, -4, 3, 0 }; ` `    ``int` `n = ``sizeof``(arr) / ``sizeof``(arr); ` `    ``cout << ``"First Point of equilibrium is at index "` `         ``<< equilibrium(arr, n) << ``"\n"``; ` `    ``return` `0; ` `}`

## Java

 `// Java program to find equilibrium ` `// index of an array ` `class` `GFG{ ` ` `  `static` `int` `equilibrium(``int` `a[], ``int` `n) ` `{  ` `    ``if` `(n == ``1``) ` `        ``return` `(``0``); ` `     `  `    ``int``[] front = ``new` `int``[n]; ` `    ``int``[] back = ``new` `int``[n]; ` ` `  `    ``// Taking the prefixsum from front end array ` `    ``for` `(``int` `i = ``0``; i < n; i++) ` `    ``{ ` `        ``if` `(i != ``0``) ` `        ``{ ` `            ``front[i] = front[i - ``1``] + a[i]; ` `        ``} ` `        ``else`  `        ``{ ` `            ``front[i] = a[i]; ` `        ``} ` `    ``} ` `   `  `    ``// Taking the prefixsum from back end of array ` `    ``for` `(``int` `i = n - ``1``; i > ``0``; i--)  ` `    ``{ ` `        ``if` `(i <= n - ``2``)  ` `        ``{ ` `            ``back[i] = back[i + ``1``] + a[i]; ` `        ``} ` `        ``else`  `        ``{ ` `            ``back[i] = a[i]; ` `        ``} ` `    ``} ` `     `  `    ``// Checking for equilibrium index by ` `    ``//comparing front and back sums  ` `    ``for``(``int` `i = ``0``; i < n; i++)  ` `    ``{ ` `        ``if` `(front[i] == back[i]) ` `        ``{ ` `            ``return` `i; ` `        ``} ` `    ``} ` `     `  `    ``// If no equilibrium index found,then return -1 ` `    ``return` `-``1``; ` `} ` ` `  `// Driver code ` `public` `static` `void` `main(String[] args) ` `{ ` `    ``int` `arr[] = { -``7``, ``1``, ``5``, ``2``, -``4``, ``3``, ``0` `}; ` `    ``int` `arr_size = arr.length; ` `     `  `    ``System.out.println(``"First Point of equilibrium "` `+ ` `                       ``"is at index "` `+  ` `                       ``equilibrium(arr, arr_size)); ` `} ` `} ` ` `  `// This code is contributed by Lovish Aggarwal`

## Python3

 `# Python program to find the equilibrium ` `# index of an array ` ` `  `# Function to find the equilibrium index ` `def` `equilibrium(arr): ` `    ``left_sum ``=` `[] ` `    ``right_sum ``=` `[] ` ` `  `    ``# Iterate from 0 to len(arr) ` `    ``for` `i ``in` `range``(``len``(arr)): ` ` `  `        ``# If i is not 0 ` `        ``if``(i): ` `            ``left_sum.append(left_sum[i``-``1``]``+``arr[i]) ` `            ``right_sum.append(right_sum[i``-``1``]``+``arr[``len``(arr)``-``1``-``i]) ` `        ``else``: ` `            ``left_sum.append(arr[i]) ` `            ``right_sum.append(arr[``len``(arr)``-``1``]) ` ` `  `    ``# Iterate from 0 to len(arr)     ` `    ``for` `i ``in` `range``(``len``(arr)): ` `        ``if``(left_sum[i] ``=``=` `right_sum[``len``(arr) ``-` `1` `-` `i ]): ` `            ``return``(i) ` `           `  `    ``# If no equilibrium index found,then return -1 ` `    ``return` `-``1` ` `  ` `  `# Driver code ` `arr ``=` `[``-``7``, ``1``, ``5``, ``2``, ``-``4``, ``3``, ``0``] ` `print``(``'First equilibrium index is '``, ` `      ``equilibrium(arr)) ` ` `  `# This code is contributed by Lokesh Sharma `

## C#

 `// C# program to find equilibrium ` `// index of an array ` `using` `System; ` ` `  `class` `GFG{ ` ` `  `static` `int` `equilibrium(``int``[] a, ``int` `n) ` `{ ` `    ``if` `(n == 1) ` `        ``return` `(0); ` ` `  `    ``int``[] front = ``new` `int``[n]; ` `    ``int``[] back = ``new` `int``[n]; ` ` `  `    ``// Taking the prefixsum from front end array ` `    ``for``(``int` `i = 0; i < n; i++)  ` `    ``{ ` `        ``if` `(i != 0) ` `        ``{ ` `            ``front[i] = front[i - 1] + a[i]; ` `        ``} ` `        ``else`  `        ``{ ` `            ``front[i] = a[i]; ` `        ``} ` `    ``} ` ` `  `    ``// Taking the prefixsum from back end of array ` `    ``for``(``int` `i = n - 1; i > 0; i--) ` `    ``{ ` `        ``if` `(i <= n - 2)  ` `        ``{ ` `            ``back[i] = back[i + 1] + a[i]; ` `        ``} ` `        ``else` `        ``{ ` `            ``back[i] = a[i]; ` `        ``} ` `    ``} ` ` `  `    ``// Checking for equilibrium index by ` `    ``// comparing front and back sums ` `    ``for``(``int` `i = 0; i < n; i++)  ` `    ``{ ` `        ``if` `(front[i] == back[i]) ` `        ``{ ` `            ``return` `i; ` `        ``} ` `    ``} ` ` `  `    ``// If no equilibrium index found,then return -1 ` `    ``return` `-1; ` `} ` ` `  `// Driver code ` `public` `static` `void` `Main(``string``[] args) ` `{ ` `    ``int``[] arr = { -7, 1, 5, 2, -4, 3, 0 }; ` `    ``int` `arr_size = arr.Length; ` ` `  `    ``Console.WriteLine(``"First Point of equilibrium "` `+  ` `                      ``"is at index "` `+  ` `                      ``equilibrium(arr, arr_size)); ` `} ` `} ` ` `  `// This code is contributed by ukasp`

## Javascript

 ``

Output

`First Point of equilibrium is at index 3`

Time Complexity: O(n)

Auxiliary Space : O(n)

Method 4:– Using binary search

To handle all the testcase, we can use binary search algorithm.

1.calculate the mid and then create left sum and right sum around mid

2.if left sum is greater than right sum, move to left until it become equal or less than right sum

3. else if right sum is greater than left, move right until it become equal or less than left sum.

4. finally we compare two sums if they are equal we got mid as index else its -1

## C++

 `#include ` `using` `namespace` `std; ` ` `  `void` `find(``int` `arr[],  ``int` `n) ` `{ ` `    ``int` `mid = n / 2; ` `    ``int` `leftSum = 0, rightSum = 0; ` ` `  `    ``//calculation sum to left of mid ` `    ``for` `(``int` `i = 0; i < mid; i++) ` `    ``{ ` `        ``leftSum += arr[i]; ` `    ``} ` `    ``//calculating sum to right of mid ` `    ``for` `(``int` `i = n - 1; i > mid; i--) ` `    ``{ ` `        ``rightSum += arr[i]; ` `    ``} ` ` `  `    ``//if rightsum > leftsum ` `    ``if` `(rightSum > leftSum) ` `    ``{ ` `        ``//we keep moving right until rightSum become equal or less than leftSum ` `        ``while` `(rightSum > leftSum && mid < n - 1) ` `        ``{ ` `            ``rightSum -= arr[mid + 1]; ` `            ``leftSum += arr[mid]; ` `            ``mid++; ` `        ``} ` `    ``} ` `    ``else` `    ``{ ` `        ``//we keep moving right until leftSum become equal or less than RightSum ` `        ``while` `(leftSum > rightSum && mid > 0) ` `        ``{ ` `            ``rightSum += arr[mid]; ` `            ``leftSum -= arr[mid - 1]; ` `            ``mid--; ` `        ``} ` `    ``} ` ` `  `    ``//check if both sum become equal ` `    ``if` `(rightSum == leftSum) ` `    ``{ ` `        ``cout <<``"First Point of equilibrium is at index ="``<< mid << endl; ` `        ``return``; ` `    ``} ` ` `  `    ``cout <<``"First Point of equilibrium is at index ="``<< -1 << endl; ` `} ` `int` `main() ` `{ ` `    ``int` `arr[] = { 1,1,1,-1,1,1,1 }; ` `    ``int` `n = ``sizeof``(arr) / ``sizeof``(arr); ` `    ``find(arr, n); ` `}`

## Java

 `// Java program for the above approach ` `import` `java.io.*; ` `import` `java.util.*; ` ` `  `class` `GFG{ ` ` `  `static` `void` `find(``int` `arr[],  ``int` `n) ` `{ ` `    ``int` `mid = n / ``2``; ` `    ``int` `leftSum = ``0``, rightSum = ``0``; ` ` `  `    ``//calculation sum to left of mid ` `    ``for` `(``int` `i = ``0``; i < mid; i++) ` `    ``{ ` `        ``leftSum += arr[i]; ` `    ``} ` `    ``//calculating sum to right of mid ` `    ``for` `(``int` `i = n - ``1``; i > mid; i--) ` `    ``{ ` `        ``rightSum += arr[i]; ` `    ``} ` ` `  `    ``//if rightsum > leftsum ` `    ``if` `(rightSum > leftSum) ` `    ``{ ` `        ``//we keep moving right until rightSum become equal or less than leftSum ` `        ``while` `(rightSum > leftSum && mid < n - ``1``) ` `        ``{ ` `            ``rightSum -= arr[mid + ``1``]; ` `            ``leftSum += arr[mid]; ` `            ``mid++; ` `        ``} ` `    ``} ` `    ``else` `    ``{ ` `        ``//we keep moving right until leftSum become equal or less than RightSum ` `        ``while` `(leftSum > rightSum && mid > ``0``) ` `        ``{ ` `            ``rightSum += arr[mid]; ` `            ``leftSum -= arr[mid - ``1``]; ` `            ``mid--; ` `        ``} ` `    ``} ` ` `  `    ``//check if both sum become equal ` `    ``if` `(rightSum == leftSum) ` `    ``{ ` `        ``System.out.print(``"First Point of equilibrium is at index ="``+ mid); ` `        ``return``; ` `    ``} ` ` `  `    ``System.out.print(``"First Point of equilibrium is at index ="` `+ -``1``); ` `} ` ` `  `// Driver code ` `public` `static` `void` `main(String args[]) ` `{ ` `    ``int` `arr[] = { ``1``,``1``,``1``,-``1``,``1``,``1``,``1` `}; ` `    ``int` `n = arr.length; ` `    ``find(arr, n); ` `} ` `}`

## Python3

 `# Python program for the above approach ` `def` `find(arr, n): ` `    ``mid ``=` `n ``/``/` `2``; ` `    ``leftSum ``=` `0``; ` `    ``rightSum ``=` `0``; ` ` `  `    ``# calculation sum to left of mid ` `    ``for` `i ``in` `range``(mid): ` `        ``leftSum ``+``=` `arr[i]; ` `     `  `    ``# calculating sum to right of mid ` `    ``for` `i ``in` `range``(n ``-` `1``, mid, ``-``1``): ` `        ``rightSum ``+``=` `arr[i]; ` `     `  ` `  `    ``# if rightsum > leftsum ` `    ``if` `(rightSum > leftSum): ` `       `  `        ``# we keep moving right until rightSum become equal or less than leftSum ` `        ``while` `(rightSum > leftSum ``and` `mid < n ``-` `1``): ` `            ``rightSum ``-``=` `arr[mid ``+` `1``]; ` `            ``leftSum ``+``=` `arr[mid]; ` `            ``mid ``+``=` `1``; ` `         `  `    ``else``: ` `        ``# we keep moving right until leftSum become equal or less than RightSum ` `        ``while` `(leftSum > rightSum ``and` `mid > ``0``): ` `            ``rightSum ``+``=` `arr[mid]; ` `            ``leftSum ``-``=` `arr[mid ``-` `1``]; ` `            ``mid ``-``=` `1``; ` `         `  `    ``# check if both sum become equal ` `    ``if` `(rightSum ``=``=` `leftSum): ` `        ``print``(``"First Point of equilibrium is at index ="` `, mid); ` `        ``return``; ` `    ``print``(``"First Point of equilibrium is at index ="` `, ``-``1``); ` ` `  `# Driver code ` `if` `__name__ ``=``=` `'__main__'``: ` `    ``arr ``=` `[ ``1``, ``1``, ``1``, ``-``1``, ``1``, ``1``, ``1` `]; ` `    ``n ``=` `len``(arr); ` `    ``find(arr, n); ` ` `  `# This code is contributed by gauravrajput1  `

## C#

 `// C# program for the above approach ` `using` `System; ` `using` `System.Collections.Generic; ` `using` `System.Linq; ` ` `  `public` `class` `GFG { ` `     `  `static` `void` `find(``int``[] arr,  ``int` `n) ` `{ ` `    ``int` `mid = n / 2; ` `    ``int` `leftSum = 0, rightSum = 0; ` ` `  `    ``//calculation sum to left of mid ` `    ``for` `(``int` `i = 0; i < mid; i++) ` `    ``{ ` `        ``leftSum += arr[i]; ` `    ``} ` `    ``//calculating sum to right of mid ` `    ``for` `(``int` `i = n - 1; i > mid; i--) ` `    ``{ ` `        ``rightSum += arr[i]; ` `    ``} ` ` `  `    ``//if rightsum > leftsum ` `    ``if` `(rightSum > leftSum) ` `    ``{ ` `        ``//we keep moving right until rightSum become equal or less than leftSum ` `        ``while` `(rightSum > leftSum && mid < n - 1) ` `        ``{ ` `            ``rightSum -= arr[mid + 1]; ` `            ``leftSum += arr[mid]; ` `            ``mid++; ` `        ``} ` `    ``} ` `    ``else` `    ``{ ` `        ``//we keep moving right until leftSum become equal or less than RightSum ` `        ``while` `(leftSum > rightSum && mid > 0) ` `        ``{ ` `            ``rightSum += arr[mid]; ` `            ``leftSum -= arr[mid - 1]; ` `            ``mid--; ` `        ``} ` `    ``} ` ` `  `    ``//check if both sum become equal ` `    ``if` `(rightSum == leftSum) ` `    ``{ ` `        ``Console.Write(``"First Point of equilibrium is at index ="``+ mid); ` `        ``return``; ` `    ``} ` ` `  `    ``Console.Write(``"First Point of equilibrium is at index ="` `+ -1); ` `} ` ` `  ` `  `// Driver Code ` `public` `static` `void` `Main (``string``[] args) { ` `     `  `    ``int``[] arr = { 1,1,1,-1,1,1,1 }; ` `    ``int` `n = arr.Length; ` `    ``find(arr, n); ` `} ` `} ` ` `  `// This code is contributed by code_hunt.`

## Javascript

 ` `

Output

`First Point of equilibrium is at index =3`

Time Complexity: O(n)

Auxiliary Space: O(1)

As pointed out by Sameer, we can remove the return statement and add a print statement to print all equilibrium indexes instead of returning only one.

Please write comments if you find the above codes/algorithms incorrect, or find better ways to solve the same problem.

My Personal Notes arrow_drop_up
Recommended Articles
Page :