LeetCode/cs/find-pivot-index.cs
Matej Focko 7fb942c159
cs: add “724. Find Pivot Index”
Signed-off-by: Matej Focko <mfocko@redhat.com>
2024-01-07 16:00:47 +01:00

18 lines
363 B
C#

public class Solution {
public int PivotIndex(int[] nums) {
var fromLeft = 0;
var fromRight = nums.Sum();
for (var i = 0; i < nums.Length; ++i) {
fromRight -= nums[i];
if (fromLeft == fromRight) {
return i;
}
fromLeft += nums[i];
}
return -1;
}
}