Skip to content

Commit

Permalink
Merge pull request ZoranPandovski#3310 from shivesh41kr/patch-3
Browse files Browse the repository at this point in the history
Equilibrium Point in an Array
  • Loading branch information
ZoranPandovski authored Oct 11, 2022
2 parents 31e0891 + 8fdb498 commit bcf67bc
Showing 1 changed file with 44 additions and 0 deletions.
44 changes: 44 additions & 0 deletions search/Equilibrium_Point_in_an_Array.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
//Equilibrium index of an Array using Array-Sum

#include <bits/stdc++.h>
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;
}

int main()
{
int arr[] = { -7, 1, 5, 2, -4, 3, 0 };
int arr_size = sizeof(arr) / sizeof(arr[0]);

// Function call
cout << "First equilibrium index is "
<< equilibrium(arr, arr_size);
return 0;
}

/*
Time Complexity: O(N)
Auxiliary Space: O(1)
*/

0 comments on commit bcf67bc

Please sign in to comment.