diff options
| author | Paulo Custodio <pauloscustodio@gmail.com> | 2022-04-12 18:56:29 +0100 |
|---|---|---|
| committer | Paulo Custodio <pauloscustodio@gmail.com> | 2022-04-12 18:56:29 +0100 |
| commit | 49f7f459092f538b5468e255ff4e8ebac490d70a (patch) | |
| tree | fa4a94a8207a58a56d4dca98ff22d39bed047f6e /challenge-159/eric-cheung/python/ch-2.py | |
| parent | 1711da4f548d3134248cd5e02a13d71762d5e4cb (diff) | |
| parent | 72ba70a96cfd587443c3eaa0f5ba02e3557f4b82 (diff) | |
| download | perlweeklychallenge-club-49f7f459092f538b5468e255ff4e8ebac490d70a.tar.gz perlweeklychallenge-club-49f7f459092f538b5468e255ff4e8ebac490d70a.tar.bz2 perlweeklychallenge-club-49f7f459092f538b5468e255ff4e8ebac490d70a.zip | |
Merge remote-tracking branch 'upstream/master'
# Conflicts:
# challenge-160/paulo-custodio/Makefile
Diffstat (limited to 'challenge-159/eric-cheung/python/ch-2.py')
| -rwxr-xr-x | challenge-159/eric-cheung/python/ch-2.py | 41 |
1 files changed, 41 insertions, 0 deletions
diff --git a/challenge-159/eric-cheung/python/ch-2.py b/challenge-159/eric-cheung/python/ch-2.py new file mode 100755 index 0000000000..220bf69b97 --- /dev/null +++ b/challenge-159/eric-cheung/python/ch-2.py @@ -0,0 +1,41 @@ +## Remarks
+## https://www.geeksforgeeks.org/equilibrium-index-of-an-array/
+
+## 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 = leftsum + arr[j]
+
+ ## Get Right Sum
+ for j in range(i + 1, n):
+ rightsum = rightsum + arr[j]
+
+ ## If Left Sum and Right Sum are same, then we are done
+ if leftsum == rightsum:
+ return i
+
+ ## return -1 if no equilibrium index is found
+ return -1
+
+
+## Driver Code
+arr = [1, 3, 5, 7, 9] ## Example 1:
+## arr = [1, 2, 3, 4, 5] ## Example 2:
+## arr = [2, 4, 2] ## Example 3:
+
+print (equilibrium(arr))
+
+## This code is contributed by Abhishek Sharama
|
