Skip to content

Commit

Permalink
Create PivotIndex.java
Browse files Browse the repository at this point in the history
  • Loading branch information
abhishektripathi66 authored Sep 15, 2024
1 parent 6707349 commit 87a58ba
Showing 1 changed file with 29 additions and 0 deletions.
29 changes: 29 additions & 0 deletions Leetcode/PivotIndex.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package Leetcode;

public class PivotIndex {


public int pivotIndex(int[] nums) {
// int sumRight=0;
int sumLeft=0;
int sum=0;

// get the total sums
for(int i=0;i<nums.length;i++){
sum+=nums[i];
}

//reduce index i from the sum which makes it right sum and add it in the left sum
for(int i=0;i<nums.length;i++){
sum-=nums[i];
if(sum==sumLeft){
return i;
}
sumLeft+=nums[i];
}
return -1;
}



}

0 comments on commit 87a58ba

Please sign in to comment.