forked from fineanmol/Hacktoberfest2024
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request fineanmol#5658 from Amisha2093/master
Add equilibriumpoint.cpp
- Loading branch information
Showing
1 changed file
with
33 additions
and
0 deletions.
There are no files selected for viewing
33 changes: 33 additions & 0 deletions
33
Program's_Contributed_By_Contributors/C++ Programs/Array/equilibriumpoint.cpp
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,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) |