Skip to content

Commit

Permalink
Added the problem 724-Find-Pivot-Index in java
Browse files Browse the repository at this point in the history
  • Loading branch information
mkhuzaima authored Dec 8, 2022
1 parent a8550cb commit 0f2a5e7
Showing 1 changed file with 24 additions and 0 deletions.
24 changes: 24 additions & 0 deletions java/724-Find-Pivot-Index.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
class Solution {
public int pivotIndex(int[] nums) {
int leftSum = 0;
int rightSum = 0;

int i;
for (i = 1; i < nums.length; i++) {
rightSum += nums[i];
}

if (leftSum == rightSum) {
return 0;
}

for (i = 1; i < nums.length; i++ ){
leftSum += nums[i-1];
rightSum -= nums[i];
if (leftSum== rightSum) {
return i;
}
}
return -1;
}
}

0 comments on commit 0f2a5e7

Please sign in to comment.