Skip to content

Commit 3d0a409

Browse files
Improved Equilibrium Index of an Array. (TheAlgorithms#10899)
* Improved Equilibrium Index of an Array. This is the modifications made to the original code: 1. Create Doctest Instructions: Python "doctest" can be executed by running the following command: python -m doctest -v equilibrium_index.py. 2. Deleted Argument {size}: Deleted the `size` argument because `len(arr)} allows the array's length to be determined inside the function, simplifying and improving the readability of the function signature. 3. Used {enumerate}: To improve code readability and indicate that we're working with element-index pairs, we iterated through the array using both elements and their indices using the `enumerate` function. 4. Optimized the Loop: To prevent pointless additions, the loop was improved by initializing {left_sum} with the value of the first element (arr[0]). Furthermore, since the beginning and last items (0 and size - 1) cannot be equilibrium indices, there is no need to check them, saving further computations. * [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --------- Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
1 parent c2c6cb0 commit 3d0a409

File tree

1 file changed

+10
-11
lines changed

1 file changed

+10
-11
lines changed

data_structures/arrays/equilibrium_index_in_array.py

+10-11
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
Find the Equilibrium Index of an Array.
33
Reference: https://www.geeksforgeeks.org/equilibrium-index-of-an-array/
44
5-
Python doctests can be run with the following command:
5+
Python doctest can be run with the following command:
66
python -m doctest -v equilibrium_index.py
77
88
Given a sequence arr[] of size n, this function returns
@@ -20,35 +20,34 @@
2020
"""
2121

2222

23-
def equilibrium_index(arr: list[int], size: int) -> int:
23+
def equilibrium_index(arr: list[int]) -> int:
2424
"""
2525
Find the equilibrium index of an array.
2626
2727
Args:
28-
arr : The input array of integers.
29-
size : The size of the array.
28+
arr (list[int]): The input array of integers.
3029
3130
Returns:
3231
int: The equilibrium index or -1 if no equilibrium index exists.
3332
3433
Examples:
35-
>>> equilibrium_index([-7, 1, 5, 2, -4, 3, 0], 7)
34+
>>> equilibrium_index([-7, 1, 5, 2, -4, 3, 0])
3635
3
37-
>>> equilibrium_index([1, 2, 3, 4, 5], 5)
36+
>>> equilibrium_index([1, 2, 3, 4, 5])
3837
-1
39-
>>> equilibrium_index([1, 1, 1, 1, 1], 5)
38+
>>> equilibrium_index([1, 1, 1, 1, 1])
4039
2
41-
>>> equilibrium_index([2, 4, 6, 8, 10, 3], 6)
40+
>>> equilibrium_index([2, 4, 6, 8, 10, 3])
4241
-1
4342
"""
4443
total_sum = sum(arr)
4544
left_sum = 0
4645

47-
for i in range(size):
48-
total_sum -= arr[i]
46+
for i, value in enumerate(arr):
47+
total_sum -= value
4948
if left_sum == total_sum:
5049
return i
51-
left_sum += arr[i]
50+
left_sum += value
5251

5352
return -1
5453

0 commit comments

Comments
 (0)