Skip to content

Commit 468386b

Browse files
authored
Create 724. Find Pivot Index
1 parent 6a1b375 commit 468386b

File tree

1 file changed

+23
-0
lines changed

1 file changed

+23
-0
lines changed

Arrays/724. Find Pivot Index

+23
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
class Solution {
2+
public:
3+
int pivotIndex(vector<int>& nums) {
4+
5+
int sum = 0 ; int leftsum = 0 ;
6+
int n = nums.size() ;
7+
//calc total array
8+
for(int i = 0 ; i < n ; i++)
9+
{
10+
sum += nums[i];
11+
}
12+
13+
14+
for(int i = 0 ; i < n ; i++)
15+
{
16+
if(leftsum == (sum - nums[i] - leftsum))
17+
return i ; //Input: nums = [1,7,3,6,5,6]
18+
leftsum += nums[i] ; // update leftsum 0 = 0 + 1 = 1 and 1 + 7 = 8 and 8 + 3 = 11
19+
}
20+
21+
return -1;
22+
}
23+
};

0 commit comments

Comments
 (0)