forked from Vishal-Aggarwal0305/DSA-CODE
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpivotElement.cpp
More file actions
37 lines (30 loc) · 726 Bytes
/
pivotElement.cpp
File metadata and controls
37 lines (30 loc) · 726 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
//author : Priyansh67
#include<bits/stdc++.h>
using namespace std;
//using binary search
class Solution {
public:
int findMiddleIndex(vector<int>& num,int size) {
int totalsum = 0;
for(int x:num) totalsum += x;
int leftsum = 0;
for(int i=0; i<size; i++) {
totalsum = totalsum - num[i];
if(leftsum == totalsum)
return i;
else
leftsum = leftsum + num[i];
}
return -1;
}
};
int main() {
int n;
cin>>n;
vector<int> v(n);
for(int i=0;i<n;i++) cin>>v[i];
Solution s;
int ans = s.findMiddleIndex(v,n);
cout<<"Pivot Element is at index : "<<ans<<endl;
return 0;
}