Skip to content

Commit

Permalink
Merge pull request fineanmol#5658 from Amisha2093/master
Browse files Browse the repository at this point in the history
Add equilibriumpoint.cpp
  • Loading branch information
fineanmol authored Oct 6, 2023
2 parents 432087e + 3549fe8 commit 35f9372
Showing 1 changed file with 33 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
// Equilibrium Point
// Find an equilibrium point such that sum of elements before it is equal to sum of elements after it.

#include<bits/stdc++.h>
using namespace std;
#define eff ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);
#define vi vector<int>

signed main(){
eff;
int n;
cin>>n;
vi v(n);
for(int i = 0; i < n; i++)
cin>>v[i];

int sum = accumulate(v.begin(), v.end(), 0);
int left = 0;
for(int i = 0; i < n; i++)
{
if(left == sum - v[i])
{
cout<<i<<" ";
break;
}
left += v[i];
sum-=v[i];
}
return 0;
}

// Time Complexity: O(n)
// Space Complexity: O(1)

0 comments on commit 35f9372

Please sign in to comment.