Skip to content

Commit a381578

Browse files
NeuroByte79pre-commit-ci[bot]cclauss
authored
Add edge cases for equilibrium index (#15372)
* Add edge cases for equilibrium index * [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci * Refactor docstring in equilibrium_index_in_array.py Updated docstring examples and formatting for clarity. --------- Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com> Co-authored-by: Christian Clauss <cclauss@me.com>
1 parent c6012e3 commit a381578

1 file changed

Lines changed: 30 additions & 22 deletions

File tree

data_structures/arrays/equilibrium_index_in_array.py

Lines changed: 30 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,54 +1,62 @@
11
"""
22
Find the Equilibrium Index of an Array.
3-
Reference: https://www.geeksforgeeks.org/equilibrium-index-of-an-array/
43
5-
Python doctest can be run with the following command:
6-
python -m doctest -v equilibrium_index_in_array.py
7-
8-
Given a sequence arr[] of size n, this function returns
9-
an equilibrium index (if any) or -1 if no equilibrium index exists.
10-
11-
The equilibrium index of an array is an index such that the sum of
12-
elements at lower indexes is equal to the sum of elements at higher indexes.
4+
Reference:
5+
https://www.geeksforgeeks.org/equilibrium-index-of-an-array
136
7+
Python doctest can be run with:
148
9+
python -m doctest -v equilibrium_index_in_array.py
1510
16-
Example Input:
17-
arr = [-7, 1, 5, 2, -4, 3, 0]
18-
Output: 3
11+
Given an array arr of size n, return an equilibrium index
12+
if one exists; otherwise return -1.
1913
14+
An equilibrium index is an index where the sum of all
15+
elements to the left equals the sum of all elements
16+
to the right.
2017
"""
2118

2219

2320
def equilibrium_index(arr: list[int]) -> int:
2421
"""
25-
Find the equilibrium index of an array.
26-
22+
Find the first equilibrium index of an array.
2723
Args:
28-
arr (list[int]): The input array of integers.
29-
24+
arr: The input array of integers.
3025
Returns:
31-
int: The equilibrium index or -1 if no equilibrium index exists.
32-
26+
The first equilibrium index, or -1 if none exists.
3327
Examples:
28+
>>> equilibrium_index([])
29+
-1
30+
>>> equilibrium_index([5])
31+
0
3432
>>> equilibrium_index([-7, 1, 5, 2, -4, 3, 0])
3533
3
34+
>>> equilibrium_index([2, 4, 6, 8, 10, 3])
35+
-1
3636
>>> equilibrium_index([1, 2, 3, 4, 5])
3737
-1
3838
>>> equilibrium_index([1, 1, 1, 1, 1])
3939
2
40-
>>> equilibrium_index([2, 4, 6, 8, 10, 3])
41-
-1
40+
>>> equilibrium_index([0, 0, 0])
41+
0
42+
>>> equilibrium_index([-1, -1, -1])
43+
1
44+
>>> equilibrium_index([1, -1, 0])
45+
2
46+
47+
Time Complexity:
48+
O(n), where n is the length of the array.
49+
50+
Space Complexity:
51+
O(1), using only constant extra space.
4252
"""
4353
total_sum = sum(arr)
4454
left_sum = 0
45-
4655
for i, value in enumerate(arr):
4756
total_sum -= value
4857
if left_sum == total_sum:
4958
return i
5059
left_sum += value
51-
5260
return -1
5361

5462

0 commit comments

Comments
 (0)