Skip to content

Commit c55a3b9

Browse files
Create 724-Find-Pivot-Index.cpp
Created 724-Find-Pivot-Index.cpp
1 parent a68908e commit c55a3b9

File tree

1 file changed

+24
-0
lines changed

1 file changed

+24
-0
lines changed

cpp/724-Find-Pivot-Index.cpp

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
class Solution {
2+
public:
3+
int pivotIndex(vector<int>& nums) {
4+
int total;
5+
for(int x: nums){
6+
total += x;
7+
}
8+
9+
int leftSum = 0;
10+
int rightSum;
11+
12+
for(int i = 0; i < nums.size(); i++){
13+
rightSum = total - nums[i] - leftSum;
14+
15+
if(leftSum == rightSum){
16+
return i;
17+
}
18+
19+
leftSum += nums[i];
20+
}
21+
22+
return -1;
23+
}
24+
};

0 commit comments

Comments
 (0)