-
-
Notifications
You must be signed in to change notification settings - Fork 46.6k
equilibrium index in an array #9856
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
ChrisO345
merged 6 commits into
TheAlgorithms:master
from
Arunsiva003:findequilibriumindex
Oct 8, 2023
Merged
Changes from 1 commit
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
839d550
equilibrium index in an array
Arunsiva003 d161bd9
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] 7e57915
equilibrium index in an array
Arunsiva003 1d5e666
Merge branch 'findequilibriumindex' of https://github.com/Arunsiva003…
Arunsiva003 9fc6467
equilibrium index in an array
Arunsiva003 fadbd5b
equilibrium index in an array removed type in docstring
Arunsiva003 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
""" | ||
Find the Equilibrium Index of an Array. | ||
Reference: https://www.geeksforgeeks.org/equilibrium-index-of-an-array/ | ||
|
||
Python doctests can be run with the following command: | ||
python -m doctest -v equilibrium_index.py | ||
|
||
Given a sequence arr[] of size n, this function returns an equilibrium index (if any) or -1 if no equilibrium index exists. | ||
|
||
The 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. | ||
|
||
|
||
|
||
Example Input: | ||
arr = [-7, 1, 5, 2, -4, 3, 0] | ||
Output: 3 | ||
|
||
""" | ||
|
||
def equilibrium_index(arr: list[int], n: int) -> int: | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Please provide descriptive name for the parameter: |
||
""" | ||
Find the equilibrium index of an array. | ||
|
||
Args: | ||
arr (List[int]): The input array of integers. | ||
n (int): The size of the array. | ||
Arunsiva003 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
Returns: | ||
int: The equilibrium index or -1 if no equilibrium index exists. | ||
|
||
Examples: | ||
>>> equilibrium_index([-7, 1, 5, 2, -4, 3, 0], 7) | ||
3 | ||
>>> equilibrium_index([1, 2, 3, 4, 5], 5) | ||
-1 | ||
>>> equilibrium_index([1, 1, 1, 1, 1], 5) | ||
2 | ||
>>> equilibrium_index([2, 4, 6, 8, 10, 3], 6) | ||
-1 | ||
""" | ||
total_sum = sum(arr) | ||
left_sum = 0 | ||
|
||
for i in range(n): | ||
total_sum -= arr[i] | ||
if left_sum == total_sum: | ||
return i | ||
left_sum += arr[i] | ||
|
||
return -1 | ||
|
||
if __name__ == "__main__": | ||
import doctest | ||
|
||
doctest.testmod() |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please provide descriptive name for the parameter:
n